十、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/[email protected]);
        }

        .goods .menu-wrapper .menu-item .discount {
            background-image: url(img/[email protected]);
        }

        .goods .menu-wrapper .menu-item .guarantee {
            background-image: url(img/[email protected]);
        }

        .goods .menu-wrapper .menu-item .invoice {
            background-image: url(img/[email protected]);
        }

        .goods .menu-wrapper .menu-item .special {
            background-image: url(img/[email protected]);
        }

        .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;
        }

页面布局


JS代码


最后完整的效果图如下:

最终效果图

感兴趣的朋友可以去试试。
为了更好的服务大家,请加入我们的技术交流群:( 511387930),同时您也可以扫描下方的二维码关注我们的公众号,每天我们都会分享经验,谢谢大家。
十、Vue如何使用better-scroll插件_第1张图片

你可能感兴趣的:(十、Vue如何使用better-scroll插件)