vue中實(shí)現(xiàn)左右聯(lián)動(dòng)的效果
這里的坑還是蠻多的,花了一個(gè)多小時(shí),才理清楚。
做一下筆記,以便于復(fù)習(xí)。
首先呢,需要讓左右的布局都可以滾動(dòng),這里使用了betterScroll
npm i better-scroll import BScroll from 'better-scroll' methods: { _initScroll () { this.menuScroll = new BScroll((this.$refs.menu), { click: true 因?yàn)閎etterscroll默認(rèn)會(huì)阻止點(diǎn)擊事件。這里要設(shè)置一下 }) this.foodScroll = new BScroll((this.$refs.good), { probeType: 3 用來獲取滾動(dòng)的距離 }) this.foodScroll.on('scroll', (pos) => { this.scrollY = Math.abs(Math.round(pos.y)) }) },
重點(diǎn):
created () { this.$ajax.get('/api/data.json').then((res) => { this.goods = res.data.goods this.$nextTick(() => { this._initScroll() this.getGoodsHeight() }) }) },
這里的代碼一定要在獲取數(shù)據(jù)里面寫nextTick()回調(diào)里面寫代碼,因?yàn)樾枰却龜?shù)據(jù)萬泉加載再去初始化scroll和獲取右邊每一個(gè)盒子的高度。
說道高度,高度如何獲取呢?
getGoodsHeight () { let goodEle = this.$refs.good let height = 0 this.listHeight.push(height) let foodList = goodEle.getElementsByClassName('goodsItemHook') for (let i = 0; i < foodList.length; i++) { let item = foodList[i] height += item.clientHeight this.listHeight.push(height) } },
這里是獲取到每一個(gè)盒子的clientHeight的高度進(jìn)行疊加,在push到一個(gè)數(shù)組里面。
this.foodScroll.on('scroll', (pos) => { this.scrollY = Math.abs(Math.round(pos.y)) })
獲取滾動(dòng)的值,賦值給scrollY。
然后根據(jù)scrollY 和listHeight的高度區(qū)間做對比,拿到index:
currentIndex () { for (let i = 0; i < this.listHeight.length; i++) { let height1 = this.listHeight[i] let height2 = this.listHeight[i + 1] if (!height2 || (this.scrollY < height2 && this.scrollY >= height1)) { return i } } }
這時(shí)候滾動(dòng)就能獲取index的值了,然后給左邊的元素去添加active的樣式就方便了。
:class="{'active': currentIndex == index}"
最后一步是如何實(shí)現(xiàn)點(diǎn)擊的時(shí)候去讓右邊的滾動(dòng)到指定的位置。
handleGoodsItem (index) { let goodEle = this.$refs.good let foodList = goodEle.getElementsByClassName('goodsItemHook') let el = foodList[index] this.foodScroll.scrollToElement(el, 300) }
這里調(diào)用了scroll的方法:scrollToElement。
總結(jié)
以上所述是小編給大家介紹的vue中實(shí)現(xiàn)左右聯(lián)動(dòng)的效果,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- vue左右側(cè)聯(lián)動(dòng)滾動(dòng)的實(shí)現(xiàn)代碼
- Vue2仿淘寶實(shí)現(xiàn)省市區(qū)三級聯(lián)動(dòng)
- vue.js模仿京東省市區(qū)三級聯(lián)動(dòng)的選擇組件實(shí)例代碼
- vue基于mint-ui的城市選擇3級聯(lián)動(dòng)的示例
- vue mint-ui 實(shí)現(xiàn)省市區(qū)街道4級聯(lián)動(dòng)示例(仿淘寶京東收貨地址4級聯(lián)動(dòng))
- Vue中的ref作用詳解(實(shí)現(xiàn)DOM的聯(lián)動(dòng)操作)
- vue省市區(qū)三聯(lián)動(dòng)下拉選擇組件的實(shí)現(xiàn)
- 使用vue.js實(shí)現(xiàn)聯(lián)動(dòng)效果的示例代碼
相關(guān)文章
vue3接口數(shù)據(jù)賦值對象,渲染報(bào)錯(cuò)問題及解決
這篇文章主要介紹了vue3接口數(shù)據(jù)賦值對象,渲染報(bào)錯(cuò)問題及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09基于vue2.0的活動(dòng)倒計(jì)時(shí)組件countdown(附源碼下載)
這是一款基于vue2.0的活動(dòng)倒計(jì)時(shí)組件,可以使用服務(wù)端時(shí)間作為當(dāng)前時(shí)間,在倒計(jì)時(shí)開始和結(jié)束時(shí)可以自定義回調(diào)函數(shù)。這篇文章主要介紹了基于vue2.0的活動(dòng)倒計(jì)時(shí)組件countdown,需要的朋友可以參考下2018-10-10Vue利用廣度優(yōu)先搜索實(shí)現(xiàn)watch
這篇文章主要為大家學(xué)習(xí)介紹了Vue如何利用廣度優(yōu)先搜索實(shí)現(xiàn)watch(有意思),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-08-08Element-ui el-tree新增和刪除節(jié)點(diǎn)后如何刷新tree的實(shí)例
這篇文章主要介紹了Element-ui el-tree新增和刪除節(jié)點(diǎn)后如何刷新tree的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08