固定在界面右侧的“回到顶部”按钮
首先要给需要定位的元素一个id,后续根据id跳转,不管是回到顶部还是定位到界面任意位置,都是根据id来定位。
其次给需要触发定位的元素加上点击事件toPage(index),这个index参数直接写好,比如0就是定位到顶部,1就是定位到其他我想定位的位置…
<div class="top" @click="toPage(0)">TOP</div>
methods: {
toPage(index) {
if (index == 0) {
document.getElementById("cover").scrollIntoView({ behavior: "smooth" });
}
if (index == 1) {
document.getElementById("face").scrollIntoView({ behavior: "smooth" });
}
},
},
.top {
position: fixed;
right: 0;
bottom: 50%;
z-index: 99999;
width: 95px;
height: 95px;
background-color: #0180bc;
}