1、html部分
<template>
<view>
<view class="left_right_column_box">
<view class="left_column">
<scroll-view :style="{height: viewHeight}" scroll-y="true" :scroll-top="scrollTop">
<view v-for="item in titleContenData" :key="item.id"
:class="{'left_column_for': true, activity: selectIndex==item.index?true:false}"
@click="clickTitle(item)">
{{item.title}}
</view>
</scroll-view>
</view>
<view class="right_column">
<scroll-view :style="{height: viewHeight}" scroll-y="true" :scroll-into-view="selectId"
scroll-with-animation="true" @scroll="scroll">
<view :id="item.id" class="floorType right_title_content_for" v-for="item in titleContenData"
:key="item.id">
<view class="right_title">{{item.title}}</view>
<view v-for="items in item.contents" :key="items.id">{{items.content}}</view>
</view>
</scroll-view>
</view>
</view>
</view>
</template>
2、JavaScript部分
let timer = undefined;
export default {
data() {
return {
viewHeight: null,
titleContenData: [],
selectId: 'id1',
selectIndex: 0,
scrollTop: 0,
}
},
mounted() {
let that = this;
uni.getSystemInfo({
success: function({
windowHeight
}) {
that.viewHeight = windowHeight + 'px';
}
});
that.createData();
that.$nextTick(function() {
const query = uni.createSelectorQuery().in(that);
query.selectAll('.floorType').boundingClientRect(VNodeAll => {
VNodeAll.forEach(({
top
}, i) => {
this.titleContenData[i].viewTop = top;
});
}).exec();
console.log('titleContenData:', this.titleContenData);
});
},
methods: {
scroll({
detail: {
scrollTop
}
}) {
let that = this,
titleContenData = that.titleContenData;
if (timer !== undefined) clearTimeout(timer);
timer = setTimeout(function() {
scrollTop = scrollTop < 10 ? 0 : scrollTop;
let selectIndex = titleContenData.findIndex((item) => item.viewTop >= scrollTop);
console.log('scrollTop:', scrollTop);
that.selectIndex = selectIndex;
that.scrollTop = 5 * that.selectIndex;
}, 70);
},
clickTitle({
id,
index
}) {
console.log(id, index);
this.selectId = id;
this.selectIndex = index;
},
createContent(n) {
let content = [];
for (let i = 0; i < n; i++) content.push({
id: i + 1,
content: `内容${i+1}内容`
});
return content;
},
createData() {
for (let i = 0; i < 24; i++) this.titleContenData.push({
id: `id${i+1}`,
index: i,
title: '标题' + (i + 1),
contents: this.createContent(parseInt(Math.random() * 24 + 1, 10))
})
}
},
}
3、css部分
.left_right_column_box {
width: 100%;
display: flex;
justify-content: space-evenly;
}
.left_column {
flex: 1;
}
.left_column_for {
text-align: center;
padding: 10rpx 0;
}
.activity {
color: #000fff;
}
.right_column {
flex: 3;
margin-left: 36rpx;
}
.right_title_content_for {
margin-top: 36rpx;
}
.right_title_content_for:first-child {
margin-top: 0;
}
.right_title {
font-weight: 700;
}
scroll-view ::-webkit-scrollbar {
width: 0;
height: 0;
background-color: transparent;
}