guangchao.xu | 070005a | 2020-12-07 09:56:40 +0800 | [diff] [blame] | 1 | <template> |
| 2 | <view class="u-tabs" :style="{ |
| 3 | background: bgColor |
| 4 | }"> |
| 5 | <!-- $u.getRect()对组件根节点无效,因为写了.in(this),故这里获取内层接点尺寸 --> |
| 6 | <view :id="id"> |
| 7 | <scroll-view scroll-x class="u-scroll-view" :scroll-left="scrollLeft" scroll-with-animation> |
| 8 | <view class="u-scroll-box" :class="{'u-tabs-scorll-flex': !isScroll}"> |
| 9 | <view class="u-tab-item u-line-1" :id="'u-tab-item-' + index" v-for="(item, index) in list" :key="index" @tap="clickTab(index)" |
| 10 | :style="[tabItemStyle(index)]"> |
| 11 | <u-badge :count="item[count] || item['count'] || 0" :offset="offset" size="mini"></u-badge> |
| 12 | {{ item[name] || item['name']}} |
| 13 | </view> |
| 14 | <view v-if="showBar" class="u-tab-bar" :style="[tabBarStyle]"></view> |
| 15 | </view> |
| 16 | </scroll-view> |
| 17 | </view> |
| 18 | </view> |
| 19 | </template> |
| 20 | |
| 21 | <script> |
| 22 | /** |
| 23 | * tabs 标签 |
| 24 | * @description 该组件,是一个tabs标签组件,在标签多的时候,可以配置为左右滑动,标签少的时候,可以禁止滑动。 该组件的一个特点是配置为滚动模式时,激活的tab会自动移动到组件的中间位置。 |
| 25 | * @tutorial https://www.uviewui.com/components/tabs.html |
| 26 | * @property {Boolean} is-scroll tabs是否可以左右拖动(默认true) |
| 27 | * @property {Array} list 标签数组,元素为对象,如[{name: '推荐'}] |
| 28 | * @property {String Number} current 指定哪个tab为激活状态(默认0) |
| 29 | * @property {String Number} height 导航栏的高度,单位rpx(默认80) |
| 30 | * @property {String Number} font-size tab文字大小,单位rpx(默认30) |
| 31 | * @property {String Number} duration 滑块移动一次所需的时间,单位秒(默认0.5) |
| 32 | * @property {String} active-color 滑块和激活tab文字的颜色(默认#2979ff) |
| 33 | * @property {String} inactive-color tabs文字颜色(默认#303133) |
| 34 | * @property {String Number} bar-width 滑块宽度,单位rpx(默认40) |
| 35 | * @property {Object} active-item-style 活动tabs item的样式,对象形式 |
| 36 | * @property {Object} bar-style 底部滑块的样式,对象形式 |
| 37 | * @property {Boolean} show-bar 是否显示底部的滑块(默认true) |
| 38 | * @property {String Number} bar-height 滑块高度,单位rpx(默认6) |
| 39 | * @property {String Number} item-width 标签的宽度(默认auto) |
| 40 | * @property {String Number} gutter 单个tab标签的左右内边距之和,单位rpx(默认40) |
| 41 | * @property {String} bg-color tabs导航栏的背景颜色(默认#ffffff) |
| 42 | * @property {String} name 组件内部读取的list参数中的属性名(tab名称),见官网说明(默认name) |
| 43 | * @property {String} count 组件内部读取的list参数中的属性名(badge徽标数),同name属性的使用,见官网说明(默认count) |
| 44 | * @property {Array} offset 设置badge徽标数的位置偏移,格式为 [x, y],也即设置的为top和right的值,单位rpx(默认[5, 20]) |
| 45 | * @property {Boolean} bold 激活选项的字体是否加粗(默认true) |
| 46 | * @event {Function} change 点击标签时触发 |
| 47 | * @example <u-tabs ref="tabs" :list="list" :is-scroll="false"></u-tabs> |
| 48 | */ |
| 49 | export default { |
| 50 | name: "u-tabs", |
| 51 | props: { |
| 52 | // 导航菜单是否需要滚动,如只有2或者3个的时候,就不需要滚动了,此时使用flex平分tab的宽度 |
| 53 | isScroll: { |
| 54 | type: Boolean, |
| 55 | default: true |
| 56 | }, |
| 57 | //需循环的标签列表 |
| 58 | list: { |
| 59 | type: Array, |
| 60 | default () { |
| 61 | return []; |
| 62 | } |
| 63 | }, |
| 64 | // 当前活动tab的索引 |
| 65 | current: { |
| 66 | type: [Number, String], |
| 67 | default: 0 |
| 68 | }, |
| 69 | // 导航栏的高度和行高 |
| 70 | height: { |
| 71 | type: [String, Number], |
| 72 | default: 80 |
| 73 | }, |
| 74 | // 字体大小 |
| 75 | fontSize: { |
| 76 | type: [String, Number], |
| 77 | default: 30 |
| 78 | }, |
| 79 | // 过渡动画时长, 单位ms |
| 80 | duration: { |
| 81 | type: [String, Number], |
| 82 | default: 0.5 |
| 83 | }, |
| 84 | // 选中项的主题颜色 |
| 85 | activeColor: { |
| 86 | type: String, |
| 87 | default: '#2979ff' |
| 88 | }, |
| 89 | // 未选中项的颜色 |
| 90 | inactiveColor: { |
| 91 | type: String, |
| 92 | default: '#303133' |
| 93 | }, |
| 94 | // 菜单底部移动的bar的宽度,单位rpx |
| 95 | barWidth: { |
| 96 | type: [String, Number], |
| 97 | default: 40 |
| 98 | }, |
| 99 | // 移动bar的高度 |
| 100 | barHeight: { |
| 101 | type: [String, Number], |
| 102 | default: 6 |
| 103 | }, |
| 104 | // 单个tab的左或有内边距(左右相同) |
| 105 | gutter: { |
| 106 | type: [String, Number], |
| 107 | default: 30 |
| 108 | }, |
| 109 | // 导航栏的背景颜色 |
| 110 | bgColor: { |
| 111 | type: String, |
| 112 | default: '#ffffff' |
| 113 | }, |
| 114 | // 读取传入的数组对象的属性(tab名称) |
| 115 | name: { |
| 116 | type: String, |
| 117 | default: 'name' |
| 118 | }, |
| 119 | // 读取传入的数组对象的属性(徽标数) |
| 120 | count: { |
| 121 | type: String, |
| 122 | default: 'count' |
| 123 | }, |
| 124 | // 徽标数位置偏移 |
| 125 | offset: { |
| 126 | type: Array, |
| 127 | default: () => { |
| 128 | return [5, 20] |
| 129 | } |
| 130 | }, |
| 131 | // 活动tab字体是否加粗 |
| 132 | bold: { |
| 133 | type: Boolean, |
| 134 | default: true |
| 135 | }, |
| 136 | // 当前活动tab item的样式 |
| 137 | activeItemStyle: { |
| 138 | type: Object, |
| 139 | default() { |
| 140 | return {} |
| 141 | } |
| 142 | }, |
| 143 | // 是否显示底部的滑块 |
| 144 | showBar: { |
| 145 | type: Boolean, |
| 146 | default: true |
| 147 | }, |
| 148 | // 底部滑块的自定义样式 |
| 149 | barStyle: { |
| 150 | type: Object, |
| 151 | default() { |
| 152 | return {} |
| 153 | } |
| 154 | }, |
| 155 | // 标签的宽度 |
| 156 | itemWidth: { |
| 157 | type: [Number, String], |
| 158 | default: 'auto' |
| 159 | } |
| 160 | }, |
| 161 | data() { |
| 162 | return { |
| 163 | scrollLeft: 0, // 滚动scroll-view的左边滚动距离 |
| 164 | tabQueryInfo: [], // 存放对tab菜单查询后的节点信息 |
| 165 | componentWidth: 0, // 屏幕宽度,单位为px |
| 166 | scrollBarLeft: 0, // 移动bar需要通过translateX()移动的距离 |
| 167 | parentLeft: 0, // 父元素(tabs组件)到屏幕左边的距离 |
| 168 | id: this.$u.guid(), // id值 |
| 169 | currentIndex: this.current, |
| 170 | barFirstTimeMove: true, // 滑块第一次移动时(页面刚生成时),无需动画,否则给人怪异的感觉 |
| 171 | }; |
| 172 | }, |
| 173 | watch: { |
| 174 | // 监听tab的变化,重新计算tab菜单的布局信息,因为实际使用中菜单可能是通过 |
| 175 | // 后台获取的(如新闻app顶部的菜单),获取返回需要一定时间,所以list变化时,重新获取布局信息 |
| 176 | list(n, o) { |
| 177 | // list变动时,重制内部索引,否则可能导致超出数组边界的情况 |
| 178 | if(n.length !== o.length) this.currentIndex = 0; |
| 179 | // 用$nextTick等待视图更新完毕后再计算tab的局部信息,否则可能因为tab还没生成就获取,就会有问题 |
| 180 | this.$nextTick(() => { |
| 181 | this.init(); |
| 182 | }); |
| 183 | }, |
| 184 | current: { |
| 185 | immediate: true, |
| 186 | handler(nVal, oVal) { |
| 187 | // 视图更新后再执行移动操作 |
| 188 | this.$nextTick(() => { |
| 189 | this.currentIndex = nVal; |
| 190 | this.scrollByIndex(); |
| 191 | }); |
| 192 | } |
| 193 | }, |
| 194 | }, |
| 195 | computed: { |
| 196 | // 移动bar的样式 |
| 197 | tabBarStyle() { |
| 198 | let style = { |
| 199 | width: this.barWidth + 'rpx', |
| 200 | transform: `translate(${this.scrollBarLeft}px, -100%)`, |
| 201 | // 滑块在页面渲染后第一次滑动时,无需动画效果 |
| 202 | 'transition-duration': `${this.barFirstTimeMove ? 0 : this.duration }s`, |
| 203 | 'background-color': this.activeColor, |
| 204 | height: this.barHeight + 'rpx', |
| 205 | // 设置一个很大的值,它会自动取能用的最大值,不用高度的一半,是因为高度可能是单数,会有小数出现 |
| 206 | 'border-radius': `${this.barHeight / 2}px` |
| 207 | }; |
| 208 | Object.assign(style, this.barStyle); |
| 209 | return style; |
| 210 | }, |
| 211 | // tab的样式 |
| 212 | tabItemStyle() { |
| 213 | return (index) => { |
| 214 | let style = { |
| 215 | height: this.height + 'rpx', |
| 216 | 'line-height': this.height + 'rpx', |
| 217 | 'font-size': this.fontSize + 'rpx', |
| 218 | 'transition-duration': `${this.duration}s`, |
| 219 | padding: this.isScroll ? `0 ${this.gutter}rpx` : '', |
| 220 | flex: this.isScroll ? 'auto' : '1', |
| 221 | width: this.$u.addUnit(this.itemWidth) |
| 222 | }; |
| 223 | // 字体加粗 |
| 224 | if (index == this.currentIndex && this.bold) style.fontWeight = 'bold'; |
| 225 | if (index == this.currentIndex) { |
| 226 | style.color = this.activeColor; |
| 227 | // 给选中的tab item添加外部自定义的样式 |
| 228 | style = Object.assign(style, this.activeItemStyle); |
| 229 | } else { |
| 230 | style.color = this.inactiveColor; |
| 231 | } |
| 232 | return style; |
| 233 | } |
| 234 | } |
| 235 | }, |
| 236 | methods: { |
| 237 | // 设置一个init方法,方便多处调用 |
| 238 | async init() { |
| 239 | // 获取tabs组件的尺寸信息 |
| 240 | let tabRect = await this.$uGetRect('#' + this.id); |
| 241 | // tabs组件距离屏幕左边的宽度 |
| 242 | this.parentLeft = tabRect.left; |
| 243 | // tabs组件的宽度 |
| 244 | this.componentWidth = tabRect.width; |
| 245 | this.getTabRect(); |
| 246 | }, |
| 247 | // 点击某一个tab菜单 |
| 248 | clickTab(index) { |
| 249 | // 点击当前活动tab,不触发事件 |
| 250 | if(index == this.currentIndex) return ; |
| 251 | // 发送事件给父组件 |
| 252 | this.$emit('change', index); |
| 253 | }, |
| 254 | // 查询tab的布局信息 |
| 255 | getTabRect() { |
| 256 | // 创建节点查询 |
| 257 | let query = uni.createSelectorQuery().in(this); |
| 258 | // 历遍所有tab,这里是执行了查询,最终使用exec()会一次性返回查询的数组结果 |
| 259 | for (let i = 0; i < this.list.length; i++) { |
| 260 | // 只要size和rect两个参数 |
| 261 | query.select(`#u-tab-item-${i}`).fields({ |
| 262 | size: true, |
| 263 | rect: true |
| 264 | }); |
| 265 | } |
| 266 | // 执行查询,一次性获取多个结果 |
| 267 | query.exec( |
| 268 | function(res) { |
| 269 | this.tabQueryInfo = res; |
| 270 | // 初始化滚动条和移动bar的位置 |
| 271 | this.scrollByIndex(); |
| 272 | }.bind(this) |
| 273 | ); |
| 274 | }, |
| 275 | // 滚动scroll-view,让活动的tab处于屏幕的中间位置 |
| 276 | scrollByIndex() { |
| 277 | // 当前活动tab的布局信息,有tab菜单的width和left(为元素左边界到父元素左边界的距离)等信息 |
| 278 | let tabInfo = this.tabQueryInfo[this.currentIndex]; |
| 279 | if (!tabInfo) return; |
| 280 | // 活动tab的宽度 |
| 281 | let tabWidth = tabInfo.width; |
| 282 | // 活动item的左边到tabs组件左边的距离,用item的left减去tabs的left |
| 283 | let offsetLeft = tabInfo.left - this.parentLeft; |
| 284 | // 将活动的tabs-item移动到屏幕正中间,实际上是对scroll-view的移动 |
| 285 | let scrollLeft = offsetLeft - (this.componentWidth - tabWidth) / 2; |
| 286 | this.scrollLeft = scrollLeft < 0 ? 0 : scrollLeft; |
| 287 | // 当前活动item的中点点到左边的距离减去滑块宽度的一半,即可得到滑块所需的移动距离 |
| 288 | let left = tabInfo.left + tabInfo.width / 2 - this.parentLeft; |
| 289 | // 计算当前活跃item到组件左边的距离 |
| 290 | this.scrollBarLeft = left - uni.upx2px(this.barWidth) / 2; |
| 291 | // 第一次移动滑块的时候,barFirstTimeMove为true,放到延时中将其设置false |
| 292 | // 延时是因为scrollBarLeft作用于computed计算时,需要一个过程需,否则导致出错 |
| 293 | if(this.barFirstTimeMove == true) { |
| 294 | setTimeout(() => { |
| 295 | this.barFirstTimeMove = false; |
| 296 | }, 100) |
| 297 | } |
| 298 | } |
| 299 | }, |
| 300 | mounted() { |
| 301 | this.init(); |
| 302 | } |
| 303 | }; |
| 304 | </script> |
| 305 | |
| 306 | <style lang="scss" scoped> |
| 307 | @import "../../libs/css/style.components.scss"; |
| 308 | |
| 309 | view, |
| 310 | scroll-view { |
| 311 | box-sizing: border-box; |
| 312 | } |
| 313 | |
| 314 | /* #ifndef APP-NVUE */ |
| 315 | ::-webkit-scrollbar, |
| 316 | ::-webkit-scrollbar, |
| 317 | ::-webkit-scrollbar { |
| 318 | display: none; |
| 319 | width: 0 !important; |
| 320 | height: 0 !important; |
| 321 | -webkit-appearance: none; |
| 322 | background: transparent; |
| 323 | } |
| 324 | /* #endif */ |
| 325 | |
| 326 | .u-scroll-box { |
| 327 | position: relative; |
| 328 | /* #ifdef MP-TOUTIAO */ |
| 329 | white-space: nowrap; |
| 330 | /* #endif */ |
| 331 | } |
| 332 | |
| 333 | /* #ifdef H5 */ |
| 334 | // 通过样式穿透,隐藏H5下,scroll-view下的滚动条 |
| 335 | scroll-view ::v-deep ::-webkit-scrollbar { |
| 336 | display: none; |
| 337 | width: 0 !important; |
| 338 | height: 0 !important; |
| 339 | -webkit-appearance: none; |
| 340 | background: transparent; |
| 341 | } |
| 342 | /* #endif */ |
| 343 | |
| 344 | .u-scroll-view { |
| 345 | width: 100%; |
| 346 | white-space: nowrap; |
| 347 | position: relative; |
| 348 | } |
| 349 | |
| 350 | .u-tab-item { |
| 351 | position: relative; |
| 352 | /* #ifndef APP-NVUE */ |
| 353 | display: inline-block; |
| 354 | /* #endif */ |
| 355 | text-align: center; |
| 356 | transition-property: background-color, color; |
| 357 | } |
| 358 | |
| 359 | .u-tab-bar { |
| 360 | position: absolute; |
| 361 | bottom: 0; |
| 362 | } |
| 363 | |
| 364 | .u-tabs-scorll-flex { |
| 365 | @include vue-flex; |
| 366 | justify-content: space-between; |
| 367 | } |
| 368 | </style> |