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

vue左右側(cè)聯(lián)動滾動的實現(xiàn)代碼

 更新時間:2018年06月06日 09:09:45   作者:豌豆突突突  
這篇文章主要介紹了vue左右側(cè)聯(lián)動滾動的實現(xiàn)代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

本文介紹了vue左右側(cè)聯(lián)動滾動的實現(xiàn)代碼,分享給大家,具體如下:

實現(xiàn)功能:

  1. 點擊左側(cè),右側(cè)滾動到相應(yīng)位置,
  2. 滾動右側(cè), 左側(cè)滾動到相應(yīng)位置

布局結(jié)構(gòu):

開源滾動庫:

better-scroll.js

技術(shù)要點:

1.<scroll>是對緊鄰的元素生效

如:

<scroll class='foods-wrapper'>
  <ul class=content>
   <li></li>
  </ul>
 </scroll>

初始化在<ul>元素上

2.foods-wrapper的高度小于content高度時才會發(fā)生滾動

3.點擊左側(cè)菜單列表時,只需要計算右側(cè)對應(yīng)的偏移距離 或是 計算對應(yīng)的移動到的元素即可

方法一: 計算移動距離, 用scrollTo()方法

 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.滾動右側(cè)列表時,會稍復雜一些.

4.1. 因為需要知道滾動的元素在哪個item列表區(qū)間, 因此需要計算右側(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 時時監(jiān)聽滾動距離

需要在<scroll>中加以下參數(shù)

復制代碼 代碼如下:
<scroll class='foods-wrapper' :listenScroll=listenScroll :probeType = 'probeType' @scroll=scroll>

其中 listenScroll probeType參數(shù) 在created中定義:

 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) //參數(shù)position: position:{x:-10, y:24}
  })
 }

posiiton.y就是需要實時監(jiān)聽的參數(shù),即:

scroll (position) {
 this.scrolly = position.y
}

其中 scrolly 需要在data中提前定義:

 data () {
  return {
   scrolly: -1
  }
 }

然后在watch中監(jiān)聽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 滾動的時候會派發(fā)scroll事件,會截流。 * 2 滾動的時候?qū)崟r派發(fā)scroll事件,不會截流。 * 3 除了實時派發(fā)scroll事件,在swipe的情況下仍然能實時派發(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>

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • vue實現(xiàn)手機號碼抽獎上下滾動動畫示例

    vue實現(xiàn)手機號碼抽獎上下滾動動畫示例

    本篇文章主要介紹了vue實現(xiàn)手機號碼抽獎上下滾動動畫示例。具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • vue中 router.beforeEach() 的用法示例代碼

    vue中 router.beforeEach() 的用法示例代碼

    導航守衛(wèi)主要是通過跳轉(zhuǎn)或取消的方式守衛(wèi)導航,本文通過示例代碼講解vue中 router.beforeEach() 的用法,感興趣的朋友跟隨小編一起看看吧
    2023-12-12
  • 項目部署后前端vue代理失效問題解決辦法

    項目部署后前端vue代理失效問題解決辦法

    這篇文章主要給大家介紹了關(guān)于項目部署后前端vue代理失效問題的解決辦法,文中通過圖文以及代碼示例將解決的辦法介紹的非常詳細,對大家的學習或者工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2024-02-02
  • 淺析vue-cli3配置webpack-bundle-analyzer插件【推薦】

    淺析vue-cli3配置webpack-bundle-analyzer插件【推薦】

    小編最近為了優(yōu)化vue項目性能,需要使用webpack-bundle-analyzer插件來分析報文件,在網(wǎng)上沒有找到合適的,下面小編給大家寫出來一個供大家參考
    2019-10-10
  • vue 點擊展開顯示更多(點擊收起部分隱藏)

    vue 點擊展開顯示更多(點擊收起部分隱藏)

    這篇文章主要介紹了vue 點擊展開顯示更多(點擊收起部分隱藏),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-04-04
  • this在vue和小程序中的使用詳解

    this在vue和小程序中的使用詳解

    這篇文章主要介紹了this在vue和小程序中的使用詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-01-01
  • vue小白入門教程

    vue小白入門教程

    vue是一套用于構(gòu)建用戶界面的漸進式框架,本文通過實例給大家介紹了vue入門教程適用小白初學者,非常不錯,具有參考借鑒價值,需要的朋友參考下吧
    2018-04-04
  • Vue中使用vue-i18插件實現(xiàn)多語言切換功能

    Vue中使用vue-i18插件實現(xiàn)多語言切換功能

    在基于vue-cli項目開發(fā)過程中,多語言切換功能可使用vue-i18插件,這篇文章分步驟給大家介紹了Vue中使用vue-i18插件實現(xiàn)多語言切換功能,感興趣的朋友一起看看吧
    2018-04-04
  • Vue中Element?UI組件庫使用方法詳解

    Vue中Element?UI組件庫使用方法詳解

    ElementUI是Vue的UI框架,提供了豐富的組件,方便快速開發(fā)頁面,本文詳細介紹了ElementUI的安裝、使用方法以及常見組件的說明,包括基礎(chǔ)組件、布局組件、選擇框組件、輸入框組件、下拉框組件等,需要的朋友可以參考下
    2024-11-11
  • vue使用vuedraggable實現(xiàn)嵌套多層拖拽排序功能

    vue使用vuedraggable實現(xiàn)嵌套多層拖拽排序功能

    這篇文章主要為大家詳細介紹了vue使用vuedraggable實現(xiàn)嵌套多層拖拽排序功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04

最新評論

永善县| 宜章县| 巴塘县| 陆丰市| 东海县| 新源县| 峡江县| 久治县| 望江县| 平山县| 获嘉县| 临城县| 汶上县| 安龙县| 沧州市| 衡东县| SHOW| 长丰县| 阿图什市| 武功县| 三门峡市| 新宾| 宁都县| 津市市| 静海县| 湖北省| 宽城| 鹤山市| 红河县| 东城区| 吉水县| 漯河市| 曲靖市| 双桥区| 甘孜县| 化州市| 来凤县| 郸城县| 枞阳县| 河津市| 舟曲县|