navbar.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. <template>
  2. <view>
  3. <view v-if="isNavbar || isPrev" class="navbar-box" :style="{ height: menuClientRect.height + statusBar + menuClientRect.searchHeight + 'px','background': bg }">
  4. <view class="status-bar" :style="{ height: statusBar + 'px' }"></view>
  5. <view v-if="isNavTitle" class="navbar" :style="{ 'color':fontColor,height: menuClientRect.height + 'px','line-height': menuClientRect.height + 'px' }">
  6. <view v-if="isPrev" class="iconfont prevPage" :style="{ 'color':fontColor,'line-height': menuClientRect.height + 'px' }" :class="{'backPage': iSimmersive && !isTitle,'statusLine': iSimmersive && scroll < 10}" @click="prev"><text :style="{ 'color':fontColor}">&#xe609;</text></view>
  7. <view v-if="isTitle" class="title" :style="{ 'color':fontColor,height: menuClientRect.height + 'px','line-height': menuClientRect.height + 'px' }">{{ title }}</view>
  8. </view>
  9. <view v-if="isSearch && isTitle" class="search-box" :style="{ 'background': bg }">
  10. <view class="iconfont search-input" @click="onJumpSearch">
  11. <text>请输入关键字</text>
  12. </view>
  13. </view>
  14. </view>
  15. <view v-if="placeholder" class="placeholder-box" style="width: 100%;" :style="{ height: menuClientRect.height - 1 + menuClientRect.searchHeight + statusBar + 'px' }"></view>
  16. </view>
  17. </template>
  18. <script>
  19. import storage from "../../common/sessionStorage"
  20. export default {
  21. props: {
  22. value: {
  23. type: [String,Number],
  24. default: 0
  25. },
  26. scroll: {
  27. type: [String,Number],
  28. default: 0
  29. },
  30. placeholder: {
  31. type:Boolean,
  32. default: false
  33. },
  34. isShow: {
  35. type:Boolean,
  36. default: true
  37. },
  38. isPrev: {
  39. type:Boolean,
  40. default: true
  41. },
  42. isSearch: {
  43. type:Boolean,
  44. default: false
  45. },
  46. isNavTitle: {
  47. type:Boolean,
  48. default: true
  49. },
  50. // 导航栏标题
  51. title: {
  52. type: String,
  53. default: ''
  54. },
  55. // 标题的颜色
  56. titleColor: {
  57. type: String,
  58. default: '#000000'
  59. },
  60. // 对象形式,因为用户可能定义一个纯色,或者线性渐变的颜色
  61. background: {
  62. type: String,
  63. default: 'transparent'
  64. },
  65. iSimmersive: {
  66. type:Boolean,
  67. default: false
  68. },
  69. onBack: {
  70. type: Function,
  71. default: null
  72. }
  73. },
  74. data(){
  75. return {
  76. statusBar: 10,
  77. menuClientRect: { height: 35, searchHeight: 0 },
  78. bg: "",
  79. fontColor: "",
  80. isTitle: true,
  81. isNavbar: true
  82. };
  83. },
  84. mounted() {
  85. let systemInfoSync = uni.getSystemInfoSync();
  86. // #ifdef MP || APP-PLUS
  87. this.statusBar = systemInfoSync.statusBarHeight;
  88. // #endif
  89. // #ifdef MP
  90. this.menuClientRect = uni.getMenuButtonBoundingClientRect();
  91. // #endif
  92. this.isNavbar = this.isShow;
  93. this.bg = this.background;
  94. this.fontColor = this.titleColor;
  95. if(this.iSimmersive){
  96. this.isTitle = false;
  97. this.isNavbar = false;
  98. this.setNavigationBarColor("#ffffff");
  99. }else{
  100. this.bg = this.background != 'transparent' ? this.background : "#ffffff";
  101. this.setNavigationBarColor(this.titleColor);
  102. }
  103. if(!this.isNavTitle){
  104. this.menuClientRect.height = 0;
  105. }
  106. // #ifdef H5
  107. this.statusBar = 0;
  108. // #endif
  109. // #ifdef APP-PLUS || MP
  110. this.menuClientRect.height += 10;
  111. // #endif
  112. if(this.isSearch){
  113. this.menuClientRect.searchHeight = 45;
  114. }
  115. let navHeight = this.menuClientRect.height + this.statusBar;
  116. // #ifdef H5
  117. this.$emit("input",systemInfoSync.screenHeight - navHeight - systemInfoSync.windowBottom)
  118. // #endif
  119. // #ifdef APP-PLUS
  120. this.$emit("input",systemInfoSync.windowHeight - navHeight - systemInfoSync.windowBottom)
  121. // #endif
  122. // #ifdef MP
  123. this.$emit("input",systemInfoSync.screenHeight - navHeight)
  124. // #endif
  125. },
  126. methods: {
  127. onJumpSearch(){
  128. this.$utils.navigateTo("search/index");
  129. },
  130. prev(){
  131. if(this.onBack){
  132. this.onBack();
  133. }else{
  134. // #ifdef H5
  135. let canNavBack = getCurrentPages();
  136. if(canNavBack && canNavBack.length>1) {
  137. uni.navigateBack();
  138. } else {
  139. if(canNavBack.length <= 1){
  140. this.$utils.switchTab("index/index");
  141. }
  142. }
  143. // #endif
  144. // #ifndef H5
  145. uni.navigateBack();
  146. // #endif
  147. }
  148. },
  149. setNavigationBarColor(color){
  150. this.fontColor = color;
  151. // #ifdef MP-WEIXIN
  152. wx.setNavigationBarColor({
  153. frontColor:color,
  154. backgroundColor:'#ff0000',
  155. animation:{
  156. duration: 200,
  157. timingFunc:'easeIn'
  158. }
  159. });
  160. // #endif
  161. // #ifdef APP-PLUS
  162. setTimeout(()=>{
  163. if(color == "#000000"){
  164. plus.navigator.setStatusBarStyle("dark");
  165. }else{
  166. plus.navigator.setStatusBarStyle("light");
  167. }
  168. },200);
  169. // #endif
  170. },
  171. color2Rgb(string){
  172. let color = string.toLowerCase();
  173. if(/^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/.test(color)){
  174. if (color.length === 4) {
  175. let colorNew = "#";
  176. for (let i = 1; i < 4; i += 1) {
  177. colorNew += color.slice(i, i + 1).concat(color.slice(i, i + 1));
  178. }
  179. color = colorNew;
  180. }
  181. let array = [];
  182. for (let i = 1; i < 7; i += 2) {
  183. array.push(parseInt("0x" + color.slice(i, i + 2)));
  184. }
  185. return array.join(",");
  186. }else{
  187. return string;
  188. }
  189. }
  190. },
  191. watch:{
  192. scroll:{
  193. handler(newValue, oldValue){
  194. if(!this.iSimmersive){
  195. return false;
  196. }
  197. let bg = "#ffffff";
  198. let fontColor = "#000000";
  199. if(this.background != 'transparent'){
  200. bg = this.background;
  201. fontColor = "#ffffff";
  202. }
  203. let rgba = this.color2Rgb(bg);
  204. if(newValue >= 10 && newValue <= 50){
  205. this.bg = "rgba("+rgba+",.3)";
  206. this.setNavigationBarColor(fontColor);
  207. this.isTitle = true;
  208. this.isNavbar = true;
  209. }else if(newValue >= 51 && newValue <= 99){
  210. this.bg = "rgba("+rgba+",.7)";
  211. this.setNavigationBarColor(fontColor);
  212. this.isTitle = true;
  213. this.isNavbar = true;
  214. }else if(newValue >= 100){
  215. this.bg = "rgba("+rgba+",1)";
  216. this.setNavigationBarColor(fontColor);
  217. this.isTitle = true;
  218. this.isNavbar = true;
  219. }else if(newValue < 10){
  220. this.bg = "rgba("+rgba+",0)";
  221. this.setNavigationBarColor("#ffffff");
  222. this.isTitle = false;
  223. this.isNavbar = false;
  224. }
  225. },
  226. deep: true
  227. }
  228. }
  229. }
  230. </script>
  231. <style lang="scss" scoped>
  232. .placeholder-box { height: 35px; }
  233. .navbar-box {
  234. position: fixed; z-index: 100000; topL: 0; left: 0; width: 100%;
  235. height: 35px;
  236. .status-bar { width: 100%; float: left; }
  237. .search-box {
  238. width: 100%; height: 45px; float: left;
  239. .search-input {
  240. position: relative;
  241. color: #fff;
  242. height: 35px; line-height: 35px; border-radius: 50rpx;
  243. margin: 10rpx 20rpx; background-color: #fff; color: #666;
  244. &::before {
  245. position: absolute;
  246. content: "\e629";
  247. left: 30rpx;
  248. top: 0rpx;
  249. font-size: 38rpx;
  250. color: #aaa;
  251. }
  252. text { padding-left: 90rpx; font-size: 30rpx; }
  253. }
  254. }
  255. .navbar {
  256. float: left; width: 100%;
  257. position: relative;
  258. .title {
  259. width: 100%; text-align: center;
  260. font-size:33rpx;
  261. /* #ifdef H5 */
  262. font-size: 29rpx;
  263. /* #endif */
  264. }
  265. .prevPage {
  266. position: absolute;
  267. left: 20rpx; top: 2%;
  268. width: 60rpx; height: 60rpx;
  269. text {
  270. color: #666; font-size: 65rpx; font-weight: bold;
  271. }
  272. }
  273. .backPage {
  274. background-color: rgba(0, 0, 0, 0.5);
  275. border-radius: 50%;
  276. text{
  277. color: #fff;
  278. position: absolute; left: 30%; top: 50%; transform: translate(-30%,-50%);
  279. }
  280. }
  281. /* #ifdef H5 */
  282. .statusLine {
  283. top: 20rpx;
  284. }
  285. /* #endif */
  286. }
  287. }
  288. </style>