微信小程序scroll-view

scroll-view

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

效果图

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

在这里插入图片描述

wxml

<!-- 使用scroll-view 滚动到对应位置 -->
<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;
        }
    } 
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值