使用better-scroll插件进行左右联动布局效果

简介: 使用better-scroll插件进行左右联动布局效果

安装环境

npm install better-scroll -S

看一下大致思路

a3b8a1e3019340d9b2ff07bb00df7344.png

52134b0acf934e22beed6c4e4e42eeda.png

代码部分:

updated() {
    if (!this.scroll) {
      this.$nextTick(() => {
    this.scroll = new BetterScroll(this.$refs.right, {
      click: true,
      probeType: 3,
    // pullUpload:true上拉加载更多
    });
    // 滚动事件
    this.scroll.on("scroll", (pos) => {
      console.log(pos);
      this.scrollY = Math.abs(pos.y);
      console.log(this.scrollY);
    });
        this.leftClick();
        console.log(this.scroll);
      });
    } 
  },
methods:{
    leftClick() {
      let uls = this.$refs.right.getElementsByClassName("li");
      console.log(uls);
      let height = 0;
      this.allHeight.push(height);
      Array.from(uls).forEach((v) => {
        height += v.clientHeight;
        //  console.log(v.clientHeight);
        this.allHeight.push(height);
      });
    },
    onChange(index) {
      console.log(this.allHeight[index]);
      this.scroll.scrollTo(0, -this.allHeight[index], 300);
      // ul.scrollTop = this.allHeight[index] + 10;
    },
    }
computed: {
      current(){
        console.log(111);
        return this.allHeight.findIndex((item,index)=>{
          console.log(item,index);
          // 滚动距离 
          // 1500>= 1400 && 1500< 2100
          return this.scrollY >= item && this.scrollY <this.allHeight[index + 1]
        })
      }
    },

左侧按钮标签

<li
            v-for="(item, index) in list"
            @click="onChange(index)"
            :key="index"
            :class="{lis_one_color:index == current}"
            class="lis_one "
          >

左侧按钮的css样式

.lis_one{
  font-weight: bold;
  text-align: center;
  font-size: 18px;
  /* width: 100%; */
  height: 50px;
  line-height: 36px;
  margin: 10px 0;
  padding: 7px 0;
  box-sizing: border-box;
}
.lis_one_color{
    color: rgb(255,102,0) ;
    border-left:3px rgb(255,102,0) solid;
}

引入better-scroll

import BetterScroll from "better-scroll";

注意:需要用到better-scroll的属性有

click:true,
probeType:3

c1eedf937c454ff7adc3ca141cb1c148.png

使用nextTick进行延迟回调

nextTick

定义:在下次 DOM 更新循环结束之后执行延迟回调。在修改数据之后立即使用这个方法,获取更新后的 DOM。

updated() {
    if (!this.scroll) {
      this.$nextTick(() => {
    this.scroll = new BetterScroll(this.$refs.right, {
      click: true,
      probeType: 3,
    });
    } 
  },

a4a6e7d1e33c40ee9e65cff537e36a8d.png

因为每次获取到的y轴为负值,所以要通过abs转换成正数

this.scroll.on("scroll", (pos) => {
      console.log(pos);
      this.scrollY = Math.abs(pos.y);
      console.log(this.scrollY)
    });

b96d289fd63a4bcdb8c1fcb8cada154f.png

  computed: {
      current(){
        return this.allHeight.findIndex((item,index)=>{
          console.log(item,index);
        })
      }
    },

5e3eb4a1708d461e8e2c63c55b72f52e.png

思路大致就是这样,如有不懂的地方或者其他更好的写法我们可以进一步探讨一下

效果如下:

f7c5eb678f624fe7a031360f824855c1.gif

相关文章
|
4月前
|
Web App开发 小程序 Android开发
uniapp TC-scroll-view 横向和纵向滚动 带滚动回调
uniapp TC-scroll-view 横向和纵向滚动 带滚动回调
43 0
|
JavaScript 前端开发
vue实现公告文字横向滚动
vue实现公告文字横向滚动
1619 0
|
2月前
|
小程序
微信小程序scroll-view横向滚动和纵向滚动实现(亲测管用)
微信小程序scroll-view横向滚动和纵向滚动实现(亲测管用)
|
4月前
|
Web App开发 小程序 Android开发
Uniapp swiper 滚动图组件
Uniapp swiper 滚动图组件
29 0
|
7月前
|
JavaScript
vue里使用animated-scroll-to代替原生滚动实现兼容ie的平滑滚动定位
vue里使用animated-scroll-to代替原生滚动实现兼容ie的平滑滚动定位
47 0
|
11月前
|
小程序 安全 前端开发
微信小程序自定义导航栏机型适配--底部Tabbar--view高度--底部按钮适配
微信小程序自定义导航栏机型适配--底部Tabbar--view高度--底部按钮适配
603 0
Flutter利用ScrollController获取、控制滚动组件的滚动位置
Flutter利用ScrollController获取、控制滚动组件的滚动位置
|
JavaScript 小程序 前端开发
uni-app/微信小程序:scroll-view纵向滚动高度自适应flex布局填充剩余高度
uni-app/微信小程序:scroll-view纵向滚动高度自适应flex布局填充剩余高度
733 0
uniapp实现横向滚动样式条
本文讲解,uniapp如何实现横向滚动样式条
1028 0
uniapp实现横向滚动样式条