相思资源网 Design By www.200059.com

vue移动端实现div拖拽移动,供大家参考,具体内容如下

本文讲述,在使用VUE的移动端实现类似于iPhone的悬浮窗的效果。

相关知识点

  • touchstart 当在屏幕上按下手指时触发
  • touchmove 当在屏幕上移动手指时触发
  • touchend 当在屏幕上抬起手指时触发
  • mousedown mousemove mouseup对应的是PC端的事件
  • touchcancel 当一些更高级别的事件发生的时候(如电话接入或者弹出信息)会取消当前的touch操作,即触发touchcancel。一般会在touchcancel时暂停游戏、存档等操作。

效果图

vuejs移动端实现div拖拽移动

实现步骤

(一)html

总结了一下评论,好像发现大家都碰到了滑动的问题。就在这里提醒一下吧。可将该悬浮 DIV 同你的 scroller web 同级。 ---- (log: 2018-08-21)

html结构: <template> <div>你的web页面</div> <div>悬浮DIV</div> </template>

<template>
 <div id="webId">
 <div>你的web页面</div>
 <!-- 1.1 如果碰到滑动问题,请检查这里是否属于同一点。 -->
 <!-- 悬浮的HTML -->
 <div v-if="!isShow" class="xuanfu" id="moveDiv"
 @mousedown="down" @touchstart="down"
 @mousemove="move" @touchmove="move"
 @mouseup="end" @touchend="end"
 >
 <div class="yuanqiu">
 {{pageInfo.totalPage}}
 </div>
 </div>
 </div>
</template>

(二)JS

<script>
data() {
 return {
 flags: false,
 position: { x: 0, y: 0 },
 nx: '', ny: '', dx: '', dy: '', xPum: '', yPum: '',
 }
}

methods: {
 // 实现移动端拖拽
 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 = moveDiv.offsetLeft;
 this.dy = moveDiv.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;
 moveDiv.style.left = this.xPum+"px";
 moveDiv.style.top = this.yPum +"px";
 //阻止页面的滑动默认事件
 document.addEventListener("touchmove",function(){ // 1.2 如果碰到滑动问题,请注意是否获取到 touchmove
 event.preventDefault();//jq 阻止冒泡事件
 // event.stopPropagation(); // 如果没有引入jq 就用 stopPropagation()
 },false);
 }
 },
//鼠标释放时候的函数
 end(){
 this.flags = false;
 },
}
</script>

(三)CSS

<style>
 .xuanfu {
 height: 4.5rem;
 width: 4.5rem;
 /*1.3 如果碰到滑动问题,请检查 z-index。z-index需比web大一级*/
 z-index: 999;
 position: fixed;
 top: 4.2rem;
 right: 3.2rem;
 border-radius: 0.8rem;
 background-color: rgba(0, 0, 0, 0.55);
 }
 .yuanqiu {
 height: 2.7rem;
 width: 2.7rem;
 border: 0.3rem solid rgba(140, 136, 136, 0.5);
 margin: 0.65rem auto;
 color: #000000;
 font-size: 1.6rem;
 line-height: 2.7rem;
 text-align: center;
 border-radius: 100%;
 background-color: #ffffff;
 }
</style>

实现好JS逻辑,基本上,问题不大。

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

标签:
vuejs移动端拖拽移动,vuejs移动端拖拽,vuejs移动端移动

相思资源网 Design By www.200059.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
相思资源网 Design By www.200059.com

评论“vuejs移动端实现div拖拽移动”

暂无vuejs移动端实现div拖拽移动的评论...

RTX 5090要首发 性能要翻倍!三星展示GDDR7显存

三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。

首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。

据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。