Vue项目解决better-scroll无法滚动

1. better-scroll版本不合适

npm uninstall better-scroll
npm install better-scroll@1.8.1 --save

2. 未添加对应属性值(mouseWheel)

<script>
    import BScroll from 'better-scroll'

    export default {
        name: "Scoll",
        methods: {
            initScroll() {
                this.scroll = new BScroll(this.$refs.wrapper, {click: true, probeType: 3, mouseWheel: true});
            },
        },
        mounted() {
            this.initScroll();
        },
    }
</script>

3. 层级关系

被scroll包裹的组件中只能由一个div层级的内容, 否则用div包裹起来

<scroll :top="150" :new-song-list="newSongList">
  <div class="scroll-wrapper">
    <div>
      <!--  my-swiper  -->
      <my-swiper style="background-color: #F2F6FC;" :swiper-list="swiperList"></my-swiper>
      <!--  hot-song-list -->
      <new-song-list :new-song-list="newSongList"></new-song-list>
    </div>
  </div>
</scroll>

4. 示例

scroll样式

// recommend.vue
<template>
  <div class="recommend">
    <scroll :top="150" :new-song-list="newSongList">
      <div class="scroll-wrapper">
        <div>
          <!--  my-swiper  -->
          <my-swiper style="background-color: #F2F6FC;" :swiper-list="swiperList"></my-swiper>
          <!--  hot-song-list -->
          <new-song-list :new-song-list="newSongList"></new-song-list>
        </div>
      </div>
    </scroll>
  </div>
</template>
// MyScroll.vue
<template>
    <div class="wrapper" ref="wrapper">
        <slot></slot>
    </div>
</template>

<script>
    import BScroll from 'better-scroll'

    export default {
        name: "Scoll",
        props: {
            top: {
                type: Number,
                default: 0,
            },
            newSongList: {
                type: Array,
                required: true
            },
        },
        methods: {
            initScroll() {
                this.$refs.wrapper.style.top = this.top + "px";
                this.scroll = new BScroll(this.$refs.wrapper, {click: true, probeType: 3, mouseWheel: true});
            },
            refresh() {
                this.scroll && this.scroll.refresh();
            }
        },
        mounted() {
            this.initScroll();
        },
        watch: {
            newSongList() {
                this.$nextTick(() => {
                    this.refresh();
                })
            }
        }
    }
</script>

<style scoped>
  .wrapper {
    position: absolute;
    overflow: hidden;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
  }
</style>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值