scroll-view
使用
scroll-view
可滚动到对应区域,
注意
: 使用竖向滚动时,需要给scroll-view一个固定高度,通过 WXSS 设置height
。这里写两个应用场景的
demo
仅供参考,欢迎大神指点…
场景一:
项目中遇到需求:
- 页面中有
未知按钮
【同一事件
每一个按钮所执行的业务不一样】,- 点击
当前按钮
触发事件发送请求
后端。数据回来后,页面显示相关数据并自动滑动
到对应区域
效果图
wxml
<!-- button -->
<view wx:for="{{getLocalPayData}}" wx:key="*this">
<button class="" bindtap='startMeasure' id="NAV{{index}}" data-index="{{index}}" data-items='{{item}}' disabled="{{item.flagDisabled}}">
{{item.flagText}}
</button>
</view>
<!-- 内容 scroll-view -->
<scroll-view scroll-y scroll-with-animation="true" scroll-into-view="NAV{{toView}}" style="height: 400px;">
<view class='measureDetailBox'>
<view class='measureDetailList'>
<view class='measureDetailItem' wx:for="{{saveAllSubmitData}}" wx:key="" id="NAV{{index}}">
<!-- 其他内容 -->
</view>
</view>
</view>
</scroll-view>
js
data: {
toView: '', //初始化
}
/**
* 点击开台测量事件
*/
startMeasure: function(e) {
// console.log('点击按钮获取', e);
var that = this;
let currentIndex = e.currentTarget.dataset.index;
console.log('当前index:' + currentIndex);
that.setData({
currentIndex: currentIndex,
})
//改变按钮文本为 "测量中"
that.data.getLocalPayData.forEach((item, i) => {
if (Number(i) == that.data.currentIndex) {
var setBtnTxt = "getLocalPayData[" + i + "].flagText";
that.setData({
[setBtnTxt]: "测量中",
})
}
})
}
/**
* 提交事件
* 后端数据回来渲染页面并自动滚动到对应区域
*/
measureSubmit: function(paramVal) {
var that = this;
var params = {
"orderitem_id": that.data.currentOrderId,
"value1": paramVal.value1
}
http.postReq('users/gaugesends...', params, function(res) {
console.log('测量成功返回的数据:', res);
feedbackApi.hideTimerToast();
if (res.code != 200) {
feedbackApi.showToast({
title: res.msg,
mask: false
})
return
}
//保存数据
let datas = res.data;
//把每次提交的数据追加保存
let allDatas = that.data.saveAllSubmitData.concat(datas);
that.setData({
saveAllSubmitData: allDatas,
})
//这里使用setTimeout,因先渲染数据.
setTimeout(() => {
//改变当前按钮状态
that.data.getLocalPayData.forEach((item, i) => {
if (Number(i) == that.data.currentIndex) {
var setDisabled = "getLocalPayData[" + i + "].flagDisabled";
var setBtnTxt = "getLocalPayData[" + i + "].flagText";
that.setData({
[setDisabled]: "true", //禁用button
[setBtnTxt]: "获取成功", //修改button文本
//保存当前index,赋值给scroll-into-view属性,滚动到对应区域
toView: that.data.currentIndex,
})
}
})
}, 800)
})
},
场景二:
已上传过此demo, 横纵向可滚动, 请看下图
效果图
下图: 标题采用
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;
}
}
}