<div class="text" id="text">文本。。。。。</div>
<buttion class="goTop" id="gotop">回到顶部按钮</buttion>
我给#text 加了滚动条,#text 的滚动条不是页面的滚动条,顶部位置因为不是浏览器顶部。
#text里面是文本内容,想实现点击按钮回到顶部。好多例子全是回到页面顶部,xx
$('#goTop').on('click', function () {
console.log("搜索_goTop:")
// let box = document.querySelector("#text");
});
window.scrollTo(0,0);
【以下回答由 GPT 生成】
在Vue.js中实现点击按钮回到指定滚动条的初始位置,可以通过以下步骤完成:
<template>
<div>
<div class="text" id="text">文本。。。。。</div>
<button class="goTop" id="gotop" @click="scrollToTop">回到顶部按钮</button>
</div>
</template>
scrollTo()
方法将滚动条回到顶部位置:export default {
methods: {
scrollToTop() {
const text = document.querySelector("#text");
text.scrollTo({
top: 0,
behavior: "smooth"
});
}
}
};
在这个方法中,我们使用querySelector
方法获取到id为"text"的元素,再通过scrollTo()
方法将滚动条的top属性设置为0,behavior属性设置为"smooth"实现平滑滚动到顶部。
<style>
.text {
overflow-y: scroll;
height: 300px;
}
</style>
通过以上步骤,我们可以在Vue.js中实现点击按钮回到指定滚动条的初始位置的功能。
希望这个解决方案对您有帮助!如果还有其他问题,请随时提问。