vue 移动端屏幕内div随意拖动demo

div在屏幕内随意拖拽
demo1

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>拖拽</title>
	<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <style>
		*{
			margin: 0;
			padding: 0;
		}
		html{
			font-size: 625%;
		}
		.text{
			font-size: .32rem;
		}
	   .touchWrap {
			height: .45rem;
			width: .45rem;
			z-index: 9999;
			position: fixed;
			top: .42rem;
			right: .32rem;
			border-radius: .08rem;
			background-color: rgba(0, 0, 0, 0.55);
		}
		.touch {
			height: .25rem;
			width: .25rem;
			border: .03rem solid rgba(140, 136, 136, 0.5);
			margin: 0.07rem auto;
			line-height: .25rem;
			border-radius: 100%;
			background-color: #ffffff;
		 }
    </style>
   
</head>
<body>
    <div id="app">
		<!-- 悬浮的HTML -->
		<div class="touchWrap" ref="moveDiv"
		  @mousedown="down" @touchstart="down"
		  @mousemove="move" @touchmove.prevent="move"   
		  @mouseup="end" @touchend="end"
		>
		  <div class="touch"></div>
		</div>
    </div>
</body>
	<script>
		new Vue({
			el:"#app",
			data:{
				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 = this.$refs.moveDiv.offsetLeft;
				this.dy = this.$refs.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;
									
				  const maxWidth = window.screen.width - 45; //屏幕分辨率宽度减去悬浮框宽高
				  const maxHeight = window.screen.height - 45;
				  if (this.xPum < 0) { //屏幕x限制
					this.xPum = 0;
				  } else if (this.xPum>maxWidth) {
					this.xPum = maxWidth;
				  }
				  if (this.yPum < 0) { //屏幕y限制
					this.yPum = 0;
				  } else if (this.yPum>maxHeight) {
					this.yPum = maxHeight;
				  }
				  this.$refs.moveDiv.style.left = this.xPum+"px";
				  this.$refs.moveDiv.style.top = this.yPum +"px";
				  //阻止页面的滑动默认事件;如果碰到滑动问题,1.2 请注意是否获取到 touchmove
				  document.addEventListener("touchmove",function(){
					  // event.preventDefault();
					  event.stopPropagation()
				  },false);
				}
			  },
			//鼠标释放时候的函数
			  end(){
				this.flags = false;
			  }
			}
		})
	</script>
</html>

demo2

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style>
			.main{
			    background-color: brown;
			    height: -webkit-fill-available;
			}
			.drag_area{
				width: 10vw;
				height: 10vw;
				background-color: dodgerblue;
				position: absolute;
				top: 0;
				left: 0;
			}
		</style>
		<script src="vue.js"></script>
	</head>
	<body>
		<div id="app">
			<div class="main">
				<div 
					ref="move_div" 
					@touchstart="down" 
					@touchmove="move" 
					@touchend="end" 
					:style="{top: top  + 'px', left: left  + 'px'}" 
					class="drag_area">
				</div>
			</div>
		</div>
		<script>
			new Vue({
			    el:"#app",
			    data:{
				  flags: false,
				  position: {x: 0, y: 0, left: 0, top: 0},
				  top: 0,
				  left: 0,
				  width: window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth,
				  height: window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight
			    },
				methods: {
				    down () { // 拖动开始的操作
				      this.flags = true
				      const refs = this.$refs.move_div.getBoundingClientRect()
				      let touch = event
				      if (event.touches) {
				        touch = event.touches[0]
				      }
				      this.position.x = touch.clientX
				      this.position.y = touch.clientY
				      this.position.left = refs.left
				      this.position.top = refs.top
				    },
				    move () { // 拖动中的操作
				      if (this.flags) {
				        let touch = event
				        if (event.touches) {
				          touch = event.touches[0]
				        }
				        const xPum = this.position.left + touch.clientX - this.position.x
				        const yPum = this.position.top + touch.clientY - this.position.y
				        this.left = xPum
				        this.top = yPum
				        this.banOut()
				        // 阻止页面的滑动默认事件
				        document.addEventListener('touchmove', function () {
				          event.preventDefault()
				        }, {passive: false})
				      }
				    },
				    end () { // 拖动结束的操作
				      this.flags = false
				      this.banOut()
				    },
				    banOut () { // 避免拖动出界的限制
				      const refs = this.$refs.move_div.getBoundingClientRect()
				      if (this.left < 0) {
				        this.left = 0
				      } else if (this.left > this.width - refs.width) {
				        this.left = this.width - refs.width
				      }
				      if (this.top < 0) {
				        this.top = 0
				      } else if (this.top > this.height - refs.height) {
				        this.top = this.height - refs.height
				      }
				    }
				  }
			})
		</script>
	</body>
	
</html>

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值