Vue3一键回到顶部/底部

效果图:

 

 示例代码:

<template>
  <div class="main">
    <div class="box">这是第一个盒子</div>
    <div class="box">这是第二个盒子</div>
    <div class="box">这是第三个盒子</div>
    <div class="box">这是第四个盒子</div>

    <div ref="totop" class="totop" @click="scrollToTop">返回顶部</div>
  </div>
</template>

<script setup lang="ts">
const totop = ref();

const scrollToTop = () => {
  window.scrollTo({
    // top: document.documentElement.offsetHeight, //回到底部
    top: 0, //回到顶部
    left: 0,
    behavior: "smooth", //smooth 平滑;auto:瞬间
  });
};

onMounted(() => {
  // 页面滚动窗口监听事件
  window.onscroll = function () {
    // 获取浏览器卷去的高度
    let high = document.documentElement.scrollTop || document.body.scrollTop; //兼容各浏览器
    if (high >= 900) {
      totop.value.style.display = "block";
    } else {
      totop.value.style.display = "none";
    }
  };
});
</script>

<style scoped lang="scss">
.main {
  .box {
    width: 100vw;
    margin: 10px auto;
    background-color: rgb(173, 173, 173);
    font-size: 50px;
    text-align: center;
    line-height: 400px;
  }
  .totop {
    display: none;
    position: fixed;
    bottom: 100px;
    right: 50px;
    background-color: skyblue;
    padding: 10px;
    &:hover {
      cursor: pointer;
      color: #fff;
    }
  }
}
</style>

回到底部:

  window.scrollTo({
    top: document.documentElement.offsetHeight, //回到底部
    left: 0,
    behavior: "smooth", //smooth 平滑;auto:瞬间
  });

封装BackTop.vue组件

 

 components/BackTop.vue: 

<template>
    <div ref="totop" class="totop" @click="scrollToTop" title="返回顶部">
        <svg t="1681820716964" class="icon" style="display:block" viewBox="0 0 1024 1024" version="1.1"
            xmlns="http://www.w3.org/2000/svg" p-id="6409" width="32" height="32">
            <path
                d="M866.7 96H157.3c-17.8 0-32.2 14.4-32.2 32.2 0 17.8 14.4 32.2 32.2 32.2h709.5c17.8 0 32.2-14.4 32.2-32.2 0-17.8-14.4-32.2-32.3-32.2zM512 225c-16.5 0-33 6.3-45.6 18.9L169.9 540.3c-5.8 5.8-9.4 13.9-9.4 22.8s3.6 17 9.4 22.8c5.8 5.8 13.9 9.4 22.8 9.4s17-3.6 22.8-9.4l264.2-264.2v574c0 17.8 14.4 32.2 32.2 32.2 17.8 0 32.2-14.4 32.2-32.2v-574l264.2 264.2c5.8 5.8 13.9 9.4 22.8 9.4s17-3.6 22.8-9.4c5.8-5.8 9.4-13.9 9.4-22.8s-3.6-17-9.4-22.8L557.6 243.9C545 231.3 528.5 225 512 225z"
                fill="#ffffff" p-id="6410"></path>
        </svg>
    </div>
</template>
   
<script setup lang="ts">
const totop = ref();

const scrollToTop = () => {
    window.scrollTo({
        // top: document.documentElement.offsetHeight, //回到底部
        top: 0, //回到顶部
        left: 0,
        behavior: "smooth", //smooth 平滑;auto:瞬间
    });
};

onMounted(() => {
    // 页面滚动窗口监听事件
    window.onscroll = function () {
        // 获取浏览器卷去的高度
        let high = document.documentElement.scrollTop || document.body.scrollTop; //兼容各浏览器
        if (high >= 500) {
            totop.value.style.display = "block";
        } else {
            totop.value.style.display = "none";
        }
    };
});
</script>
   
<style scoped lang="scss">
.totop {
    display: none;
    position: fixed;
    bottom: 120px;
    right: 80px;
    background-color: #b0b0b0;
    padding: 10px;
    border-radius: 50%;

    &:hover {
        cursor: pointer;
        background-color: #00000088;
    }
}
</style>

然后直接在页面中使用<BackTop></BackTop>即可

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,我们可以实现一个基于Vue一键回到顶部按钮,具体实现过程如下: 1. 在Vue组件中添加一个按钮元素,用于触发回到顶部事件。 2. 在Vue组件的data中添加一个变量isShowBackTop,用于控制回到顶部按钮的显示和隐藏。 3. 在Vue组件的methods中添加一个backTop方法,用于实现回到顶部的操作。在该方法中,我们可以使用window.scrollTo方法将页面滚动到顶部,并添加一个定时器,实现滚动的动画效果。 4. 在Vue组件的mounted生命周期函数中,添加一个滚动事件监听器,用于判断是否需要显示回到顶部按钮。当页面滚动高度大于屏幕高度时,将isShowBackTop变量设置为true,否则设置为false。 下面是一个示例代码: ```vue <template> <div> <!-- 回到顶部按钮 --> <div v-show="isShowBackTop" class="back-top" @click="backTop"> <i class="iconfont icon-top"></i> </div> <!-- 页面内容 --> <div class="content"> ... </div> </div> </template> <script> export default { data() { return { isShowBackTop: false // 控制回到顶部按钮的显示和隐藏 } }, methods: { backTop() { // 回到顶部操作 let currentScroll = document.documentElement.scrollTop || document.body.scrollTop if (currentScroll > 0) { window.requestAnimationFrame(this.backTop) window.scrollTo(0, currentScroll - (currentScroll / 5)) } } }, mounted() { // 滚动事件监听器 window.addEventListener('scroll', () => { let scrollTop = document.documentElement.scrollTop || document.body.scrollTop this.isShowBackTop = scrollTop > window.innerHeight }) } } </script> <style> .back-top { position: fixed; right: 50px; bottom: 50px; width: 50px; height: 50px; line-height: 50px; text-align: center; background-color: #ccc; border-radius: 50%; cursor: pointer; } .icon-top { font-size: 24px; } </style> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值