实现vue返回顶部backTop组件

在一些页面比较长的时候,可能会用到回到顶部功能,一般都是右下角一个图标我们点击可以返回到顶部,这个功能还是蛮常见的在一些网页中,这个功能看起来还是蛮简单的,我们可以自由发挥。

./components/BackToTop/index.vue组件

<template>
  <el-tooltip placement="top" content="返回顶部">
    <transition :name="transitionName">
      <div v-show="visible" :style="customStyle" class="back-to-ceiling" @click="backToTop">
        <svg
          focusable="false"
          class=""
          data-icon="vertical-align-top"
          width="1.3em"
          height="1.3em"
          fill="currentColor"
          aria-hidden="true"
          viewBox="64 64 896 896"
        >
          <path
            d="M859.9 168H164.1c-4.5 0-8.1 3.6-8.1 8v60c0 4.4 3.6 8 8.1 8h695.8c4.5 0 8.1-3.6 8.1-8v-60c0-4.4-3.6-8-8.1-8zM518.3 355a8 8 0 00-12.6 0l-112 141.7a7.98 7.98 0 006.3 12.9h73.9V848c0 4.4 3.6 8 8 8h60c4.4 0 8-3.6 8-8V509.7H624c6.7 0 10.4-7.7 6.3-12.9L518.3 355z"
          ></path>
        </svg>
      </div>
    </transition>
  </el-tooltip>
</template>
<script>
export default {
  name: "BackToTop",
  props: {
    // 距离顶部多远出现按钮
    visibilityHeight: {
      type: Number,
      default: 100
    },
    // 返回顶部后离顶部的距离
    backPosition: {
      type: Number,
      default: 0
    },
    customStyle: {
      type: Object,
      default: function () {
        return {
          "right": "40px",
          "bottom": "40px",
          "width": "40px",
          "height": "40px",
          "border-radius": "20px",
          "line-height": "50px",
          "background-color": "#00000073"
        };
      }
    },
    transitionName: {
      type: String,
      default: "fade"
    }
  },
  data() {
    return {
      visible: false,
      interval: null,
      isMoving: false
    };
  },
  mounted() {
    window.addEventListener("scroll", this.handleScroll);
  },
  beforeDestroy() {
    window.removeEventListener("scroll", this.handleScroll);
    if (this.interval) {
      clearInterval(this.interval);
    }
  },
  methods: {
    handleScroll() {
      this.visible = window.pageYOffset > this.visibilityHeight;
    },
    backToTop() {
      if (this.isMoving) return;
      const start = window.pageYOffset;
      let i = 0;
      this.isMoving = true;
      this.interval = setInterval(() => {
        const next = Math.floor(this.easeInOutQuad(10 * i, start, -start, 500));
        if (next <= this.backPosition) {
          window.scrollTo(0, this.backPosition);
          clearInterval(this.interval);
          this.isMoving = false;
        } else {
          window.scrollTo(0, next);
        }
        i++;
      }, 16.7);
    },
    easeInOutQuad(t, b, c, d) {
      if ((t /= d / 2) < 1) return (c / 2) * t * t + b;
      return (-c / 2) * (--t * (t - 2) - 1) + b;
    }
  }
};
</script>
<style lang="scss" scoped>
.back-to-ceiling {
  position: fixed;
  display: inline-block;
  text-align: center;
  cursor: pointer;
  overflow: hidden;
  transition: all .3s;
  z-index: 999;
  color: #fff
}
.back-to-ceiling:hover {
  background: #d5dbe7;
}
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.5s;
}
.fade-enter,
.fade-leave-to {
  opacity: 0;
}
.back-to-ceiling .Icon {
  fill: #9aaabf;
  background: none;
}
</style>

mian.js全局引入

// 返回顶部
import BackToTop from "@/components/BackToTop";
Vue.component("back-to-top", BackToTop);

App.vue全局添加

<template>
  <div id="app">
      <router-view />
      <!-- 返回顶部 -->
      <back-to-top/>
  </div>
</template>

效果图
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值