zoukankan      html  css  js  c++  java
  • vue 回到顶部效果实现

     <div class="goPageTop">
        <img
          v-if="btnFlag"
          class="go-top"
          src="@/assets/images/Back_top.png"
          @click="backTop"
        />
      </div>
    export default {
      name: "",
      components: {},
      props: {},
      data() {
        return {
            btnFlag:false
        };
      },
      computed: {},
      watch: {},
      created() {},
      mounted() {
        window.addEventListener("scroll", this.scrollToTop);
      },
    
      destroyed() {
        window.removeEventListener("scroll", this.scrollToTop);
      },
      methods: {
        backTop() {
          const that = this;
          let timer = setInterval(() => {
            let ispeed = Math.floor(-that.scrollTop / 5);
            document.documentElement.scrollTop = document.body.scrollTop =
              that.scrollTop + ispeed;
            if (that.scrollTop === 0) {
              clearInterval(timer);
            }
          }, 16);
        },
    
        scrollToTop() {
          const that = this;
          let scrollTop =
            window.pageYOffset ||
            document.documentElement.scrollTop ||
            document.body.scrollTop;
          that.scrollTop = scrollTop;
          if (that.scrollTop > 60) {
            that.btnFlag = true;
          } else {
            that.btnFlag = false;
          }
        },
      },
    };
    .goPageTop img {
       40px;
      height: 40px;
      position: fixed;
      right: 10px;
      bottom: 60px;
      z-index: 9;
    }
  • 相关阅读:
    Python基础四
    Python基础三
    Python基础二
    Python基础一
    JAVA测试
    国庆随笔
    ATM-JAVA程序 //程序有5处相同错误,找不出原因 转账功能没有实现,修改密码来不及实现了
    JAVA程序测试感受
    第八周
    第七周
  • 原文地址:https://www.cnblogs.com/yugueilou/p/14066435.html
Copyright © 2011-2022 走看看