十、Vue如何使用better-scroll插件

在我们移动项目开发中,经常会处理滚动列表的功能,有的是横向滚动列表,有的是纵向滚动列表。我们在实现这类滚动效果的时候,一般会使用better-scroll插件。
better-scroll是基于移动端滚动的解决方案,它是基于iscroll重写。better-scroll很强大,不仅可以做普通的滚动列表,还可以做轮播图、Picker等等。
今天我就以“饿了么”来讲解如何使用better-scroll。先看“饿了么”动图:

饿了么

今天我们主要实现以下几个功能:

1、左右侧页面滑动,并且不显示滚动条
2、根据左侧的商品类别对应到右侧相应的选择区间,并且高亮标题
3、根据右侧用户滑动的区间,能够对应到左侧的商品类别,并且高亮选择标题

安装插件

这个很简单,一句话就搞定。

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相等的时候,设置高亮-->
                    <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结构加载结束
                        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)
                        //最后一个区间没有height2
                        if(!height2 || (this.scrollY >= height1 && this.scrollY < height2)) {
                            return i;
                        }
                    }
                    return 0;
                }
            },
methods: {
                selectMenu(index, event) {
                    //      自己默认派发事件时候(BScroll),_constructed被置为true,但是浏览器原生并没有这个属性
                    if(!event._constructed) {
                        return;
                    }
                    //运用BScroll接口,滚动到相应位置
                    let foodList = this.$refs.foodwrapper.getElementsByClassName('food-list-hook');
                    //获取对应元素的列表
                    let el = foodList[index];
                    this.foodScroll.scrollToElement(el, 300);
                },
                 /*
                  _initScroll的函数,主要用来对左右两侧dom结构进行初始化 
                 用better-scroll的方法初始化需要滚动的dom结构,
                vue为我们提供了一个方法可以便利的获取到 dom结构,
                我们在需要获取dom结构的父容器内添加ref="foodwrapper" ,
               然后在函数内用this.$refs.menuwrapper获取到dom
                */
                _initScroll() {
                    this.meunScroll = new BScroll(this.$refs.menuwrapper, {
                        click: true
                    });
                    this.foodScroll = new BScroll(this.$refs.foodwrapper, {
                        click: true,
                        //探针作用,实时监测滚动位置
                        probeType: 3
                    });
                    //设置监听滚动位置
                    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;
                    //把第一个高度送入数组
                    this.listHeight.push(height);
                    //通过循环foodList下的dom结构,将每一个li的高度依次送入数组
                    for(let i = 0; i < foodList.length; i++) {
                        let item = foodList[i]
                        height += item.clientHeight
                        this.listHeight.push(height);
                    }
                },
            }
}
</script>

最后完整的效果图如下:

最终效果图

感兴趣的朋友可以去试试。
为了更好的服务大家,请加入我们的技术交流群:(511387930),同时您也可以扫描下方的二维码关注我们的公众号,每天我们都会分享经验,谢谢大家。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,117评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,328评论 1 293
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,839评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,007评论 0 206
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,384评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,629评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,880评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,593评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,313评论 1 243
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,575评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,066评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,392评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,052评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,082评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,844评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,662评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,575评论 2 270

推荐阅读更多精彩内容