最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue中實(shí)現(xiàn)左右聯(lián)動(dòng)的效果

 更新時(shí)間:2018年06月22日 11:47:12   作者:simoonQian  
這篇文章主要介紹了vue中實(shí)現(xiàn)左右聯(lián)動(dòng)的效果,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧

這里的坑還是蠻多的,花了一個(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)會阻止點(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)的效果,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評論

嘉义市| 南开区| 贺州市| 新蔡县| 迭部县| 咸丰县| 阳原县| 晋江市| 察雅县| 屏南县| 绥中县| 吴堡县| 灵丘县| 莱州市| 淳化县| 临桂县| 三原县| 荆门市| 银川市| 阳江市| 凌云县| 年辖:市辖区| 凉城县| 鹤山市| 云南省| 吴堡县| 莆田市| 闵行区| 武乡县| 延津县| 嫩江县| 台东县| 旬阳县| 克东县| 兴安盟| 济源市| 甘肃省| 淮阳县| 平陆县| 镇康县| 临安市|