vue中使用better-scroll实现滑动

下载 better-scroll

npm install better-scroll --save

注意:

  • 必须包含两个大的div,外层和内层div
  • 外层div设置可视的大小(宽或者高)-有限制宽或高
  • 内层div,包裹整个可以滚动的部分
  • 内层div高度一定大于外层div的宽或高,才能滚动
  • 必须在获取数据和DOM渲染完之后再调用定义的 initScroll()方法,否则无法滚动
在组件script标签中引入
<script>
import BScroll from 'better-scroll'
export default {
  data() {
	return {
	  goods: []
	}
  }
}
</script>

一、 垂直滚动

在组件模板template中布局
<template>
  <div class="wrapper" ref="wrapper">
    <ul>
      <li v-for="item in goods" class="item">
        <span class="text">
          {{item.name}}
        </span>
      </li>
    </ul>
  </div>
</template>
在组件钩子函数methods中定义方法
<script>
export default {
  data() {
	return {
	  goods: [],
	  Scroll: null
	}
  },
  methods: {
  	initScroll() {
      this.$nextTick(()=>{
        if(!this.Scroll) {
          this.Scroll = new BScroll(this.$refs.wrapper,{
            click: true,      // 配置允许点击事件
            scrollY: true     // 开启纵向滚动
          })
        } else {
          this.Scroll.refresh()    // 重新计算 better-scroll,当 DOM 结构发生变化的时确保滚动效果正常
        }
      })
    }
  },
  mounted() {
    this.initScroll()
  }
}
</script>
在组件的CSS中(stylus 语法)
<style lang="stylus" scoped>
.wrapper
  width 100%
  position absolute
  top 0
  bottom 0
  overflow hidden
</style>

二、 水平滚动

在组件模板template中布局
<template>
  <div class="wrapper" ref="wrapper">
    <ul ref="content">
      <li v-for="item in goods" class="item">
        <span class="text">
          {{item.name}}
        </span>
      </li>
    </ul>
  </div>
</template>
在组件钩子函数methods中定义方法(略有不同)
<script>
export default {
  data() {
	return {
	  goods: [],
	  Scroll: null
	}
  },
  methods: {
  	initScroll() {
  	  // 给内层盒子设置宽度,不设置宽度的话无法滚动
  	  let width = this.goods.length * 60
  	  // 如果有外边距,可以这样写。需要去掉最后一个元素的外边距,在后面减一下
  	  // let width = this.goodslength * (60 + 10) - 10
      this.$refs.content.style.width = width + 'px'
      this.$nextTick(()=>{
        if(!this.Scroll) {
          this.Scroll = new BScroll(this.$refs.wrapper,{
            click: true,      // 配置允许点击事件
            scrollX: true,    // 开启横向滚动
            eventPassthrough: 'vertical'  // 当设置 eventPassthrough 为 'vertical' 的时候,scrollY 无效
          })
        } else {
          this.Scroll.refresh()     // 重新计算 better-scroll,当 DOM 结构发生变化的时确保滚动效果正常
        }
      })
    }
  },
  mounted() {
    this.initScroll()
  }
}
</script>
在组件的CSS中(stylus 语法)
<style lang="stylus" scoped>
.wrapper
  width 100%
  overflow hidden
  .content
  	white-space nowrap
  	.item
  	  float left
      width 60px
</style>
  • 14
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
使用better-scroll实现下拉刷新和上拉加载功能,需要先安装better-scroll插件。 1. 安装better-scroll ```bash npm install better-scroll --save ``` 2. 导入better-scroll ```javascript import BScroll from 'better-scroll' ``` 3. 创建better-scroll实例 ```javascript mounted() { this.initScroll() }, methods: { initScroll() { this.scroll = new BScroll(this.$refs.wrapper, { probeType: 3, // 实时派发scroll事件 click: true, // 开启点击事件 pullDownRefresh: true, // 开启下拉刷新 pullUpLoad: true // 开启上拉加载 }) } }, ``` 4. 监听下拉刷新和上拉加载事件 ```javascript this.scroll.on('pullingDown', () => { // 下拉刷新业务逻辑 this.scroll.finishPullDown() // 完成下拉刷新 this.scroll.refresh() // 重新计算滚动区域 }) this.scroll.on('pullingUp', () => { // 上拉加载业务逻辑 this.scroll.finishPullUp() // 完成上拉加载 this.scroll.refresh() // 重新计算滚动区域 }) ``` 5. 在模板添加DOM元素 ```html <div class="wrapper" ref="wrapper"> <div class="content"> <!-- 内容 --> </div> </div> ``` 6. 完整代码示例 ```html <template> <div class="scroll-wrapper"> <div class="wrapper" ref="wrapper"> <div class="content"> <ul> <li v-for="item in list" :key="item.id">{{ item.title }}</li> </ul> </div> <div class="pullup-wrapper"> <div class="pullup"> <span v-show="!isPullUpLoad">上拉加载更多</span> <span v-show="isPullUpLoad">正在加载...</span> </div> </div> </div> </div> </template> <script> import BScroll from 'better-scroll' export default { data() { return { list: [], // 列表数据 isPullUpLoad: false // 是否正在上拉加载 } }, mounted() { this.initScroll() }, methods: { initScroll() { this.scroll = new BScroll(this.$refs.wrapper, { probeType: 3, // 实时派发scroll事件 click: true, // 开启点击事件 pullDownRefresh: true, // 开启下拉刷新 pullUpLoad: true // 开启上拉加载 }) // 监听下拉刷新和上拉加载事件 this.scroll.on('pullingDown', () => { // 下拉刷新业务逻辑 this.scroll.finishPullDown() // 完成下拉刷新 this.scroll.refresh() // 重新计算滚动区域 }) this.scroll.on('pullingUp', () => { // 上拉加载业务逻辑 this.isPullUpLoad = true // 正在上拉加载 setTimeout(() => { this.isPullUpLoad = false // 完成上拉加载 this.scroll.finishPullUp() // 完成上拉加载 this.scroll.refresh() // 重新计算滚动区域 }, 1000) }) } } } </script> <style> .scroll-wrapper { height: 100%; overflow: hidden; } .wrapper { height: 100%; overflow: auto; } .pullup-wrapper { text-align: center; margin-top: 10px; margin-bottom: 10px; } .pullup { height: 40px; line-height: 40px; } </style> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

半度℃温热

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值