代码:
<template>
<div class="process-details-container">
<div class="left-box">
<div
v-for="(item, index) in titelList"
:key="item.id"
:class="activeIndex == index ? 'btn btn-active' : 'btn'"
@click="navClick(item, index)"
>{{ item.title }}</div>
</div>
<div class="right-box" @scroll="onScroll">
<div v-for="item in titelList" :key="item.id" :id="item.id" class="section-box">
<div class="title">{{ item.title }}</div>
</div>
</div>
</div>
</template>
<script>
export default {
data() {
return {
titelList: [
{
title: '党委会阶段',
id: 'conferenceStage',
top: '',
},
{
title: '招标阶段',
id: 'biddingPhase',
top: '',
},
{
title: '合同执行情况',
id: 'contractExecution',
top: '',
},
{
title: '结题资料',
id: 'concludingData',
top: '',
}
],
activeIndex: 0,
}
},
mounted() {
this.init();
},
methods: {
init() {
this.titelList.forEach(item => {
let anchor = document.getElementById(item.id);
item.top = anchor.offsetTop;
})
// console.log(this.titelList);
},
navClick(item, index) {
this.activeIndex = index;
let anchor = document.getElementById(item.id);
// console.log(anchor.offsetTop, 'top');
anchor.scrollIntoView({ behavior: "smooth" });
},
onScroll(e) {
// console.log(e.target.scrollTop, 'scroll');
this.titelList.map((item, index) => {
if (e.target.scrollTop >= item.top) {
this.activeIndex = index;
}
})
},
}
}
</script>
<style lang="scss" scoped>
.process-details-container {
position: relative;
width: 100%;
height: 700px;
display: flex;
align-items: center;
.left-box {
width: 200px;
height: 100%;
background-color: #eef1f6;
.btn {
padding: 0 20px;
height: 50px;
line-height: 50px;
cursor: pointer;
}
.btn:hover {
color: #1890ff;
background-color: #c8d6f0;
}
.btn-active {
color: #1890ff;
background-color: #c8d6f0;
}
}
.right-box {
width: calc(100% - 200px);
height: 100%;
padding: 0 20px;
overflow: auto;
.section-box {
width: 100%;
height: 500px;
.title {
width: 100%;
height: 40px;
line-height: 40px;
padding: 0 10px;
background-color: #7db6eb;
color: #fff;
}
}
}
}
</style>