vue移动端实现滚动到顶部

12 篇文章 0 订阅
4 篇文章 0 订阅
<template>
  <div class="home">
    <div @scroll="scroll()">
      <div v-for="(item,index) in this.list" :key="index" class="every">{{item}}</div>
    </div>
    <div class="scrolltop" v-show="isShow" @click="goTop">返回顶部</div>
  </div>
</template>

<script>
export default {
  name: "Home",
  components: {},
  data: function() {
    return {
      list: [1, 2, 3, 4, 5, 6, 7, 8],
      isShow: false //默认弹出框是隐藏状态
    };
  },
  mounted() {
    window.addEventListener("scroll", () => {
      let scrollHeight = document.documentElement.scrollHeight;
      console.log("页面的总体高度", scrollHeight);
      let clientHeight = document.documentElement.clientHeight;
      console.log("页面的 可视区域", clientHeight);
      //获取可是区域的距离
      let scrollTop = parseInt(document.documentElement.scrollTop);
      console.log(scrollTop, "距离页面最顶部的距离");
      // 当我们的滚动距离加上也是区域
      if (scrollTop > 400) {
        this.isShow = true;
      }
    });
  },
  methods: {
    goTop() {
      document.documentElement.scrollTop = 0;
      this.isShow = false;
    }
  }
};
</script>
<style scoped>
.home {
  width: 100%;
  height: 1800px;
  /* background-color: palegreen; */
  position: relative;
}
* {
  margin: 0;
  padding: 0;
}
.every {
  width: 100%;
  height: 150px;
  border: 1px solid red;
}
.scrolltop {
  width: 50px;
  height: 50px;
  border: 1px solid red;
  font-size: 12px;
  line-height: 50px;
  position: absolute;
  top: 800px;
  right: 10px;
}
</style>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值