vue 回到顶部 backTop(自定义组件)

在这里插入图片描述

使用方式

<back-top :topHeight = 'topHeight' ,/>

使用参数

参数类型是否必填默认描述
topHeightNumber500初始出现的高度
speedNumber10初始返回的速度
setSpeedNumber20加速度
rightNumber60距离浏览器右侧的距离
bottomNumber80距离游览器下面的距离

源代码

<template>
  <div class="backTop" v-show="toTopShow" @click="handleToTop" 
  :style="{right: right + 'px', bottom: bottom +'px',}">
    <img src="../assets/img/up.png">
  </div>
</template>

<script>
  export default {
    data() {
      return {
        toTopShow: false,
        scrollTop: 0
      }
    },
    props: {
      // 开始出现的高度
      topHeight: {
        type: Number,
        default: 500
      },
      // 开始速度
      speed: {
        type: Number,
        default: 10
      },
      // 叠加速度
      setSpeed: {
        type: Number,
        default: 20
      },
      right: {
        type: Number,
        default: 60
      },
      bottom: {
        type: Number,
        default: 80
      }
    },
    mounted () {
      const self = this
      window.addEventListener('scroll', this.debounce(function () {
        self.handleScrolls()
      }, 300))
    },
    methods: {
      // 监控防抖
      debounce(fn, wait) {
        let timer = null
        return function () {
          if (timer) {
            clearTimeout(timer)
          }
          timer = setTimeout(fn, wait)
        }
      },
      // 处理滚动条的监控
      handleScrolls() {
        const scrollTop = document.documentElement.scrollTop || document.body.scrollTop
        this.scrollTop = scrollTop
        if (scrollTop > this.topHeight) {
          this.toTopShow = true
        } else {
          this.toTopShow = false
        }
      },
      // 处理回到开始位置
      handleToTop() {
        let speed = this.speed
        const setSpeed = this.setSpeed
        const self = this
        const timer = setInterval(function() {
          self.scrollTop = document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop
          if (self.scrollTop > 0) {
            self.scrollTop = (self.scrollTop - speed > 0) ? (self.scrollTop - speed) : 0
            speed += setSpeed
            window.scrollTo(0, self.scrollTop)
          } else {
            clearInterval(timer)
          }
        }, 60)
      }
    }
  }
</script>

<style lang="scss" rel="stylesheet/scss" scoped>
  .backTop {
    width: 60px;
    height: 60px;
    background-color: #2C2C2C;
    border-radius: 50%;
    position: fixed;
    /*right: 60px;*/
    /*bottom: 80px;*/
    text-align: center;
    padding-top: 2px;
    cursor: pointer;
    /* 动画 */
    transition: 0.6s;
    img {
      text-align: center;
    }
  }
  .backTop:hover {
    background-color: #868880;
  }
</style>

如果感觉这篇文章对你有帮助,加个关注吧!!!

也可关注我的公众号,我们一起交流。

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值