<!-- 左侧的滑动区域 -->
<scroll-view class="left-scroll-view" scroll-y="true" :style="{height: wh + 'px'}">
<!-- 将滚动区域的高度设置为当前设备可视区的高度 -->
<block v-for="(item,i) in cateList" :key="i">
<view :class="i === active ? 'left-scroll-view-item active' : 'left-scroll-view-item'" @click="activeChange(i)">{{item.cat_name}}</view>
</block>
</scroll-view>
<!-- 右侧的滑动区域 -->
<scroll-view class="right-scroll-view" scroll-y="true" :style="{height: wh + 'px'}" :scroll-top="scrollTop">
<view class="right-scroll-view-item" v-for="(item2, i2) in cateLevel2">
<!-- 标题 -->
<view class="title">/{{item2.cat_name}}/</view>
</view>
</scroll-view>
<script>
export default {
data() {
return {
wh: 0, // 当前设备可用的高度
cateList: [
{
cat_name: '电视',
children: [
{
cat_name: '液晶电视',
},
{
cat_name: '彩色电视'
}
]
},
{
cat_name: '冰箱',
children: [
{
cat_name: '美的冰箱',
},
{
cat_name: '普通冰箱'
}
]
},
{
cat_name: '空调',
children: [
{
cat_name: '格力空调',
},
{
cat_name: '海尔空调'
}
]
},
], // 分类列表
active: 0, // 当前激活项
cateLevel2: [], // 右侧列表的标题
scrollTop: 0,
};
},
onLoad() {
// 获取当前使用的手机设备信息
const sysInfo = uni.getSystemInfoSync()
console.log(sysInfo)
this.wh = sysInfo.screenHeight
},
methods: {
// 切换tab
activeChange(i) {
this.active = i
// tab切换,右侧区域内容也要变
this.cateLevel2 = this.cateList[i].children
// this.scrollTop = 0
this.scrollTop = this.scrollTop === 0 ? '1' : '0'
},
}
}
</script>
解决: 给右侧的滑动区域加上scroll-top属性,scroll-top是指竖向滚动条的位置;当每次切换tab时,设置scroll-top为0,就能使页面从开始位置显示
但是有一个问题,scrollTop的初始值为0,当改变它的值,设置的还是0时,就不会生效,所以,可以设置一下:当scrollTop为0时,设置它为1,当它为1时,设置为0,只有1px的偏差,使用者是体验不出来的,还能达到目的。