Vue3一键回到顶部/底部

作者 : admin 本文共638个字,预计阅读时间需要2分钟 发布时间: 2024-06-9 共1人阅读

效果图:

 Vue3一键回到顶部/底部插图

 示例代码:


  
    这是第一个盒子
    这是第二个盒子
    这是第三个盒子
    这是第四个盒子

    返回顶部
  





.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;
    }
  }
}

回到底部:

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

封装BackTop.vue组件

 Vue3一键回到顶部/底部插图(1)

 components/BackTop.vue: 


    
        
            
        
    

   

   

.totop {
    display: none;
    position: fixed;
    bottom: 120px;
    right: 80px;
    background-color: #b0b0b0;
    padding: 10px;
    border-radius: 50%;

    &:hover {
        cursor: pointer;
        background-color: #00000088;
    }
}

然后直接在页面中使用即可

本站无任何商业行为
个人在线分享 » Vue3一键回到顶部/底部
E-->