vue-better-scroll 的使用實例代碼詳解
首先安裝better-scroll
npm i better-scroll -S
goods頁面模板
<template> <div class="goods"> <div class="menu-wrapper" ref="menuWrapper"> <ul> <li v-for="item in goods" class="menu-item"> <span class="text border-1px"> <span v-show="item.type>0" class="icon" :class="classMap[item.type]"></span>{{item.name}} </span> </li> </ul> </div> <div class="foods-wrapper" ref="foodsWrapper"> <ul> <li v-for="item in goods" > <h1 class="title">{{item.name}}</h1> <ul> <li v-for="food in item.foods" class="food-item border-1px"> <div class="icon"> <img :src="food.icon" alt="" width="57" height="57"> </div> <div class="content"> <h2 class="name">{{food.name}}</h2> <p class="desc">{{food.description}}</p> <div class="extra"> <span class="food-number">月售{{food.sellCount}}份</span> <span>好評率{{food.rating}}%</span> </div> <div class="price"> <span class="nowPrice">¥{{food.price}}</span> <span class="oldPrice">¥{{food.oldPrice}}</span> </div> </div> </li> </ul> </li> </ul> </div> </div> </template>
js
<script type="text/ecmascript-6"> /* eslint-disable*/ import BScroll from 'better-scroll' export default{ props:{ seller:{ type:Object } }, data(){ return{ goods:[] } }, created(){ this.classMap=['decrease', 'discount', 'special', 'invoice', 'guarantee'] this.$http.get('/api/goods').then((res)=>{ this.goods=res.data.data; this.$nextTick(()=>{ this._initScroll(); }) console.log(this.$refs.menuWrapper) }) }, methods:{ _initScroll(){ this.meunScroll=new BScroll(this.$refs.menuWrapper,{}); this.foodsScroll=new BScroll(this.$refs.foodsWrapper,{}); } } } </script>
先用ref 綁定事件, 在vue中 用$ .refs注冊
在鉤子函數(shù) create中 用vue-resource 請求數(shù)據(jù),并異步調(diào)用方法
this.$nextTick(()=>{ this._initScroll(); }
注冊方法
_initScroll(){ this.meunScroll=new BScroll(this.$refs.menuWrapper,{}); this.foodsScroll=new BScroll(this.$refs.foodsWrapper,{}); }
better-scroll用法
我們先來看一下 better-scroll 常見的 html 結(jié)構(gòu):
<div class="wrapper"> <ul class="content"> <li></li> <li></li> <li></li> <li></li> </ul> </div>
當(dāng) content 的高度不超過父容器的高度,是不能滾動的,而它一旦超過了父容器的高度,我們就可以滾動內(nèi)容區(qū)了,這就是 better-scroll 的滾動原理。
import BScroll from 'better-scroll' let wrapper = document.querySelector('.wrapper') let scroll = new BScroll(wrapper, {})
better-scroll 對外暴露了一個 BScroll 的類,我們初始化只需要 new 一個類的實例即可。第一個參數(shù)就是我們 wrapper 的 DOM 對象,第二個是一些配置參數(shù)。
better-scroll 的初始化時機很重要,因為它在初始化的時候,會計算父元素和子元素的高度和寬度,來決定是否可以縱向和橫向滾動。因此,我們在初始化它的時候,必須確保父元素和子元素的內(nèi)容已經(jīng)正確渲染了。如果沒有辦法滑動,那就是初始化的時機不對。
餓了么是這樣處理的:
mounted() { this.$nextTick(() => { this.scroll = new Bscroll(this.$refs.wrapper, {}) }) }
this.$nextTick()
這個方法作用是當(dāng)數(shù)據(jù)被修改后使用這個方法會回調(diào)獲取更新后的dom再render出來
如果不在下面的this.$nextTick()
方法里回調(diào)這個方法,數(shù)據(jù)改變后再來計算滾動軸就會出錯
上拉刷新功能
<div class="wrapper" ref="wrapper"> <ul class="content" ref="content"> <li v-for="(item,key) in detail" :key="key" v-if="detail.length > 0"> <Row type="flex" justify="start" align="middle"> <Col :span="8" class="detail-item"> <span :class="{'color-red':item.is_delay === 1}">{{item.order_sn}}</span> </Col> <Col :span="8" class="detail-item"> <span>{{item.date}}</span> </Col> <Col :span="8" class="detail-item"> <span>¥ {{item.partner_profit | number2}}</span> </Col> </Row> </li> <li v-if="!scrollFinish"> <Row type="flex" justify="center" align="middle"> <Col :span="6" v-if="loadingText"> <p>{{loadingText}}</p> </Col> <Col :span="2" v-else> <Spin size="large"></Spin> </Col> </Row> </li> </ul> </div> mounted() { // 設(shè)置wrapper的高度 this.$refs.wrapper.style.height = document.getElementById("app").offsetHeight - document.getElementById("scroll").offsetTop + "px"; // better-scroll 的content高度不大于wrapper高度就不能滾動,這里的問題是,當(dāng)一頁數(shù)據(jù)的高度不夠srapper的高度的時候,即使存在n頁也不能下拉 // 需要設(shè)置content的min-height大于wrapper高度 this.$refs.content.style.minHeight = this.$refs.wrapper.offsetHeight + 1 + "px"; this._initScroll(); this.getIncomeDetail(this.nextPage); // 設(shè)置scroll的高度 // this.scrollHeight = document.getElementById("app").offsetHeight - document.getElementById("scroll").offsetTop ; }, methods:{ _initScroll() { this.orderScroll = new BScroll(this.$refs.wrapper, { probeType: 3, click:true, pullUpLoad: { // 配置上啦加載 threshold: -80 //上拉80px的時候加載 }, mouseWheel: { // pc端同樣能滑動 speed: 20, invert: false }, useTransition:false, // 防止iphone微信滑動卡頓 }); // 上拉加載數(shù)據(jù) this.orderScroll.on('pullingUp',()=>{ this.scrollFinish = false; // 防止一次上拉觸發(fā)兩次事件,不要在ajax的請求數(shù)據(jù)完成事件中調(diào)用下面的finish方法,否則有可能一次上拉觸發(fā)兩次上拉事件 this.orderScroll.finishPullUp(); // 加載數(shù)據(jù) this.getIncomeDetail(this.nextPage); }); }
總結(jié)
以上所述是小編給大家介紹的vue-better-scroll 的使用實例代碼詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
vue-element-admin項目導(dǎo)入和導(dǎo)出的實現(xiàn)
組件是Vue.js最強大的功能,這篇文章主要介紹了vue-element-admin項目導(dǎo)入和導(dǎo)出的實現(xiàn),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-05-05vue-router的HTML5 History 模式設(shè)置
這篇文章主要介紹了vue-router的HTML5 History模式設(shè)置,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-09-09vue2.x,vue3.x使用provide/inject注入的區(qū)別說明
這篇文章主要介紹了vue2.x,vue3.x使用provide/inject注入的區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04vue3+springboot部署到Windows服務(wù)器的詳細步驟
這篇文章主要介紹了vue3+springboot部署到Windows服務(wù)器,配置Nginx時,因為現(xiàn)在是把vue前端交給了Nginx代理,所以這里的端口號不一定是我們在vue項目中設(shè)置的端口號,本文給大家介紹的非常詳細,需要的朋友參考下吧2022-10-10VUE識別訪問設(shè)備是pc端還是移動端的實現(xiàn)步驟
經(jīng)常在項目中會有支持pc與手機端需求,并且pc與手機端是兩個不一樣的頁面,這時就要求判斷設(shè)置,下面這篇文章主要給大家介紹了關(guān)于VUE識別訪問設(shè)備是pc端還是移動端的相關(guān)資料,需要的朋友可以參考下2023-05-05