移动端悬浮框可移动,可回弹,Vue and React

一,首先讲 React的悬浮框

示例,可参照链接

Demo文档,可参照链接

 

1. 安装

1
npm install suspend-button -S

2. 使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import React, { Component } from 'react'
import ReactDOM from 'react-dom'
import SuspendButton from 'suspend-button'
 
class App extends Component {
  render() {
    return (
      <SuspendButton></SuspendButton>
    )
  }
}
 
ReactDOM.render(
  <App />,
  document.getElementById('container')
)

  

二,Vue的悬浮框 (可直接将代码进行拷贝到页面组件中)

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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<template>
  <div class="removeHome">
    <span class="t-suspend-button"
          @touchstart="onTouchStart"
          @touchmove="onTouchMove"
          @touchend="onTouchEnd"
          ref="remove"
          :style="`left: ${oLeft}px; top: ${oTop}px;`">
 
      <div class="yuanqiu"
           @click="$router.replace({path: '/'})">
        <img :src="img" />
      </div>
    </span>
  </div>
</template>
 
<script>
export default {
  props: {
    img: {
      type: String,
      default: ""
    }
  },
  data() {
    return {
      oLeft: "",
      oTop: "",
      $vm: null, // 悬浮按钮
      moving: false, // 移动状态
      oW: null, // 悬钮距离
      oH: null,
      htmlWidth: null, // 页面宽度
      htmlHeight: null,
      bWidth: null, // 悬钮宽度
      bHeight: null,
      click: false // 是否是点击
    };
  },
  mounted() {
    this.$refs.remove.addEventListener(
      "touchmove",
      e => {
        if (e.cancelable) {
          e.preventDefault();
        }
      },
      { passive: false }
    );
  },
  methods: {
    // 移动触发
    onTouchStart(e) {
      e = e.touches[0];
      this.click = true;
 
      this.oW = e.clientX - this.$refs.remove.getBoundingClientRect().left;
      this.oH = e.clientY - this.$refs.remove.getBoundingClientRect().top;
 
      console.log("e.clientX宽", e.clientX, "e.clientY高", e.clientY);
 
      console.log(
        "移动宽",
        this.$refs.remove.getBoundingClientRect().left,
        "移动高",
        this.$refs.remove.getBoundingClientRect().top
      );
 
      this.htmlWidth = document.documentElement.clientWidth;
      this.htmlHeight = document.documentElement.clientHeight;
 
      console.log("body宽", this.htmlWidth, "body高", this.htmlHeight);
 
      this.bWidth = this.$refs.remove.offsetWidth;
      this.bHeight = this.$refs.remove.offsetHeight;
 
      console.log("a宽", this.oW, "a高", this.oH);
 
      let oLeft = e.clientX - this.oW;
      let oTop = e.clientY - this.oH;
 
      this.oLeft = oLeft;
      this.oTop = oTop;
 
      this.moving = true;
    },
    // 移动结束
    onTouchEnd(e) {
      this.moving = false;
 
      this.$refs.remove.class + " t-suspend-button-animate";
 
      // 左侧距离
      let oLeft = this.oLeft;
      if (oLeft < (this.htmlWidth - this.bWidth) / 2) {
        oLeft = 0;
      } else {
        oLeft = this.htmlWidth - this.bWidth;
      }
 
      // if (this.click) {
      //   this.props.onClick();
      // }
      // }
      // if (oTop < 0) {
      //   oTop = 0;
      // } else if (oTop > this.htmlHeight - this.bHeight) {
      //   oTop = this.htmlHeight - this.bHeight;
      // }
 
      this.oLeft = oLeft;
    },
    // 开始移动
    onTouchMove(e) {
      this.$refs.remove.class = "t-suspend-button";
      this.moving && this.onMove(e);
    },
    // 移动中
    onMove(e) {
      e = e.touches[0];
      this.click = false;
 
      // 左侧距离
      let oLeft = e.clientX - this.oW;
      let oTop = e.clientY - this.oH;
      console.log("移动左距离", oLeft, "移动上距离", oTop);
      if (oLeft < 0) {
        oLeft = 0;
      } else if (oLeft > this.htmlWidth - this.bWidth) {
        oLeft = this.htmlWidth - this.bWidth;
      }
      if (oTop < 0) {
        oTop = 0;
      } else if (oTop > this.htmlHeight - this.bHeight) {
        oTop = this.htmlHeight - this.bHeight;
      }
 
      this.oLeft = oLeft;
      this.oTop = oTop;
    }
  }
};
</script>
 
<style lang="scss">
.removeHome {
  .t-suspend-button {
    position: fixed;
    bottom: 105px;
    right: 9px;
    width: 3rem;
    height: 3rem;
    border-radius: 2rem;
    z-index: 99999;
  }
 
  .t-suspend-button img {
    width: 100%;
    height: 100%;
  }
 
  .t-suspend-button-animate {
    transition-duration: 0.4s;
  }
  .yuanqiu {
    width: 48px;
    height: 48px;
    img {
      width: 100%;
      height: 100%;
    }
  }
}
</style>

  

posted @   小短腿奔跑吧  阅读(1352)  评论(1编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示