這篇文章將為大家詳細(xì)講解有關(guān)vue-better-scroll怎么用,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

創(chuàng)新互聯(lián)建站2013年開(kāi)創(chuàng)至今,先為晉城等服務(wù)建站,晉城等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為晉城企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問(wèn)題。
首先安裝better-scroll
npm i better-scroll -S
goods頁(yè)面模板
<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" >
<h2 class="title">{{item.name}}</h2>
<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">
<h3 class="name">{{food.name}}</h3>
<p class="desc">{{food.description}}</p>
<div class="extra">
<span class="food-number">月售{{food.sellCount}}份</span>
<span>好評(píng)率{{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注冊(cè)
在鉤子函數(shù) create中 用vue-resource 請(qǐng)求數(shù)據(jù),并異步調(diào)用方法
this.$nextTick(()=>{
this._initScroll();
}注冊(cè)方法
_initScroll(){
this.meunScroll=new BScroll(this.$refs.menuWrapper,{});
this.foodsScroll=new BScroll(this.$refs.foodsWrapper,{});
}better-scroll用法
我們先來(lái)看一下 better-scroll 常見(jiàn)的 html 結(jié)構(gòu):
<div class="wrapper"> <ul class="content"> <li></li> <li></li> <li></li> <li></li> </ul> </div>
當(dāng) content 的高度不超過(guò)父容器的高度,是不能滾動(dòng)的,而它一旦超過(guò)了父容器的高度,我們就可以滾動(dòng)內(nèi)容區(qū)了,這就是 better-scroll 的滾動(dòng)原理。
import BScroll from 'better-scroll'
let wrapper = document.querySelector('.wrapper')
let scroll = new BScroll(wrapper, {})better-scroll 對(duì)外暴露了一個(gè) BScroll 的類,我們初始化只需要 new 一個(gè)類的實(shí)例即可。第一個(gè)參數(shù)就是我們 wrapper 的 DOM 對(duì)象,第二個(gè)是一些配置參數(shù)。
better-scroll 的初始化時(shí)機(jī)很重要,因?yàn)樗诔跏蓟臅r(shí)候,會(huì)計(jì)算父元素和子元素的高度和寬度,來(lái)決定是否可以縱向和橫向滾動(dòng)。因此,我們?cè)诔跏蓟臅r(shí)候,必須確保父元素和子元素的內(nèi)容已經(jīng)正確渲染了。如果沒(méi)有辦法滑動(dòng),那就是初始化的時(shí)機(jī)不對(duì)。
餓了么是這樣處理的:
mounted() {
this.$nextTick(() => {
this.scroll = new Bscroll(this.$refs.wrapper, {}) })
}this.$nextTick()這個(gè)方法作用是當(dāng)數(shù)據(jù)被修改后使用這個(gè)方法會(huì)回調(diào)獲取更新后的dom再render出來(lái)
如果不在下面的this.$nextTick()方法里回調(diào)這個(gè)方法,數(shù)據(jù)改變后再來(lái)計(jì)算滾動(dòng)軸就會(huì)出錯(cuò)
上拉刷新功能
<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),這里的問(wèn)題是,當(dāng)一頁(yè)數(shù)據(jù)的高度不夠srapper的高度的時(shí)候,即使存在n頁(yè)也不能下拉
// 需要設(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的時(shí)候加載
},
mouseWheel: { // pc端同樣能滑動(dòng)
speed: 20,
invert: false
},
useTransition:false, // 防止iphone微信滑動(dòng)卡頓
});
// 上拉加載數(shù)據(jù)
this.orderScroll.on('pullingUp',()=>{
this.scrollFinish = false;
// 防止一次上拉觸發(fā)兩次事件,不要在ajax的請(qǐng)求數(shù)據(jù)完成事件中調(diào)用下面的finish方法,否則有可能一次上拉觸發(fā)兩次上拉事件
this.orderScroll.finishPullUp();
// 加載數(shù)據(jù)
this.getIncomeDetail(this.nextPage);
});
}關(guān)于“vue-better-scroll怎么用”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
當(dāng)前名稱:vue-better-scroll怎么用
網(wǎng)頁(yè)URL:http://www.chinadenli.net/article48/pgshep.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、全網(wǎng)營(yíng)銷推廣、做網(wǎng)站、網(wǎng)站設(shè)計(jì)公司、商城網(wǎng)站、云服務(wù)器
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)