<div
class="qp-box"
v-for="(allItem, allIndex) in qList"
:key="allIndex"
:ref="'ques' + allIndex"
>
<div class="question-box">
.......
</div>
</div>
...
qList: [],
...
mounted() {
window.addEventListener("scroll", this.getTopicIndex, true);
},
methods: {
getTopicIndex(index) {
let name = "ques" + index;
if (this.$refs[name] !== undefined) {
var target = this.$refs[name][0].getBoundingClientRect().top;
}
let top = 0;
const scrollTop =document.documentElement.scrollTop ||window.pageYOffset ||document.body.scrollTop;
if (typeof name === "number") {
top = name - scrollTop;
} else {
top = target - scrollTop - 46;
}
window.scrollBy({ top, behavior: "smooth" });
},