vue左右側(cè)聯(lián)動(dòng)滾動(dòng)的實(shí)現(xiàn)代碼
本文介紹了vue左右側(cè)聯(lián)動(dòng)滾動(dòng)的實(shí)現(xiàn)代碼,分享給大家,具體如下:
實(shí)現(xiàn)功能:
- 點(diǎn)擊左側(cè),右側(cè)滾動(dòng)到相應(yīng)位置,
- 滾動(dòng)右側(cè), 左側(cè)滾動(dòng)到相應(yīng)位置
布局結(jié)構(gòu):
開(kāi)源滾動(dòng)庫(kù):
better-scroll.js
技術(shù)要點(diǎn):
1.<scroll>是對(duì)緊鄰的元素生效
如:
<scroll class='foods-wrapper'> <ul class=content> <li></li> </ul> </scroll>
初始化在<ul>元素上
2.foods-wrapper的高度小于content高度時(shí)才會(huì)發(fā)生滾動(dòng)
3.點(diǎn)擊左側(cè)菜單列表時(shí),只需要計(jì)算右側(cè)對(duì)應(yīng)的偏移距離 或是 計(jì)算對(duì)應(yīng)的移動(dòng)到的元素即可
方法一: 計(jì)算移動(dòng)距離, 用scrollTo()方法
for (let i = 0; i < index; i++) { height += this.$refs.item[i].offsetHeight } this.$refs.foodsWrapper.scrollTo(0, -height)
方法二: 計(jì)算移動(dòng)到的元素,用scrollToElement()方法
let foodsEle = this.$refs.foodsUl.getElementsByClassName('item')[index] this.$refs.foodsWrapper.scrollToElement(foodsEle, 400)
4.滾動(dòng)右側(cè)列表時(shí),會(huì)稍復(fù)雜一些.
4.1. 因?yàn)樾枰罎L動(dòng)的元素在哪個(gè)item列表區(qū)間, 因此需要計(jì)算右側(cè)五組item距離頂部的距離
_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 時(shí)時(shí)監(jiān)聽(tīng)滾動(dòng)距離
需要在<scroll>中加以下參數(shù)
其中 listenScroll probeType參數(shù) 在created中定義:
created () { this.listenScroll = true this.probeType = 3 }
而@scroll=scroll是在scroll.vue中代理過(guò)來(lái)的方法:
//scroll.vue if (this.listenScroll) { let me = this this.scroll.on('scroll', (position) => { me.$emit('scroll', position) //參數(shù)position: position:{x:-10, y:24} }) }
posiiton.y就是需要實(shí)時(shí)監(jiān)聽(tīng)的參數(shù),即:
scroll (position) { this.scrolly = position.y }
其中 scrolly 需要在data中提前定義:
data () { return { scrolly: -1 } }
然后在watch中監(jiān)聽(tīng)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 } } } }
代碼部分:
//左側(cè)結(jié)構(gòu) <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> //右側(cè)結(jié)構(gòu) <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> //......... //略去右側(cè)詳情代碼 </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 滾動(dòng)的時(shí)候會(huì)派發(fā)scroll事件,會(huì)截流。 * 2 滾動(dòng)的時(shí)候?qū)崟r(shí)派發(fā)scroll事件,不會(huì)截流。 * 3 除了實(shí)時(shí)派發(fā)scroll事件,在swipe的情況下仍然能實(shí)時(shí)派發(fā)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>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- vue省市區(qū)三聯(lián)動(dòng)下拉選擇組件的實(shí)現(xiàn)
- vue elementUI使用tabs與導(dǎo)航欄聯(lián)動(dòng)
- vue基于mint-ui的城市選擇3級(jí)聯(lián)動(dòng)的示例
- vue基于mint-ui實(shí)現(xiàn)城市選擇三級(jí)聯(lián)動(dòng)
- 詳解Vue、element-ui、axios實(shí)現(xiàn)省市區(qū)三級(jí)聯(lián)動(dòng)
- VUE2 前端實(shí)現(xiàn) 靜態(tài)二級(jí)省市聯(lián)動(dòng)選擇select的示例
- Vue.js組件tree實(shí)現(xiàn)省市多級(jí)聯(lián)動(dòng)
- vue mint-ui 實(shí)現(xiàn)省市區(qū)街道4級(jí)聯(lián)動(dòng)示例(仿淘寶京東收貨地址4級(jí)聯(lián)動(dòng))
- Vue實(shí)現(xiàn)左右菜單聯(lián)動(dòng)實(shí)現(xiàn)代碼
- vue實(shí)現(xiàn)列表左右聯(lián)動(dòng)效果
相關(guān)文章
element?el-upload文件上傳覆蓋第一個(gè)文件的實(shí)現(xiàn)
這篇文章主要介紹了element?el-upload文件上傳覆蓋第一個(gè)文件的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03vue單應(yīng)用在ios系統(tǒng)中實(shí)現(xiàn)微信分享功能操作
這篇文章主要介紹了vue單應(yīng)用在ios系統(tǒng)中實(shí)現(xiàn)微信分享功能操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09Vue3中進(jìn)行二維碼的生成與解碼實(shí)現(xiàn)詳解
這篇文章主要為大家介紹了Vue3中進(jìn)行二維碼的生成與解碼實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03vue+canvas實(shí)現(xiàn)數(shù)據(jù)實(shí)時(shí)從上到下刷新瀑布圖效果(類(lèi)似QT的)
這篇文章主要介紹了vue+canvas實(shí)現(xiàn)數(shù)據(jù)實(shí)時(shí)從上到下刷新瀑布圖效果(類(lèi)似QT的),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04優(yōu)化Vue項(xiàng)目編譯文件大小的方法步驟
這篇文章主要介紹了優(yōu)化Vue項(xiàng)目編譯文件大小的方法步驟,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-05-05Vue如何實(shí)現(xiàn)變量表達(dá)式選擇器
這篇文章主要介紹了Vue如何實(shí)現(xiàn)變量表達(dá)式選擇器,幫助大家更好的理解和學(xué)習(xí)使用vue框架,感興趣的朋友可以了解下2021-02-02