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项目中企业微信使用js-sdk时config和agentConfig配置方式详解
Dec 15 Vue.js
在vue项目中封装echarts的步骤
Dec 25 Vue.js
vue watch监控对象的简单方法示例
Jan 07 Vue.js
vue 组件基础知识总结
Jan 26 Vue.js
vue使用transition组件动画效果的实例代码
Jan 28 Vue.js
vue 实现click同时传入事件对象和自定义参数
Jan 29 Vue.js
vue 使用饿了么UI仿写teambition的筛选功能
Mar 01 Vue.js
vue首次渲染全过程
Apr 21 Vue.js
Vue3.0中Ref与Reactive的区别示例详析
Jul 07 Vue.js
vue中 this.$set的使用详解
Nov 17 Vue.js
Vue全局事件总线你了解吗
Feb 24 Vue.js
vue 自定义组件添加原生事件
Apr 21 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
PHP file_get_contents 函数超时的几种解决方法
2009/07/30 PHP
php实现图片添加描边字和马赛克的方法
2014/12/10 PHP
php获取数据库结果集方法(推荐)
2017/06/01 PHP
yii2 commands模式以及配置crontab定时任务的方法
2017/08/19 PHP
PHP实现通过CURL上传文件功能示例
2018/05/30 PHP
CodeIgniter框架实现的整合Smarty引擎DEMO示例
2019/03/28 PHP
PHP进阶学习之Geo的地图定位算法详解
2019/06/19 PHP
基于prototype扩展的JavaScript常用函数库
2010/11/30 Javascript
javascript获取URL参数与参数值的示例代码
2013/12/20 Javascript
JS版的date函数(和PHP的date函数一样)
2014/05/12 Javascript
JS实现让访问者自助选择网页文字颜色的方法
2015/02/24 Javascript
轻量级javascript 框架Backbone使用指南
2015/07/24 Javascript
使用pcs api往免费的百度网盘上传下载文件的方法
2016/03/17 Javascript
jquery 删除节点 添加节点 找兄弟节点的简单实现
2016/12/07 Javascript
bootstrap配合Masonry插件实现瀑布式布局
2017/01/18 Javascript
javascript 单例模式详解及简单实例
2017/02/14 Javascript
mui 打开新窗口的方式总结及注意事项
2017/08/20 Javascript
基于 D3.js 绘制动态进度条的实例详解
2018/02/26 Javascript
vue定义全局变量和全局方法的方法示例
2018/08/01 Javascript
python连接mysql实例分享
2016/10/09 Python
Python中的错误和异常处理简单操作示例【try-except用法】
2017/07/25 Python
Python字符串格式化的方法(两种)
2017/09/19 Python
详解python中init方法和随机数方法
2019/03/13 Python
Python字符串对齐、删除字符串不需要的内容以及格式化打印字符
2021/01/23 Python
CSS3图片旋转特效(360/60/-360度)
2013/10/10 HTML / CSS
加拿大最大的书店:Indigo
2017/01/01 全球购物
星空联盟C# .net笔试题
2014/12/05 面试题
汽车工程专业应届生求职信
2013/10/19 职场文书
大学生毕业求职自荐书范文
2014/02/04 职场文书
创业女性典型材料
2014/05/02 职场文书
2014年教学管理工作总结
2014/12/02 职场文书
面试通知单大全
2015/04/20 职场文书
早安问候语大全
2015/11/10 职场文书
企业转让协议书(范文2篇)
2019/08/15 职场文书
大学生如何逃脱“毕业季创业队即散伙”魔咒?
2019/08/19 职场文书
警用民用对讲机找不同
2022/02/18 无线电