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