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

Vue項目中使用better-scroll實現(xiàn)菜單映射功能方法

 更新時間:2019年09月11日 08:22:35   作者:素素  
這篇文章主要介紹了Vue項目中使用better-scroll實現(xiàn)菜單映射功能,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

組件全部代碼

<template>

<div class="goods">
  <!--左側區(qū)域-->
  <div class="menu-wrapper" ref="left">
    <ul class="menu">
      <li class="menu-item border-bottom"
        :class="{'current':currentIndex===index}"
        v-for="(item,index) in dataLlist.goods"
        :key="index"
        @click="selectIndex(index,$event)"
        ref="menuList"
      >
        <div class="text">
          <goods-icon v-show="item.type>0" :index="item.type" class="text-ico"></goods-icon>
          {{item.name}}
        </div>
      </li>
    </ul>
  </div>
  <!--右側區(qū)域-->
  <div class="foods-wrapper" ref="right">
    <ul>
      <li class="food-list food-list-hook"
        v-for="(item,index) in dataLlist.goods"
        :key="index">
        <!--標題區(qū)域-->
        <h1 class="border-left">{{item.name}}</h1>
        <ul>
          <li class="food-item border-bottom"
            v-for="(foodItem,index) in item.foods"
          >
            <div class="food-image">
              <img :src="foodItem.icon" alt="foodItem.name">
            </div>
            <div class="food-desc">
              <div class="title">{{foodItem.name}}</div>
              <div class="desc">{{foodItem.description}}</div>
              <div class="num">
                <div class="sellCount">月售{{foodItem.sellCount}}份</div>
                <div class="rating">好評率{{foodItem.rating}}%</div>
              </div>
              <div class="price">
                <div class="new-price">¥{{foodItem.price}}</div>
                <div class="old-price border-bottom" v-show="foodItem.oldPrice">¥{{foodItem.oldPrice}}</div>
              </div>
            </div>

          </li>
        </ul>
      </li>
    </ul>
  </div>
</div>
</template>

<script>

import Icon from '../../common/iconType/Icon';
import BScroll from 'better-scroll'

export default {
  name: "Goods",
  props:['dataLlist'],
  data(){
    return{
      listHeight:[],
      scrollY:0 ,//為了實現(xiàn)左右區(qū)域映射
    }
  },
  computed:{
    currentIndex(){ //這個返回的是下標,當這個currentIndex的值與goods的下標一致的時候,
      // 左側區(qū)域就會呈現(xiàn)高亮現(xiàn)象
      for (let i = 0; i < this.listHeight.length; i++) {
        let height1 = this.listHeight[i];
        let height2 = this.listHeight[i + 1];
        if (!height2 || (this.scrollY >= height1 && this.scrollY < height2)) {
          this._followScroll(i) //實現(xiàn)當滑動的時候,左側隱藏的食物類型顯示
          return i;
        }
      }
      return 0;
    }
  },
  created(){
    //dataLlist數(shù)據是異步加載,直接用new BScroll時,dom可能還沒有更新
    this.$nextTick(()=>{ //this.$nextTick()將回調延遲到下次 DOM 更新循環(huán)之后執(zhí)行,使用$nextTick異步初始化Bscroll
      this.meunScroll=new BScroll(this.$refs.left,{
        click:true
      });
      this.foodScroll=new BScroll(this.$refs.right,{
        probeType: 3 //可以派發(fā)scroll事件,檢測到實時滾動的位置
      });
      this.foodScroll.on('scroll',(pos) =>{
        //參數(shù)pos就是在右側區(qū)域滑動的實時位置
        //Math.round()取整數(shù),Math.abs取絕對值
        this.scrollY =Math.abs( Math.round(pos.y));
      });
      this._calculateHeight(); //這個方法為了獲取每個商品類的最大區(qū)間的高度
    })
  },
  methods:{
    _followScroll(index) {
      if(index > 0 ){
        let menuList = this.$refs.menuList;
        let el = menuList[index];
        this.meunScroll.scrollToElement(el, 300, 0, -100);//better-scroll的scrollToElement方法滾動到指定位置
      }
      },
    _calculateHeight(){ //這個方法為了獲取每個商品類的最大區(qū)間的高度
     let height = 0;
     let foodLsit = this.$refs.right.getElementsByClassName('food-list-hook');
     this.listHeight.push(height); //listHeight這個數(shù)組是用來存放右側商品中每個類型商品的最大區(qū)間高度的集合
     for(var i=0;i<foodLsit.length;i++){
       let item = foodLsit[i];
       //clientHeight代表元素的高度
       height += item.clientHeight; //每個元素的高度等于自身高度加上上一個元素的高度
       this.listHeight.push(height); //最終listHeight集合了所有l(wèi)i[類為food-list-hook]到最頂部的高度
     }
    },
    selectIndex(index,ele){
      //better-scroll 會禁止移動端的點擊事件,需要重新派發(fā),同時在PC端會點擊兩次,此處需要做判斷
      if(!ele._constructed){
        //better-scroll的派發(fā)事件scroll的event和pc端瀏覽器的點擊事件的event有個
        // 屬性區(qū)別_constructed,pc端瀏覽器的點擊事件的event中是沒有這個屬性的
        return;
      }
      let rightItem =this.$refs.right.getElementsByClassName('food-list-hook');
      let item = rightItem[index]; //找到相應的li
      this.foodScroll.scrollToElement(item, 250) //better-scroll的scrollToElement方法滾動到指定位置
    }
   // scrollToElement(el, time, offsetX, offsetY, easing) //第一個值接收目標元素,第二個是滾動時間,第三第四個是相對于目標元素的偏移量。
},

  components:{
    'goods-icon': Icon
  }

}
</script>

<style scoped lang="stylus">

@import "../../assets/stylus/mixin.styl"
.goods

position absolute
top 3.6rem
bottom .92rem
display flex
width: 100%
overflow: hidden
.menu-wrapper
  flex 0 0 1.6rem
  width 1.6rem
  background-color #f3f5f7
  .menu-item
    height 1.08rem
    display flex
    align-items center
    justify-content left
    &.border-bottom::before
       color rgba(7,17,27,.1)
    .text
      font-weight 200
      font-size .24rem
      line-height .28rem
      margin 0 .24rem
      .text-ico
         margin-right -.08rem
         vertical-align top;
    &.current
      font-size .24rem
      line-height .28rem
      color rgb(240,20,20)
      background-color #ffffff
.foods-wrapper
  flex 1
  .food-list
    h1
      width 100%
      height .52rem
      line-height .52rem
      padding-left .28rem
      background-color #f3f5f7
      font-size .24rem
      color rgb(147,153,159)
      &.border-left::before
        border-color #d9dde1
        border-width .1rem


    .food-item
      display flex
      padding .36rem
      &:last-child.border-bottom
        border none
      .food-desc
        margin-left .2rem
        font-size .2rem
        color rgb(147,153,159)
        .title
          font-size:.28rem
          color rgb(7,17,27)
          margin-top .04rem
          line-height .28rem
        .desc
          margin .15rem auto
          line-height:.28rem
        .num
          display flex
          margin 0 0 .16rem 0
          .sellCount
             margin-right .24rem

        .price
          display flex
          align-items center
          .new-price
            color rgb(220,20,60)
            font-weight 700
            line-height .48rem
            margin-right .16rem
            font-size .28rem

          .old-price
            &.border-bottom::before
              position absolute
              top: 25%;
              border-width: 0.08rem;

</style>

Vue項目中使用better-scroll實現(xiàn)菜單滑動功能

安裝和在組件中引入better-scroll

npm install better-scroll --save

引入import BScroll from 'better-scroll' 【在組件中引入,在后續(xù)的export default中就可以直接使用封裝好的better-scroll功能了】

better-scroll實現(xiàn)的下面功能

在菜單中要實現(xiàn)點擊左側菜單的食品類型名稱,右側就會自動滑動到此食品類型下的所有食品;在右側區(qū)域中滑動到食品類型下的所有食品區(qū)域下的時候,左側菜單會出現(xiàn)相應的高亮效果

如何實現(xiàn)上面的功能:

第一:需要知道要在哪些區(qū)域間實現(xiàn)滑動

第二:通過new BScroll()獲取要實現(xiàn)滑動的區(qū)域

this.meunScroll=new BScroll(this.$refs.left);
this.foodScroll=new BScroll(this.$refs.right);

第三:上面代碼在理論上應該在相應的區(qū)域都應該能滑動了,但是現(xiàn)實是并不能滑動

原因是:數(shù)據的獲取是異步獲取的,在定義滑動區(qū)域的時候,也許數(shù)據還沒有更新,這是this.meunScroll的高度可能就沒有高度外部類goods的高度,這樣就不會滑動。

解決的方法:this.$nextTick()將回調延遲到下次 DOM 更新循環(huán)之后執(zhí)行,使用$nextTick異步初始化Bscroll

 this.$nextTick(()=>{ //this.$nextTick()將回調延遲到下次 DOM 更新循環(huán)之后執(zhí)行,使用$nextTick異步初始化Bscroll
      this.meunScroll=new BScroll(this.$refs.left,{
        click:true //左側菜單可以進行點擊事件
      });
      this.foodScroll=new BScroll(this.$refs.right,{
        probeType: 3 //可以派發(fā)scroll事件,檢測到實時滾動的位置
      });
      【this.foodScroll中必須有 probeType: 3后才能進行下面的scroll事件】
      this.foodScroll.on('scroll',(pos) =>{
        //參數(shù)pos就是在右側區(qū)域滑動的實時位置
        //Math.round()取整數(shù),Math.abs取絕對值
        this.scrollY =Math.abs( Math.round(pos.y));
      });
      this._calculateHeight(); //這個方法為了獲取每個商品類的最大區(qū)間的高度
    })

獲取每個右側區(qū)域的 <li class="food-list food-list-hook">的高度

在data中定義一個空listHeight數(shù)組;數(shù)組中的元素代表了每個li到this.foodScroll最頂部的區(qū)域高度;

_calculateHeight(){ //這個方法為了獲取每個商品類的最大區(qū)間的高度

     let height = 0;
     let foodLsit = this.$refs.right.getElementsByClassName('food-list-hook');
     this.listHeight.push(height); //listHeight這個數(shù)組是用來存放右側商品中每個類型商品的最大區(qū)間高度的集合
     for(var i=0;i<foodLsit.length;i++){
       let item = foodLsit[i];
       //clientHeight代表元素的高度
       height += item.clientHeight; //每個元素的高度等于自身高度加上上一個元素的高度
       this.listHeight.push(height); //最終listHeight集合了所有l(wèi)i[類為food-list-hook]到最頂部的高度
     }
    },
let foodLsit = this.$refs.right.getElementsByClassName('food-list-hook');

foodLsit表示所有l(wèi)i【 <li class="food-list food-list-hook">】dom集合;

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

相關文章

  • vue3:vue2中protoType更改為config.globalProperties問題

    vue3:vue2中protoType更改為config.globalProperties問題

    這篇文章主要介紹了vue3:vue2中protoType更改為config.globalProperties問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • vue cli使用融云實現(xiàn)聊天功能的實例代碼

    vue cli使用融云實現(xiàn)聊天功能的實例代碼

    最近小編接了一個新項目,項目需求要實現(xiàn)一個聊天功能,今天小編通過實例代碼給大家介紹vue cli使用融云實現(xiàn)聊天功能的方法,感興趣的朋友跟隨小編一起看看吧
    2019-04-04
  • Vue?package-lock.json的作用及說明

    Vue?package-lock.json的作用及說明

    這篇文章主要介紹了Vue?package-lock.json的作用及說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • Vue封裝全局toast組件的完整實例

    Vue封裝全局toast組件的完整實例

    組件(Component)是 Vue.js 最強大的功能之一,組件可以擴展 HTML 元素,封裝可重用的代碼,這篇文章主要給大家介紹了關于Vue封裝全局toast組件,需要的朋友可以參考下
    2021-07-07
  • vue3使用vuedraggable和grid實現(xiàn)自定義拖拽布局方式

    vue3使用vuedraggable和grid實現(xiàn)自定義拖拽布局方式

    這篇文章主要介紹了vue3使用vuedraggable和grid實現(xiàn)自定義拖拽布局方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-06-06
  • VUE v-for循環(huán)中每個item節(jié)點動態(tài)綁定不同函數(shù)的實例

    VUE v-for循環(huán)中每個item節(jié)點動態(tài)綁定不同函數(shù)的實例

    今天小編就為大家分享一篇VUE v-for循環(huán)中每個item節(jié)點動態(tài)綁定不同函數(shù)的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-09-09
  • 前端自動化測試Vue中TDD和單元測試示例詳解

    前端自動化測試Vue中TDD和單元測試示例詳解

    這篇文章主要為大家介紹了前端自動化測試Vue中TDD和單元測試示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-02-02
  • vue3封裝request請求的完整案例

    vue3封裝request請求的完整案例

    本文提供了如何將Vue3的靜態(tài)頁面集成到基于Vue2的若依項目中,并確保能夠訪問Vue2的接口,文中通過代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2024-09-09
  • vue開發(fā)環(huán)境配置跨域的方法步驟

    vue開發(fā)環(huán)境配置跨域的方法步驟

    本文介紹了使用vue-cli搭建的項目在開發(fā)時配置跨域,上線后不做任何任何修改,接口也可以訪問,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-01-01
  • vue項目實現(xiàn)面包屑導航

    vue項目實現(xiàn)面包屑導航

    這篇文章主要為大家詳細介紹了vue項目實現(xiàn)面包屑導航,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04

最新評論

金秀| 伊通| 哈尔滨市| 仙居县| 巍山| 嵊泗县| 万全县| 龙胜| 八宿县| 嘉定区| 东源县| 阿拉善左旗| 黑龙江省| 兴义市| 淮北市| 湟中县| 乌拉特前旗| 霞浦县| 顺昌县| 金溪县| 玉树县| 沭阳县| 舒兰市| 晴隆县| 株洲市| 长沙县| 元朗区| 杭锦后旗| 贡觉县| 光山县| 桑植县| 且末县| 柏乡县| 大田县| 德保县| 宁德市| 百色市| 延安市| 无极县| 桃园市| 莒南县|