欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-07-03 23:20:26
...

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>

相关标签: web前端