在我們移動項目開發(fā)中加勤,經(jīng)常會處理滾動列表的功能,有的是橫向滾動列表同波,有的是縱向滾動列表鳄梅。我們在實現(xiàn)這類滾動效果的時候,一般會使用better-scroll插件未檩。
better-scroll是基于移動端滾動的解決方案戴尸,它是基于iscroll重寫。better-scroll很強大冤狡,不僅可以做普通的滾動列表孙蒙,還可以做輪播圖、Picker等等悲雳。
今天我就以“餓了么”來講解如何使用better-scroll挎峦。先看“餓了么”動圖:
今天我們主要實現(xiàn)以下幾個功能:
1、左右側(cè)頁面滑動合瓢,并且不顯示滾動條
2坦胶、根據(jù)左側(cè)的商品類別對應(yīng)到右側(cè)相應(yīng)的選擇區(qū)間,并且高亮標題
3、根據(jù)右側(cè)用戶滑動的區(qū)間顿苇,能夠?qū)?yīng)到左側(cè)的商品類別峭咒,并且高亮選擇標題
安裝插件
這個很簡單,一句話就搞定纪岁。
npm install--save better-scroll
以下我們就直接上代碼凑队,需要注意的地方我們會說明
樣式代碼
[v-cloak] {
display: none;
}
.goods {
position: absolute;
width: 100%;
top: 174px;
bottom: 46px;
display: flex;
overflow: hidden;
}
.goods .menu-wrapper {
flex: 0 0 80px;
width: 80px;
background: #f3f5f7;
}
.goods .menu-wrapper .current {
position: relative;
z-index: 10;
margin-top: -1px;
background: #FFFFFF;
font-weight: 700;
font-size: 14px;
}
.goods .menu-wrapper .menu-item {
display: table;
height: 54px;
width: 80px;
line-height: 14px;
padding: 0 12px;
border-bottom: 1px solid rgba(7, 17, 27, .1);
box-sizing: border-box;
}
.goods .menu-wrapper .menu-item .icon {
display: inline-block;
width: 12px;
height: 12px;
margin-right: 2px;
-webkit-background-size: 12px 12px;
background-size: 12px 12px;
background-repeat: no-repeat;
vertical-align: top;
}
.goods .menu-wrapper .menu-item .text {
display: table-cell;
width: 56px;
vertical-align: middle;
font-size: 12px;
text-align: center;
}
.goods .menu-wrapper .menu-item .decrease {
background-image: url(img/decrease_2@2x.png);
}
.goods .menu-wrapper .menu-item .discount {
background-image: url(img/decrease_2@2x.png);
}
.goods .menu-wrapper .menu-item .guarantee {
background-image: url(img/decrease_2@2x.png);
}
.goods .menu-wrapper .menu-item .invoice {
background-image: url(img/decrease_2@2x.png);
}
.goods .menu-wrapper .menu-item .special {
background-image: url(img/decrease_2@2x.png);
}
.goods .foods-wrapper {
flex: 1;
}
.goods .foods-wrapper .title {
padding-left: 14px;
height: 26px;
line-height: 26px;
border-left: 2px solid #d9dde1;
font-size: 12px;
color: rgb(147, 153, 159);
background: #F3F5F7;
}
.goods .foods-wrapper .current {
color: #42B983;
font-size: 14px;
transition: all .5s;
line-height: 27px;
}
.goods .foods-wrapper .food-item {
display: flex;
margin: 18px 0 18px 0;
border-bottom: 1px solid rgba(7, 17, 27, .1);
padding-bottom: 18px;
}
.goods .foods-wrapper .food-item:last-child {
border-bottom: 0px solid rgba(7, 17, 27, .1);
margin-bottom: 0;
}
.goods .foods-wrapper .food-item .icon {
flex: 0 0 57px;
margin-right: 10px;
margin-left: 10px;
}
.goods .foods-wrapper .food-item .content {
position: relative;
flex: 1;
}
.goods .foods-wrapper .food-item .content .name {
margin: 2px 0 8px 0;
height: 14px;
line-height: 14px;
font-size: 14px;
color: rgb(7, 17, 27);
}
.goods .foods-wrapper .food-item .content .desc {
margin-bottom: 8px;
line-height: 10px;
font-size: 10px;
color: rgb(147, 153, 159);
}
.goods .foods-wrapper .food-item .content .extra {
font-size: 10px;
color: rgb(147, 153, 159);
line-height: 10px;
}
.goods .foods-wrapper .food-item .content .extra .count {
margin-right: 12px;
}
.goods .foods-wrapper .food-item .content .price {
font-weight: 700;
line-height: 24px;
}
.goods .foods-wrapper .food-item .content .price .now {
margin-right: 8px;
font-size: 14px;
color: rgb(240, 20, 20);
}
.goods .foods-wrapper .food-item .content .price .old {
text-decoration: line-through;
font-size: 10px;
color: rgb(147, 153, 159);
}
.goods .foods-wrapper .food-item .content .cartcontrol-wrapper {
position: absolute;
right: 6px;
bottom: 12px;
}
頁面布局
<template>
<div class="goods" v-cloak>
<div class="menu-wrapper" ref="menuwrapper">
<ul>
<!--當currentIndex與index相等的時候,設(shè)置高亮-->
<li v-for="(item,index) in goods" class="menu-item" :class="{'current':currentIndex === index}" @click="selectMenu(index,$event)" v-cloak>
<span class="text">
<span v-show="item.type>0" class="icon" :class="classMap[item.type]" v-cloak></span> {{item.name}}
</span>
</li>
</ul>
</div>
<div class="foods-wrapper" ref="foodwrapper">
<ul>
<!--food-list-hook用于dom操作幔翰,獲取整體容器的高度-->
<li v-for="(item,index) in goods" class="food-list food-list-hook" v-cloak>
<h2 class="title" :class="{'current':currentIndex === index}">{{item.name}}</h2>
<ul>
<li @click="selectfood(food,$event)" v-for="food in item.foods" class="food-item">
<div class="icon">
![](food.icon)
</div>
<div class="content">
<h2 class="name">{{food.name}}</h2>
<p class="desc">{{food.description}}</p>
<div class="extra">
<span class="count">月售{{food.sellCount}}份</span>
<span>好評率{{food.rating}}%</span>
</div>
<div class="price">
<span class="now">¥{{food.price}}</span>
<span v-show="food.oldPrice" class="old">¥{{food.oldPrice}}</span>
</div>
</div>
</li>
</ul>
</li>
</ul>
</div>
</div>
</template>
JS代碼
<script>
export default {
data() {
return {
msg: 'goods',
goods: [],
listHeight: [],
scrollY: 0,
}
},
created() {
this.classMap = ['decrease', 'discount', 'guarantee', 'invoice', 'special'];
this.$http.get('./data.json').then((res) => {
if(res.status === ERR_OK) {
res = res.body.goods;
this.goods = res;
//dom結(jié)構(gòu)加載結(jié)束
this.$nextTick(() => {
this._initScroll();
//計算高度
this._calculateHeight();
})
}
});
},
computed: {
currentIndex() {
for(let i = 0; i < this.listHeight.length; i++) {
//判斷當currentIndex在height1和height2之間的時候顯示
let height1 = this.listHeight[i];
let height2 = this.listHeight[i + 1];
// console.log('height1:'+height1+','+'height2:'+height2)
//最后一個區(qū)間沒有height2
if(!height2 || (this.scrollY >= height1 && this.scrollY < height2)) {
return i;
}
}
return 0;
}
},
methods: {
selectMenu(index, event) {
// 自己默認派發(fā)事件時候(BScroll),_constructed被置為true,但是瀏覽器原生并沒有這個屬性
if(!event._constructed) {
return;
}
//運用BScroll接口顽决,滾動到相應(yīng)位置
let foodList = this.$refs.foodwrapper.getElementsByClassName('food-list-hook');
//獲取對應(yīng)元素的列表
let el = foodList[index];
this.foodScroll.scrollToElement(el, 300);
},
/*
_initScroll的函數(shù),主要用來對左右兩側(cè)dom結(jié)構(gòu)進行初始化
用better-scroll的方法初始化需要滾動的dom結(jié)構(gòu)导匣,
vue為我們提供了一個方法可以便利的獲取到 dom結(jié)構(gòu)才菠,
我們在需要獲取dom結(jié)構(gòu)的父容器內(nèi)添加ref="foodwrapper" ,
然后在函數(shù)內(nèi)用this.$refs.menuwrapper獲取到dom
*/
_initScroll() {
this.meunScroll = new BScroll(this.$refs.menuwrapper, {
click: true
});
this.foodScroll = new BScroll(this.$refs.foodwrapper, {
click: true,
//探針作用贡定,實時監(jiān)測滾動位置
probeType: 3
});
//設(shè)置監(jiān)聽滾動位置
this.foodScroll.on('scroll', (pos) => {
//scrollY接收變量
this.scrollY = Math.abs(Math.round(pos.y));
})
},
_calculateHeight() {
let foodList = this.$refs.foodwrapper.getElementsByClassName('food-list-hook');
let height = 0;
//把第一個高度送入數(shù)組
this.listHeight.push(height);
//通過循環(huán)foodList下的dom結(jié)構(gòu)赋访,將每一個li的高度依次送入數(shù)組
for(let i = 0; i < foodList.length; i++) {
let item = foodList[i]
height += item.clientHeight
this.listHeight.push(height);
}
},
}
}
</script>
最后完整的效果圖如下:
感興趣的朋友可以去試試。
為了更好的服務(wù)大家缓待,請加入我們的技術(shù)交流群:(511387930)蚓耽,同時您也可以掃描下方的二維碼關(guān)注我們的公眾號,每天我們都會分享經(jīng)驗旋炒,謝謝大家步悠。