element-ui BackTop改造, 增加回到底部

<template>
  <div>
    <transition name="el-fade-in">
      <div
        v-if="toTopVisible"
        @click.stop="handleTopClick"
        :style="{
        'right': styleRight,
        'bottom': styletopBottom
      }"
        class="el-backtop">
        <slot name="top">
          <el-icon name="caret-top"></el-icon>
        </slot>
      </div>
    </transition>
    <transition name="el-fade-in">
      <div
        v-if="toBottomVisible"
        @click.stop="handleBottomClick"
        :style="{
        'right': styleRight,
        'bottom': stylebotBottom
      }"
        class="el-backtop">
        <slot name="bottom">
          <el-icon name="caret-bottom"></el-icon>
        </slot>
      </div>
    </transition>
  </div>
</template>

<script>
import throttle from 'throttle-debounce/throttle';

const cubic = value => Math.pow(value, 3);
const easeInOutCubic = value => value < 0.5
  ? cubic(value * 2) / 2
  : 1 - cubic((1 - value) * 2) / 2;

export default {
  name: 'GoTopAndBottom',

  props: {
    goTopVisibilityHeight: {
      type: Number,
      default: 200
    },
    tobotVisibilityHeight: {
      type: Number,
      default: 200
    },
    target: [String],
    right: {
      type: Number,
      default: 40
    },
    bottom: {
      type: Number,
      default: 40
    }
  },

  data() {
    return {
      el: null,
      container: null,
      toTopVisible: false,
      toBottomVisible:true,
    };
  },

  computed: {
    styletopBottom() {
      return `${this.bottom+25}px`;
    },
    styleRight() {
      return `${this.right}px`;
    },
    stylebotBottom() {
      return `${this.bottom-25}px`;
    },
  },

  mounted() {
    this.init();
    this.throttledScrollHandler = throttle(300, this.onScroll);
    this.container.addEventListener('scroll', this.throttledScrollHandler);
    this.onScroll();
  },

  methods: {
    init() {
      this.container = document;
      this.el = document.documentElement;
      if (this.target) {
        this.el = document.querySelector(this.target);
        if (!this.el) {
          throw new Error(`target is not existed: ${this.target}`);
        }
        this.container = this.el;
      }
    },
    onScroll() {

      const scrollTop = this.el.scrollTop;
      this.toTopVisible = scrollTop >= this.goTopVisibilityHeight;
      this.toBottomVisible = (scrollTop + document.documentElement.clientHeight) <= (this.el.scrollHeight-this.tobotVisibilityHeight);
    },
    handleTopClick(e) {
      this.scrollToTop();
      this.$emit('toTopClick', e);
    },
    handleBottomClick(e) {
      this.scrollToTBottom();
      this.$emit('toBotClick', e);
    },
    scrollToTop() {
      const el = this.el;
      const beginTime = Date.now();
      const beginValue = el.scrollTop;
      const rAF = window.requestAnimationFrame || (func => setTimeout(func, 16));
      const frameFunc = () => {
        const progress = (Date.now() - beginTime) / 500;
        if (progress < 1) {
          el.scrollTop = beginValue * (1 - easeInOutCubic(progress));
          console.log(beginValue);
          rAF(frameFunc);
        } else {
          el.scrollTop = 0;
        }
      };
      rAF(frameFunc);
    },
    scrollToTBottom() {
      const el = this.el;
      const beginTime = Date.now();
      const beginValue = el.scrollTop;
      const endScrollTop = el.scrollHeight-document.documentElement.clientHeight;
      const rAF = window.requestAnimationFrame || (func => setTimeout(func, 16));
      const frameFunc = () => {
        const progress = (Date.now() - beginTime) / 500;

        if (progress < 1) {
          el.scrollTop = (endScrollTop-beginValue) * easeInOutCubic(progress) + beginValue;
          rAF(frameFunc);
        } else {
          el.scrollTop = endScrollTop;
        }
      };
      rAF(frameFunc);
    }
  },

  beforeDestroy() {
    this.container.removeEventListener('scroll', this.throttledScrollHandler);
  }
};
</script>

改造element 中BackTop , 写成固定组件, 通过组件调用 ,页面效果与element区别不大。

引用组件:

import GoTopAndBottom from "@/components/GoTopAndBottom/index";

 效果图:
页面顶部

 

页面中段

 

页面底部:

 

转自:vue element-ui BackTop,el-backtop 改造,element 底部,拉到底部_奇怪的怪奇的博客-CSDN博客

  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
el-scrollbar和el-backtop是Element Plus库中的两个组件,用于实现滚动条和回到顶部功能。 el-scrollbar是一个自定义滚动条组件,用于在需要滚动的容器中显示自定义的滚动条。它可以通过设置不同的属性来控制滚动条的样式和行为。 el-backtop是一个回到顶部的组件,当页面滚动时,它会显示在页面的某个位置,并在点击时将页面滚动到顶部。 在使用el-backtop组件时,需要注意设置target属性。这个属性指定了要监听滚动的元素。如果页面中有多个滚动元素,需要将target属性设置为正确的元素选择器,以确保el-backtop能够正确监听滚动事件。 下面是一个使用el-scrollbar和el-backtop的示例: ```html <template> <div class="container"> <el-scrollbar wrap-class="scroll-wrap"> <!-- 这里是需要滚动的内容 --> <!-- ... --> </el-scrollbar> <el-backtop target=".scroll-wrap"></el-backtop> </div> </template> <script> export default { // ... } </script> <style> .container { height: 400px; /* 设置容器的高度,使其出现滚动条 */ position: relative; } .scroll-wrap { height: 100%; /* 设置滚动内容的高度,使其撑开容器 */ } </style> ``` 在上面的示例中,我们将el-scrollbar和el-backtop放在一个容器中。容器的高度设置为400px,使其出现滚动条。el-scrollbar的wrap-class属性设置为"scroll-wrap",这样我们可以通过选择器".scroll-wrap"来指定el-backtop的target属性。 这样,当页面滚动时,el-backtop会显示在容器的右下角,并在点击时将页面滚动到顶部。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值