vue中返回顶部封装的组件 滚动一定位置显示隐藏

用两个不同方式写的返回顶部
返回顶部子组件1

<template>
  <div>
    <div @click="backtop" class="fh" v-show="isShow">顶部1</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isShow: false,
    };
  },
  mounted() {
    this.listenerFunction();
  },
  methods: {
    listenerFunction(e) {
      document.addEventListener("scroll", this.handleScroll, true);
    },
    beforeDestroy() {
      document.removeEventListener("scroll", this.listenerFunction);
    },
    handleScroll() {
      // handleScroll 和 methods 是同级
      if (window.pageYOffset > 300) {
        //window.pageYOffset:获取滚动距离
        this.isShow = true;
      } else {
        this.isShow = false;
      }
    },
    //返回顶部
    backtop() {
      let top = document.documentElement.scrollTop || document.body.scrollTop;
      // 实现滚动效果
      const timeTop = setInterval(() => {
        document.body.scrollTop = document.documentElement.scrollTop = top -= 50;
        if (top <= 0) {
          clearInterval(timeTop);
        }
      }, 10);
    },
  },
};
</script>

<style lang="scss" scoped>
.fh {
  width: 50px;
  height: 50px;
  border: 1px solid;
  position: fixed;
  bottom: 50px;
  left: 20px;
}
</style>

返回顶部子组件2

<template>
  <div>
    <!-- 使用超链接锚点定位回到顶部 没动画效果 -->
    <div @click="back" class="back1" v-show="isShow">顶部2</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isShow: false,
    };
  },
  mounted() {
    this.listenerFunction();
  },

  methods: {
    listenerFunction(e) {
      document.addEventListener("scroll", this.handleScroll, true);
    },
    beforeDestroy() {
      document.removeEventListener("scroll", this.listenerFunction);
    },
    handleScroll() {
      // handleScroll 和 methods 是同级
      if (window.pageYOffset > 300) {
        //window.pageYOffset:获取滚动距离
        this.isShow = true;
      } else {
        this.isShow = false;
      }
    },

    back() {
      //返回顶部 $这个地方需要引入在线jq
      $("html").stop().animate(
        //animate:动画 点击时让它行动
        {
          scrollTop: 0, //距离顶部为0
        },
        1000 //多少时间完成行动
      );
    },
  },
};
</script>

<style lang="scss" scoped>
.back1 {
  width: 50px;
  height: 50px;
  border: 1px solid;
  position: fixed;
  bottom: 50px;
  left: 183px;
}
</style>

父组件

<template>
  <div class="about">
    <ul>
      <li v-for="(item, index) in 100" :key="index">{{ item }}</li>
    </ul>
    <a href="#">顶部3</a>

    <FH1 />
    <FH2 />
  </div>
</template>

<script>
import FH1 from "../components/dingbu/FH1";  //导入
import FH2 from "../components/dingbu/FH2";
export default {
  components: {
    FH1,
    FH2,
  },
  data() {
    return {
  
    };
  },

  methods: {

  },
  created() {

  },
};
</script>

<style lang="scss" scoped>
ul {
  margin: 0;
  padding: 0;
}
li {
  height: 50px;
  border-bottom: 1px solid;
  list-style: none;
  margin: 0;
}
a {
  width: 50px;
  height: 50px;
  border: 1px solid;
  position: fixed;
  bottom: 50px;
  right: 20px;
}
</style>

效果
在这里插入图片描述

  • 11
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

江一铭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值