vue开发公共组件之返回顶部

 更新时间:2022年01月06日 09:52:32   作者:一键写代码  
这篇文章主要为大家详细介绍了vue开发公共组件之返回顶部,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了vue开发公共组件之返回顶部的具体代码,供大家参考,具体内容如下

记录一下开发公共组件的流程。

背景:pc端使用element-ui框架,本身是有返回顶部的组件的。现在需要在移动端使用。照着葫芦画瓢弄一个。
记录如何将公共组件通过install的方式,注册为全局的组件使用。

components目录下,新建bacttop文件夹,内部包含一个index.js文件和一个src文件夹。
src文件夹内放backtop.vue组件文件。

|--components
   |--index.js
   |-- backtop
       |--index.js
       |--src
             |--backtop.vue

backtop下的index.js负责安装,backtop.vue内部写具体的组件代码

index.js文件内容:

// index.js
import Backtop from "./src/backtop"; // 引入组件

// 配置安装方法
/* istanbul ignore next */
Backtop.install = function (Vue) {
  Vue.component(Backtop.name, Backtop);
};

// 导出模块
export default Backtop;

backtop.vue文件内容:

<template>
 <!-- xl-backtop样式名,需要自己在样式文件中定义这个样式内容 -->
  <div
    v-if="visible"
    @click.stop="handleClick"
    :style="{
      right: styleRight,
      bottom: styleBottom,
    }"
    class="xl-backtop"
  >
    <slot>
    <!-- 这里是返回顶部的图标 -->
      <van-icon name="arrow-up" />
    </slot>
  </div>
</template>

<script>
// 这里引入了节流函数
import { _throttle } from "@/utils";

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: "XlBacktop",

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

  data() {
    return {
      el: null,
      container: null,
      visible: false,
    };
  },

  computed: {
    styleBottom() {
      return `${this.bottom}px`;
    },
    styleRight() {
      return `${this.right}px`;
    },
  },

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

  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.visible = scrollTop >= this.visibilityHeight;
    },
    handleClick(e) {
      this.scrollToTop();
      this.$emit("click", 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));
          rAF(frameFunc);
        } else {
          el.scrollTop = 0;
        }
      };
      rAF(frameFunc);
    },
  },

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

返回顶部的样式内容:

// 返回顶部
.xl-backtop {
   position: fixed;
  width: 40px;
  height: 40px;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 20px;
  cursor: pointer;
  box-shadow: 0 0 6px rgba(0, 0, 0, 0.12);
  border-radius: 50%;
  z-index: 5;
}

为了一次性注册多个自己写的功能组件,我们在components文件夹下面写一个index.js

components下的index负责一次性组合多个

// components/index.js
import BackTop from "./backtop"; // 引入我们的返回顶部组件。其他的类似的一起写在这里
const components = [BackTop]; // 其他的组件以数组形式继续写
const install = function (Vue, opts = {}) {
  components.map((component) => {
    Vue.component(component.name, component);
  });
};
/* istanbul ignore if */
if (typeof window !== "undefined" && window.Vue) {
  install(window.Vue);
}
// 组合导出安装方法
const exportsResult = {
  version: "1.0.0",
  install,
};
Object.assign(exportsResult, components);

export default exportsResult;

最后在项目的main.js安装

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from "vue";
import App from "./App";
import store from "./store";
import router from "./router";


// 自己封装的公共安装组件
import XlComponent from "@/components";
Vue.use(XlComponent);


import "@/styles/index.less"; // 全局 css

Vue.config.productionTip = false;

/* eslint-disable no-new */
new Vue({
  el: "#app",
  router,
  store,
  components: { App },
  template: "<App/>",
});

补充:上述方法在安卓端会失效

原因是document.documentElement.scrollTop在安卓端始终是0;只有pc端和IOS端才是正常的。

改写backtop组件中的代码,完成兼容。

<template>
  <transition name="van-fade">
    <div
      v-if="visible"
      @click.stop="handleClick"
      :style="{
        right: styleRight,
        bottom: styleBottom,
      }"
      class="xl-backtop"
    >
      <slot>
        <van-icon name="arrow-up" color="#13b7f6" />
      </slot>
    </div>
  </transition>
</template>

<script>
import { _throttle } from "@/utils";

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: "XlBacktop",

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

  data() {
    return {
      el: null,
      container: null,
      visible: false,
    };
  },

  computed: {
    styleBottom() {
      return `${this.bottom}px`;
    },
    styleRight() {
      return `${this.right}px`;
    },
  },

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

  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() {
    // 这里,如果document.documentElement.scrollTop 的值为0,就获取document.body.scrollTop
      const scrollTop = this.el.scrollTop || document.body.scrollTop;
      this.visible = scrollTop >= this.visibilityHeight;
    },
    handleClick(e) {
      this.scrollToTop();
      this.$emit("click", e);
    },
    scrollToTop() {
    // 还有这里,如果document.documentElement.scrollTop 的值为0,就获取document.body元素
      const el = this.el.scrollTop? this.el : document.body;
      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));
          rAF(frameFunc);
        } else {
          el.scrollTop = 0;
        }
      };
      rAF(frameFunc);
    },
  },

  beforeDestroy() {
    this.container.removeEventListener("scroll", this.throttledScrollHandler);
    this.el = null;
  },
};
</script>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • vue forEach循环数组拿到自己想要的数据方法

    vue forEach循环数组拿到自己想要的数据方法

    今天小编就为大家分享一篇vue forEach循环数组拿到自己想要的数据方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-09-09
  • 源码分析Vue3响应式核心之effect

    源码分析Vue3响应式核心之effect

    这篇文章主要为大家详细介绍了Vue3响应式核心之effect的相关知识,文中的示例代码讲解详细,对我们学习Vue3有一定的帮助,需要的可以参考一下
    2023-04-04
  • 详解element-ui表格中勾选checkbox,高亮当前行

    详解element-ui表格中勾选checkbox,高亮当前行

    这篇文章主要介绍了详解element-ui表格中勾选checkbox,高亮当前行,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-09-09
  • Vue.js中让人容易忽略的API详解

    Vue.js中让人容易忽略的API详解

    Vue.js是构建Web界面的渐进式JavaScript框架,通过简洁的 API 提供高效的数据绑定和灵活的组件系统,这篇文章主要给大家介绍了关于Vue.js中让人容易忽略的API的相关资料,需要的朋友可以参考下
    2021-09-09
  • vue下的@change事件的实现

    vue下的@change事件的实现

    这篇文章主要介绍了vue下的@change事件的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-10-10
  • vue中解决异步交互数据出现延迟问题

    vue中解决异步交互数据出现延迟问题

    这篇文章主要介绍了vue中解决异步交互数据出现延迟问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2025-04-04
  • 深入浅析Vue全局组件与局部组件的区别

    深入浅析Vue全局组件与局部组件的区别

    这篇文章主要介绍了Vue全局组件与局部组件的区别,通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2018-06-06
  • 单页面Vue页面刷新出现闪烁问题及解决

    单页面Vue页面刷新出现闪烁问题及解决

    这篇文章主要介绍了单页面Vue页面刷新出现闪烁问题及解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-07-07
  • VueX模块的具体使用(小白教程)

    VueX模块的具体使用(小白教程)

    这篇文章主要介绍了VueX模块的具体使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-06-06
  • VUE表达式{{}}中如何拼接字符

    VUE表达式{{}}中如何拼接字符

    这篇文章主要介绍了VUE表达式{{}}中如何拼接字符问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-07-07

最新评论