vue滾動(dòng)軸插件better-scroll使用詳解
跟做慕課網(wǎng)的vue高仿外賣(mài)項(xiàng)目中用到了一個(gè)很好用的插件BScroll,用來(lái)計(jì)算左側(cè)menu欄對(duì)應(yīng)右側(cè)foods欄相應(yīng)顯示的食物區(qū),如果不用插件就比較費(fèi)事了,因此這里分享一下這個(gè)插件的簡(jiǎn)單使用:
一、項(xiàng)目中下載,并引入
在配置文件package.json中引入版本
"dependencies": {
"better-scroll": "^0.1.7"
}
然后進(jìn)入項(xiàng)目目錄,打開(kāi)cmd更新配置
npm i (i是install縮寫(xiě))
最后引入,比如我在項(xiàng)目goods組件中使用則:
import BScroll from 'better-scroll';
二、舉個(gè)栗子
需求是處于當(dāng)前比如熱銷(xiāo)榜欄目,則菜單欄高亮。滾動(dòng)到下一欄高亮欄目則下一欄菜單高亮。點(diǎn)擊菜單中某一欄菜單該欄目高亮并且跳轉(zhuǎn)到對(duì)應(yīng)食物區(qū)。
下面是foods組件中的代碼
template:
<template>
<div class="goods">
<div class="menu-wrap" ref="menuWrap">//菜單欄
<ul>
<li v-for="(item,index) in goods" class="menu-item" :class="{'current':currentIndex===index}" @click="selectMenu(index,$event)">
<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-wrap" ref="foodsWrap">//食物欄
</div>
</div>
</template>
script
<script type="text/ecmascript-6">
import Vue from 'vue';
import BScroll from 'better-scroll';
import shopcart from '@/components/shopcart/shopcart';
export default {
props: {//接收父組件傳的數(shù)據(jù)
seller: {
type: Object
}
},
data () {
return {
goods: [],
listHeight: [],//菜單中一個(gè)菜單欄目的高度
scrollY: 0//定義的Y滾動(dòng)軸及初始值
};
},
computed: {//計(jì)算屬性
currentIndex () {//當(dāng)前菜單欄在整個(gè)菜單中的下標(biāo)index
for (let i = 0; i < this.listHeight.length; i++) {//遍歷菜單中每個(gè)欄目的高度
let height1 = this.listHeight[i];//第i個(gè)欄目的高度
let height2 = this.listHeight[i + 1];//第i+1個(gè)欄目的高度
if (!height2 || (this.scrollY >= height1 && this.scrollY < height2)) {//根據(jù)當(dāng)前滾動(dòng)軸的位置得出index(如果第二個(gè)欄目不存在即第一個(gè)欄目是最后一個(gè))或者(當(dāng)前欄目高度大于等于第一個(gè)并且當(dāng)前滾動(dòng)軸小于第二個(gè)欄目高度)
return i;//如果滿足則返回index
}
}
return 0;//初始值0
}
},
created () {
Vue.prototype.$http.get('/api/goods')
.then(res => {
this.goods = res.data.data;
this.$nextTick(() => {//
this._initScroll();
this._calculateHeight();
});
});
this.classMap = ['decrease', 'discount', 'special', 'invoice', 'guarantee'];
},
methods: {
selectMenu (index, event) {
if (!event._constructed) {
return;
}
let foodList = this.$refs.foodsWrap.getElementsByClassName('food-list-hook');
let el = foodList[index];
this.foodsScroll.scrollToElement(el, 300);
},
_initScroll () {
this.menuScroll = new BScroll(this.$refs.menuWrap, {
click: true
});
this.foodsScroll = new BScroll(this.$refs.foodsWrap, {probeType: 3
});
this.foodsScroll.on('scroll', (pos) => {
this.scrollY = Math.abs(Math.round(pos.y));
console.log(this.scrollY);
});
},
_calculateHeight () {
let foodList = this.$refs.foodsWrap.getElementsByClassName('food-list-hook');
let height = 0;
this.listHeight.push(height);
for (let i = 0; i < foodList.length; i++) {
let item = foodList[i];
height += item.clientHeight;
this.listHeight.push(height);
}
}
}
};
</script>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue+Openlayer中使用select選擇要素的實(shí)現(xiàn)代碼
本文通過(guò)實(shí)例代碼給大家介紹Vue+Openlayer中使用select選擇要素,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2021-08-08
Vue實(shí)現(xiàn)當(dāng)訪問(wèn)的路由不存在時(shí)跳轉(zhuǎn)到404頁(yè)面的方法詳解
在 Vue3 中,可以使用 Vue Router 實(shí)現(xiàn)跳轉(zhuǎn)到 404 頁(yè)面,即當(dāng)用戶訪問(wèn)一個(gè)不存在路由時(shí),系統(tǒng)會(huì)默認(rèn)跳轉(zhuǎn)到 404 頁(yè)面,本文給大家介紹了一個(gè)簡(jiǎn)單的實(shí)現(xiàn)方法,需要的朋友可以參考下2023-12-12
基于axios請(qǐng)求封裝的vue應(yīng)用實(shí)例代碼
這篇文章主要給大家介紹了基于axios請(qǐng)求封裝的vue應(yīng)用的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05
詳解Vue如何使用xlsx庫(kù)導(dǎo)出Excel文件
第三方庫(kù)xlsx提供了強(qiáng)大的功能來(lái)處理Excel文件,它可以簡(jiǎn)化導(dǎo)出Excel文件這個(gè)過(guò)程,本文將為大家詳細(xì)介紹一下它的具體使用,需要的小伙伴可以了解下2025-01-01
ssm+vue前后端分離框架整合實(shí)現(xiàn)(附源碼)
這篇文章主要介紹了ssm+vue前后端分離框架整合實(shí)現(xiàn)(附源碼),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07

