vue滾動(dòng)軸插件better-scroll使用詳解
跟做慕課網(wǎng)的vue高仿外賣項(xiàng)目中用到了一個(gè)很好用的插件BScroll,用來計(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)目目錄,打開cmd更新配置
npm i (i是install縮寫)
最后引入,比如我在項(xiàng)目goods組件中使用則:
import BScroll from 'better-scroll';
二、舉個(gè)栗子
需求是處于當(dāng)前比如熱銷榜欄目,則菜單欄高亮。滾動(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)文章
element實(shí)現(xiàn)二級(jí)菜單和頂部導(dǎo)航聯(lián)動(dòng)的示例
本文主要介紹了element實(shí)現(xiàn)二級(jí)菜單和頂部導(dǎo)航聯(lián)動(dòng)的示例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02vue + Electron 制作桌面應(yīng)用的示例代碼
這篇文章主要介紹了vue + Electron 制作桌面應(yīng)用,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-11-11詳解vue-cli 構(gòu)建Vue項(xiàng)目遇到的坑
本篇文章主要介紹了詳解vue-cli 構(gòu)建Vue項(xiàng)目遇到的坑,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08vue 簡(jiǎn)單自動(dòng)補(bǔ)全的輸入框的示例
這篇文章主要介紹了vue 簡(jiǎn)單自動(dòng)補(bǔ)全的輸入框的示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-03-03基于axios封裝fetch方法及調(diào)用實(shí)例
下面小編就為大家分享一篇基于axios封裝fetch方法及調(diào)用實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-02-02vue3+ts實(shí)現(xiàn)樹形組件(菜單組件)
本文主要介紹了vue3+ts實(shí)現(xiàn)樹形組件(菜單組件),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05Vue3視頻播放器組件Vue3-video-play新手入門教程
這篇文章主要給大家介紹了關(guān)于Vue3視頻播放器組件Vue3-video-play新手入門教程的相關(guān)資料,本文實(shí)例為大家分享了vue-video-player視頻播放器的使用配置,供大家參考,需要的朋友可以參考下2023-12-12