css实现拖拽悬浮球组件

刚好最近在开发这个效果,直接拿去复制就可以用,之后再遇到这种,可以直接拿代码~ 有需要的可以拿

<!-- 可拖拽的小球 封装 -->
<template>
  <div
    class="backHome"
    ref="floatButton"
    @click="goCreatePage"
    :style="{
      width: itemWidth + 'px',
      height: itemHeight + 'px',
      left: left + 'px',
      top: top + 'px',
    }"
  >
    <img src="" alt="" />
  </div>
</template>
 
<script>
export default {
  name: "BackHome",
  props: {
    itemWidth: {
      // 悬浮按钮宽度
      type: Number,
      default: 40,
    },
    itemHeight: {
      // 悬浮按钮高度
      type: Number,
      default: 50,
    },
    gapWidth: {
      // 距离左右两边距离
      type: Number,
      default: 20,
    },
    coefficientHeight: {
      // 从上到下距离比例
      type: Number,
      default: 0.72,
    },
  },
  data() {
    return {
      ssicId: "",
      clientWidth: 0,
      clientHeight: 0,
      timer: null,
      currentTop: 0,
      left: 0,
      top: 0,
    };
  },
  created() {
    // this.ssicId = sessionStorage.getItem("ssicId");
    this.clientWidth = document.documentElement.clientWidth;
    this.clientHeight = document.documentElement.clientHeight;
    this.left = this.clientWidth - this.itemWidth - this.gapWidth;
    this.top = this.clientHeight * this.coefficientHeight;
  },
  methods: {
    // 返回首页菜单
    goCreatePage() {
      this.$emit("goManage");
      // location.href = ``;
    },
    handleScrollEnd() {
      let scrollTop =
        document.documentElement.scrollTop || document.body.scrollTop;
      if (scrollTop === this.currentTop) {
        if (this.left > this.clientWidth / 2) {
          this.left = this.clientWidth - this.itemWidth - this.gapWidth;
        } else {
          this.left = this.gapWidth;
        }
      }
      clearTimeout(this.timer);
    },
  },
  mounted() {
    this.$nextTick(() => {
      const floatButton = this.$refs.floatButton;
      floatButton.addEventListener("touchstart", () => {
        floatButton.style.transition = "none";
      });
      // 在拖拽过程中,组件应该跟随手指的移动而移动
      floatButton.addEventListener("touchmove", (e) => {
        if (e.targetTouches.length === 1) {
          // 一根手指
          let touch = e.targetTouches[0];
          this.left = touch.clientX - 20;
          this.top = touch.clientY - 25;
        }
      });
      // 拖拽结束后,重新调整组件的位置并重新设置过渡动画
      floatButton.addEventListener("touchend", () => {
        floatButton.style.transition = "all 0.3s";
        if (this.left > document.documentElement.clientWidth / 2) {
          this.left = document.documentElement.clientWidth - 60;
        } else {
          this.left = 0;
        }
      });
    });
  },
  beforeDestroy() {
    window.removeEventListener("scroll", this.handleScrollStart);
  },
};
</script>
 
<style lang="scss" scoped>
.backHome {
  position: fixed;
  bottom: 165px;
  right: 15px;
  z-index: 999;
  width: 61px;
  height: 61px;
  img {
    width: 61px;
    height: 61px;
  }
}
</style>

相关推荐

  1. css实现悬浮组件

    2024-04-24 11:04:03       39 阅读
  2. React实践

    2024-04-24 11:04:03       72 阅读
  3. React实践

    2024-04-24 11:04:03       66 阅读
  4. React实践

    2024-04-24 11:04:03       52 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-24 11:04:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-24 11:04:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-24 11:04:03       82 阅读
  4. Python语言-面向对象

    2024-04-24 11:04:03       91 阅读

热门阅读

  1. 零基础 Chrome 扩展开发指南

    2024-04-24 11:04:03       36 阅读
  2. F检验的步骤

    2024-04-24 11:04:03       30 阅读
  3. 代码随想录算法训练营day39

    2024-04-24 11:04:03       145 阅读
  4. 使用Python实现集成学习算法:Bagging与Boosting

    2024-04-24 11:04:03       62 阅读
  5. 富格林:有效技能维护出金盈利

    2024-04-24 11:04:03       33 阅读
  6. 通用型自定义拼接 SQL 脚本,摆脱重复工作量

    2024-04-24 11:04:03       40 阅读
  7. 观察者模式

    2024-04-24 11:04:03       168 阅读
  8. Linux:SSL中加密的数字证书主要内容

    2024-04-24 11:04:03       36 阅读
  9. AWS ECS Fargate 实现批量启用部署断路器

    2024-04-24 11:04:03       43 阅读
  10. ABAP MR21: BAPI_MATVAL_PRICE_CHANGE

    2024-04-24 11:04:03       35 阅读
  11. Hive 数据倾斜

    2024-04-24 11:04:03       34 阅读