• vue封装返回顶部组件【cv可用】


    一、博主介绍

    💒首页:水香木鱼

    🛫专栏:后台管理系统

    简介: 博主:花名 “水香木鱼”,星座"水瓶座一枚"

    🔰 格言: 生活是一面镜子。 你对它笑, 它就对你笑; 你对它哭, 它也对你哭。

    🔋 充电:相信付出终将会得到回报!


    二、笔芯文章

    可直接粘贴走,拿去直接用即可。

    (1)、封装BackTop.vue组件

    
    <template>
      <transition :name="transitionName">
        <div
          v-show="visible"
          :style="customStyle"
          class="back-to-ceiling"
          @click="backToTop"
        >
          <svg
            width="16"
            height="16"
            viewBox="0 0 17 17"
            xmlns="http://www.w3.org/2000/svg"
            class="Icon Icon--backToTopArrow"
            aria-hidden="true"
            style="height: 16px; width: 16px"
          >
            <path
              d="M12.036 15.59a1 1 0 0 1-.997.995H5.032a.996.996 0 0 1-.997-.996V8.584H1.03c-1.1 0-1.36-.633-.578-1.416L7.33.29a1.003 1.003 0 0 1 1.412 0l6.878 6.88c.782.78.523 1.415-.58 1.415h-3.004v7.004z"
            />
          svg>
        div>
      transition>
    template>
    
    <script>
    export default {
      name: "BackTop",
      props: {
        visibilityHeight: {
          type: Number,
          default: 400,
        },
        backPosition: {
          type: Number,
          default: 0,
        },
        customStyle: {
          type: Object,
          default: function () {
            return {
              right: "50px",
              bottom: "50px",
              width: "40px",
              height: "40px",
              "border-radius": "4px",
              "line-height": "45px",
              background: "#e7eaf1",
            };
          },
        },
        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 scoped>
    .back-to-ceiling {
      position: fixed;
      display: inline-block;
      text-align: center;
      cursor: pointer;
    }
    
    .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: #245CDC;
      background: none;
    }
    style>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130

    (2)、使用BackTop.vue组件

    注意:在数据多的时候会显示出来

    在这里插入图片描述

    <template>
    	<div>
    		<ul>
    			<li>测试文本li>
    			<li>测试文本li>
    			<li>测试文本li>
    			<li>测试文本li>
    			<li>测试文本li>
    		ul>
    		
    		<BackTop>BackTop>
    	div>
    template>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    <script>
    import BackTop from "@/components/BackTop.vue";
    export default {
      components: {
        BackTop,
      },
    }
    </script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    三、博主致谢

    感谢小伙伴们,耐心的阅读完本篇文章,关注👉 水香木鱼🔔 获取更多精彩文章!

    ⭐⭐⭐ 做为小可爱的你,别忘记一键三连呦!

  • 相关阅读:
    【桶计数】面试题 01.02. 判定是否互为字符重排
    项目管理软件dhtmlxGantt配置教程(十五):突出展示时间段
    如何处理Flutter应用在iOS平台上的兼容性问题
    Hadoop Streaming使用简介
    【小尘送书-第五期】《巧用ChatGPT快速提高职场晋升力》用ChatGPT快速提升职场能力,全面促进自身职业发展
    Xcode预览(Preview)显示List视图内容的一个Bug及解决
    获取钉钉机器人的token及secret
    C++之template的简单介绍
    面试题总结 20231024
    Java编程技巧:Excel导入、导出(支持EasyExcel和EasyPoi)
  • 原文地址:https://blog.csdn.net/weixin_48337566/article/details/126750173