微信小程序scroll-view

scroll-view

使用scroll-view可滚动到对应区域,
注意: 使用竖向滚动时,需要给scroll-view一个固定高度,通过 WXSS 设置 height

效果图

下图: 标题采用scroll-x横向滚动,内容采用scroll-y纵向滚动,当点击某个分类时滚动到对应区域

微信小程序scroll-view_第1张图片

wxml


<view class="scrollViewWrap">
    
    <view  class="cagetory-scroll">
        <scroll-view scroll-x="true" scroll-into-view="NAV{{status}}" scroll-with-animation="true">
            <text bindtap="getStatus" id="NAV{{index}}" class="cagetory-nav-li {{index === status ? 'cagetory-nav-active' : ''}}" data-index="{{index}}" wx:for="{{navList}}" wx:key="">{{item}}text>
        scroll-view>
    view>

    
    <view class="cagetory-fixed-y">
        <scroll-view class="cagetory-scroll-y" scroll-y="true" scroll-into-view="NAV{{status}}" scroll-with-animation="true">
            <view wx:for="{{navList}}" wx:key="">
                <view id="NAV{{index}}" class="cagetory-list-head">{{item}}view>
                <view class="cagetory-list-li">{{item}} 分类 {{index}}view>
            view>
        scroll-view>
    view>
view>

js

Page({

    /**
     * 页面的初始数据
     */
    data: {
        toView: '',
        navList: [
            "关注",
            "推荐",
            "时常",
            "正能量",
            "科技",
            "运动",
            "小视频",
            "生活",
            "体育",
            "军事",
        ],
    },
    getStatus(e) {
        console.log('获取值', e);
        this.setData({
            // 获取当前index
            status: e.currentTarget.dataset.index,
        })
    },
})

wxss

以下样式可通过Webstorm编译为wxss

.scrollViewWrap {
    .cagetory-fixed-x {
        width: 100%;
        position: fixed;
        top: 0;
        left: 0;
    }

    .cagetory-scroll {
        height: 80rpx;
        line-height: 80rpx;
        width: 100%;
        white-space: nowrap;
        text {
            /* height: 120rpx;
            line-height: 80rpx;
            width: 100%;
            white-space: nowrap; */
        }
        .cagetory-nav-li {
            font-size: 33rpx;
            padding: 0 10rpx;
            &:first-of-type {
                padding-left: 16rpx;
            }
            &:last-of-type {
                padding-right: 16rpx;
            }
        }
         .cagetory-nav-active {
            color: red;
            border-bottom: 3rpx solid red;
        }
    }

    .cagetory-fixed-y {
        width: 100%;
        height: calc(100% - 200rpx);
        position: fixed;
        bottom: 0;
        left: 0;
        .cagetory-scroll-y {
            padding: 0 20rpx;
            height: 100%;
            box-sizing: border-box;
        }
        .cagetory-list-head {
            height: 50px;
            line-height: 50px;
            text-align: center;
            font-size: 30rpx;
            color: skyblue;
        }

        .cagetory-list-li {
            height: 400px;
            padding: 10rpx;
            color: #fff;
            font-size: 50rpx;
            background-color: skyblue;
        }
    } 
}

你可能感兴趣的:(微信小程序)