點擊左側,右側滾動到相應位置,
滾動右側, 左側滾動到相應位置vue
better-scroll.js
1.<scroll>是對緊鄰的元素生效
如:app
<scroll class='foods-wrapper'> <ul class=content> <li></li> </ul> </scroll>
初始化在<ul>元素上佈局
2.foods-wrapper的高度小於content高度時纔會發生滾動this
3.點擊左側菜單列表時,只須要計算右側對應的偏移距離 或是 計算對應的移動到的元素便可
方法一: 計算移動距離, 用scrollTo()方法spa
for (let i = 0; i < index; i++) { height += this.$refs.item[i].offsetHeight } this.$refs.foodsWrapper.scrollTo(0, -height)
方法二: 計算移動到的元素,用scrollToElement()方法代理
let foodsEle = this.$refs.foodsUl.getElementsByClassName('item')[index] this.$refs.foodsWrapper.scrollToElement(foodsEle, 400)
4.滾動右側列表時,會稍複雜一些. code
4.1. 由於須要知道滾動的元素在哪一個item列表區間, 所以須要計算右側五組item距離頂部的距離component
_heightArr () { let h = 0 let list = this.$refs.item list.forEach((item, i) => { h += list[i].clientHeight this.itemHeight.push(h) }) console.log(this.itemHeight) //[0, 481, 850, 2227, 2820, 3189] }
4.2 時時監聽滾動距離事件
須要在<scroll>中加如下參數
<scroll class='foods-wrapper' :listenScroll=listenScroll :probeType = 'probeType' @scroll=scroll>
其中 listenScroll probeType參數 在created中定義:ip
created () { this.listenScroll = true this.probeType = 3 }
而@scroll=scroll是在scroll.vue中代理過來的方法:
//scroll.vue if (this.listenScroll) { let me = this this.scroll.on('scroll', (position) => { me.$emit('scroll', position) //參數position: position:{x:-10, y:24} }) }
posiiton.y就是須要實時監聽的參數,即:
scroll (position) { this.scrolly = position.y }
其中 scrolly 須要在data中提早定義:
data () { return { scrolly: -1 } }
而後在watch中監聽scrolly變化便可:
watch: { scrolly (newy) { if (newy >= 0) this.currentIndex = 0 let itemHeight = this.itemHeight for (let i = 0; i < itemHeight.length - 1; i++) { let h1 = itemHeight[i] let h2 = itemHeight[i + 1] if (-newy >= h1 && -newy < h2) { this.currentIndex = i return } } } }
//左側結構 <scroll class='menu-wrapper'> <ul> <li v-for='(item,index) in foodsList' :key=index class=item :class="{active:currentIndex === index}" @click=selectMenu(index) > <span>{{item.name}}</span> </li> </ul> </scroll> //右側結構 <scroll class='foods-wrapper' ref=foodsWrapper :listenScroll=listenScroll :probeType = 'probeType' @scroll=scroll> <ul ref=foodsUl> <li v-for='(item,index) in foodsList' :key=index class=item ref=item :data-index=index> <div class=title><span class='title-name'>{{item.name}}</span><span>{{item.description}}</span></div> <ul> <li v-for='(food,i) in item.foods' :key=i class=food> //......... //略去右側詳情代碼 </li> </ul> </li> </ul> </scroll> //js部分 <script> import Scroll from "base/scroll" const H = 112 export default { data () { return { currentIndex: 0, offset: 0, scrolly: -1 } }, created () { this.listenScroll = true this.probeType = 3 this.itemHeight = [0] }, mounted () { this.$nextTick(() => { this._heightArr() }, 20); }, methods: { selectMenu (index) { let height = 0 this.currentIndex = index for (let i = 0; i < index; i++) { height += this.$refs.item[i].offsetHeight } let foodsEle = this.$refs.foodsUl.getElementsByClassName('item')[index] this.$refs.foodsWrapper.scrollToElement(foodsEle, 400) // this.$refs.foodsWrapper.scrollTo(0, -height) this.offset = height }, scroll (position) { this.scrolly = position.y }, _heightArr () { let h = 0 let list = this.$refs.item list.forEach((item, i) => { h += list[i].clientHeight this.itemHeight.push(h) }) } }, watch: { scrolly (newy) { if (newy >= 0) this.currentIndex = 0 let itemHeight = this.itemHeight for (let i = 0; i < itemHeight.length - 1; i++) { let h1 = itemHeight[i] let h2 = itemHeight[i + 1] if (-newy >= h1 && -newy < h2) { this.currentIndex = i return } } } }, components: { Scroll } } </script> //scroll.vue <template> <div ref=wrapper> <slot></slot> </div> </template> <script> import BScroll from 'better-scroll' export default { props: { probeType: { type: Number, default: 1//* 1 滾動的時候會派發scroll事件,會截流。 * 2 滾動的時候實時派發scroll事件,不會截流。 * 3 除了實時派發scroll事件,在swipe的狀況下仍然能實時派發scroll事件 }, click: { type: Boolean, default: true }, scrollX: { type: Boolean, default: false }, data: { type: Array, default: null }, listenScroll: { type: Boolean, default: false }, }, mounted () { this.$nextTick(() => { this.initScroll() }, 20) }, methods: { initScroll () { if (!this.$refs.wrapper) return this.scroll = new BScroll(this.$refs.wrapper, { probeType: this.probeType, click: this.click, scrollX: this.scrollX }) if (this.listenScroll) { let me = this this.scroll.on('scroll', (position) => { me.$emit('scroll', position) }) } }, enable () { this.scroll && this.scroll.enable() }, disable () { this.scroll && this.scroll.disable() }, refresh () { this.scroll && this.scroll.refresh() }, scrollTo () { this.scroll && this.scroll.scrollTo.apply(this.scroll, arguments) }, scrollToElement () { this.scroll && this.scroll.scrollToElement.apply(this.scroll, arguments) } }, watch: { data () { setTimeout(() => { this.scroll.refresh() }, 20) } } } </script>