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

简介: 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>
————————————————
版权声明:本文为CSDN博主「前端江太公」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/ZiChen_Jiang/article/details/108791224

父组件

<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>

效果

2345_image_file_copy_10.jpg

目录
相关文章
|
1天前
|
JavaScript API UED
Vue3中的Suspense组件有什么用?
Vue3中的Suspense组件有什么用?
15 6
|
1天前
|
JavaScript 前端开发
vue3中使用动态组件
vue3中使用动态组件
10 0
|
1天前
|
JavaScript 前端开发 容器
Vue 3 中 <transition-group> 组件报错的非 props 属性传递问题
Vue 3 中 <transition-group> 组件报错的非 props 属性传递问题
14 1
|
1天前
|
移动开发 JavaScript 前端开发
ruoyi-nbcio-plus基于vue3的flowable的自定义业务显示历史信息组件的升级修改
ruoyi-nbcio-plus基于vue3的flowable的自定义业务显示历史信息组件的升级修改
|
1天前
|
移动开发 前端开发
ruoyi-nbcio-plus基于vue3的flowable的自定义业务撤回申请组件的升级修改
ruoyi-nbcio-plus基于vue3的flowable的自定义业务撤回申请组件的升级修改
12 2
|
1天前
|
移动开发 前端开发
ruoyi-nbcio-plus基于vue3的flowable流程查看器组件的升级修改
ruoyi-nbcio-plus基于vue3的flowable流程查看器组件的升级修改
11 1
|
1天前
|
移动开发 前端开发
vue3 Element-Plus封装的el-tree-select的使用
vue3 Element-Plus封装的el-tree-select的使用
vue3 Element-Plus封装的el-tree-select的使用
|
1天前
|
JavaScript
vue打印v-model 的值
vue打印v-model 的值
|
1天前
|
移动开发 前端开发 JavaScript
VUE3内置组件Transition的学习使用
VUE3内置组件Transition的学习使用
|
1天前
|
JavaScript
Vue实战-组件通信
Vue实战-组件通信
7 0