0
点赞
收藏
分享

微信扫一扫

返回顶部滚动事件

Separes 2022-01-05 阅读 47
wpfp2plinq


滚动顶部事件
事先为window开启滚动事件,这个滚动事件里,来获取当前的滚动高度

mounted() {
    window.onscroll = () => {
      this.add();
    };
  },
  

add() {
      let body = document.body;
      let html = document.documentElement;
      let ht = body || html;
      this.top = ht.scrollTop;
 },
监听高度的变化,来判断返回按钮的显示隐藏

watch: {
    top(newVal, oldVal) {
      if (newVal > 200) {
        this.show = true;
      } else {
        this.show = false;
      }
    }
  },
点击返回顶部按钮时,设置均匀滚动到头部

 goskip() {
      window.scroll({ top: 0, behavior: "smooth" });
    }

举报

相关推荐

0 条评论