better-scroll使用

这篇博客介绍了如何在Vue项目中安装和使用Better-Scroll库来实现页面滚动效果。首先通过npm安装了better-scroll@1.13.2,并在组件中进行了封装。接着,在组件的mounted生命周期钩子中实例化BScroll对象,启用点击功能。最后,展示了在其他页面中如何引入并使用这个自定义的Scroll组件,结合DetailSwiper组件展示商品详情,实现了页面的上下滚动。
摘要由CSDN通过智能技术生成

1.安装npm install better-scroll@1.13.2 --save
2.封装组件scroll

<template>
<div class="wrapper" ref="wrapper">
  <div class="content">
    <slot></slot>
  </div>

</div>
</template>

<script>
import  BScroll from 'better-scroll'
export default {
  name: "Scroll",
  data(){
    return{
      scroll:null
    }
  },
  //可点击
  mounted() {
    this.scroll=new BScroll(this.$refs.wrapper,{
      click:true

    })
  }
}
</script>

<style scoped>

</style>

3.在其他页面中使用

<template>
  <div class="detail">
    <detail-nav-bar class="top-nav"></detail-nav-bar>
    <scroll class="content">
    <detail-swiper :top-img="topImg"></detail-swiper>
    </scroll>
  </div>
</template>

<script>
import DetailNavBar from "@/views/detail/childComps/DetailNavBar";
import {getDetail} from "@/network/detail";
import DetailSwiper from "@/views/detail/childComps/DetailSwiper";
import Scroll from "@/components/common/scroll/Scroll";

export default {
  name: "Detail",
  components: {Scroll, DetailSwiper, DetailNavBar},
  data(){
    return{
      iid: null,
      topImg: []
    }
  },
  created() {
    this.iid = this.$route.params.iid

    getDetail(this.iid).then(res=>{
      console.log(res)
      this.topImg=res.data.result.itemInfo.topImages
    })
  }

}
</script>

<style scoped>
.detail{
  position: relative;
  height: 100vh;
  z-index: 3;
  background-color: #ffffff;
}
.top-nav{
position: relative;
  z-index: 4;
  background-color: #ffffff;
}
.content{
  height: calc(100% - 44px);

}
</style>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值