vue实现可移动的悬浮按钮


Posted in Vue.js onMarch 04, 2021

本文实例为大家分享了vue实现可随处移动悬浮按钮的具体代码,供大家参考,具体内容如下

1.html代码

<div
 class="callback float"
 @click="onClick"
 @mousedown="down"
 @touchstart="down"
 @mousemove="move"
 @touchmove="move"
 @mouseup="end"
 @touchend="end"
 ref="fu"
 >
 <!-- <p @click="callback">返回</p> -->
 <img @click="callback" src="@/assets/images/callbs.jpg" alt />
</div>

2.再data中定义

data() {
 return {
  isLoading: false,
  flags: false, //控制使用
  position: {
  x: 0,
  y: 0,
  },
  nx: "",
  ny: "",
  dx: "",
  dy: "",
  xPum: "",
  yPum: "",
 };
 },

3.js代码

methods: {
 callback() {
  this.$router.go(-1);
 },
 onRefresh() {
  // window.location.reload();
  setTimeout((res) => {
  console.log(res);
  this.isLoading = false;
  }, 1000);
 },
 down() {
  this.flags = true;
  var touch;
  if (event.touches) {
  touch = event.touches[0];
  } else {
  touch = event;
  }
  this.position.x = touch.clientX;
  this.position.y = touch.clientY;
  this.dx = this.$refs.fu.offsetLeft;
  this.dy = this.$refs.fu.offsetTop;
 },
 move() {
  if (this.flags) {
  var touch;
  if (event.touches) {
   touch = event.touches[0];
  } else {
   touch = event;
  }
  this.nx = touch.clientX - this.position.x;
  this.ny = touch.clientY - this.position.y;
  this.xPum = this.dx + this.nx;
  this.yPum = this.dy + this.ny;
  let width = window.innerWidth - this.$refs.fu.offsetWidth; //屏幕宽度减去自身控件宽度
  let height = window.innerHeight - this.$refs.fu.offsetHeight; //屏幕高度减去自身控件高度
  this.xPum < 0 && (this.xPum = 0);
  this.yPum < 0 && (this.yPum = 0);
  this.xPum > width && (this.xPum = width);
  this.yPum > height && (this.yPum = height);
  // if (this.xPum >= 0 && this.yPum >= 0 && this.xPum<= width &&this.yPum<= height) {
  this.$refs.fu.style.left = this.xPum + "px";
  this.$refs.fu.style.top = this.yPum + "px";
  // }
  //阻止页面的滑动默认事件
  document.addEventListener(
   "touchmove",
   function () {
   event.preventDefault();
   },
   false
  );
  }
 },
 //鼠标释放时候的函数
 end() {
  this.flags = false;
 },
 onClick() {
  //在这里我是作为子组件来使用的
  this.$emit("click");
 },
 },

4.style样式

<style scoped>
.callback p {
 font-size: 16px;
 color: #fff;
 background: rgba(56, 57, 58, 0.5);
 border-radius: 50%;
 text-align: center;
 width: 50px;
 height: 50px;
 line-height: 50px;
 font-family: PingFang SC;
 font-weight: 600;
 box-shadow: 0 0 10px #fff;
}
.callback img {
 display: block;
 width: 50px;
 height: 50px;
 box-shadow: 0 0 10px rgb(133, 129, 129);
 border-radius: 50%;
 background: #fff;
}
.callback {
 position: fixed;
 top: 40px;
 left: 20px;
 z-index: 99999;
}
.float {
 position: fixed;
 right: 20px;
 top: 60%;
 touch-action: none;
 text-align: center;
 width: 50px;
 height: 50px;
 border-radius: 24px;
 line-height: 48px;
 color: white;
}
</style>

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

Vue.js 相关文章推荐
Vue 打包的静态文件不能直接运行的原因及解决办法
Nov 19 Vue.js
vue 数据操作相关总结
Dec 17 Vue.js
Vue实现简易购物车页面
Dec 30 Vue.js
Vue页面渲染中key的应用实例教程
Jan 12 Vue.js
Vue常用API、高级API的相关总结
Feb 02 Vue.js
vite2.0+vue3移动端项目实战详解
Mar 03 Vue.js
使用vue-element-admin框架从后端动态获取菜单功能的实现
Apr 29 Vue.js
vue完美实现el-table列宽自适应
May 08 Vue.js
Element-ui Layout布局(Row和Col组件)的实现
Dec 06 Vue.js
vue实力踩坑之push当前页无效
Apr 10 Vue.js
Vue OpenLayer测距功能的实现
Apr 20 Vue.js
使用vuex-persistedstate本地存储vuex
Apr 29 Vue.js
vue中axios封装使用的完整教程
Mar 03 #Vue.js
详解Vue.js 可拖放文本框组件的使用
Mar 03 #Vue.js
详解vue3中组件的非兼容变更
Mar 03 #Vue.js
vite2.0+vue3移动端项目实战详解
Mar 03 #Vue.js
Vue多选列表组件深入详解
Mar 02 #Vue.js
Vue2.x-使用防抖以及节流的示例
Mar 02 #Vue.js
Vue中避免滥用this去读取data中数据
Mar 02 #Vue.js
You might like
yii添删改查实例
2015/11/16 PHP
Laravel下生成验证码的类
2017/11/15 PHP
简单JS代码压缩器
2006/10/12 Javascript
jQuery 各种浏览器下获得日期区别
2008/12/22 Javascript
js自定义事件代码说明
2011/01/31 Javascript
jQuery自动切换/点击切换选项卡效果的小例子
2013/08/12 Javascript
Javascript中setTimeOut和setInterval的定时器用法
2015/06/12 Javascript
通过扫描二维码打开app的实现代码
2016/11/10 Javascript
bootstrapValidator自定验证方法写法
2016/12/01 Javascript
jQuery学习笔记之入门
2016/12/14 Javascript
Vue.js与 ASP.NET Core 服务端渲染功能整合
2017/11/16 Javascript
在vue中添加Echarts图表的基本使用教程
2017/11/22 Javascript
详解node.js 下载图片的 2 种方式
2018/03/02 Javascript
VuePress 静态网站生成方法步骤
2019/02/14 Javascript
Js和VUE实现跑马灯效果
2020/05/25 Javascript
浅谈JavaScript中this的指向更改
2020/07/28 Javascript
Vue自定义表单内容检查rules实例
2020/10/30 Javascript
[38:31]完美世界DOTA2联赛PWL S3 Magma vs GXR 第一场 12.13
2020/12/17 DOTA
Python中Collection的使用小技巧
2014/08/18 Python
python中pycurl库的用法实例
2014/09/30 Python
python获取mp3文件信息的方法
2015/06/15 Python
Django自定义认证方式用法示例
2017/06/23 Python
Python实现在线暴力破解邮箱账号密码功能示例【测试可用】
2017/09/06 Python
详解tensorflow训练自己的数据集实现CNN图像分类
2018/02/07 Python
基于python的ini配置文件操作工具类
2019/04/24 Python
Python Opencv实现图像轮廓识别功能
2020/03/23 Python
Python 一键制作微信好友图片墙的方法
2019/05/16 Python
如何给Python代码进行加密
2020/01/10 Python
如何通过安装HomeBrew来安装Python3
2020/12/23 Python
JINS眼镜官方网站:日本最大的眼镜邮购
2016/10/14 全球购物
Net-A-Porter美国官网:全球时尚奢侈品名站
2017/02/11 全球购物
来自圣地亚哥的实惠太阳镜:Knockaround
2018/08/27 全球购物
请说出这段代码执行后a和b的值分别是多少
2015/03/28 面试题
毕业生就业协议书
2014/04/11 职场文书
旅游投诉信范文
2015/07/02 职场文书
广播稿:校园广播稿范文
2019/04/17 职场文书