当前位置:   article > 正文

JS拖曳盒子案例

JS拖曳盒子案例

让我为大家带来一个小案例吧!

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style>
			* {
				margin: 0;
				padding: 0;
			}
			.box1 {
				width: 100px;
				height: 100px;
				background-color: black;
				margin-bottom: 10px;
				position: relative;
				left: 0;
				top: 0;
			}
			.box2 {
				width: 100px;
				height: 100px;
				background-color: pink;
				margin-bottom: 10px;
				margin-left: 200px;
			}
			.box3 {
				width: 100px;
				height: 100px;
				background-color: orange;
				margin-top: 120px;
				margin-left: 200px;
			}
		</style>
	</head>
	<body>
		<div class="box1"></div>
		<div class="box2"></div>
		<div class="box3"></div>
	</body>
	<script>
		//获取要移动的盒子
	 	var div = document.querySelector(".box1")
		//获取其他2个盒子
		var div1=document.querySelector(".box2")
		var div2=document.querySelector(".box3")
		div.onmousedown = function(e){
			//获取盒子距离页面多少
			var divX =div.offsetLeft
			var divY =div.offsetTop
			//获取盒子点击的xy坐标
			var downX =e.clientX
			var downY =e.clientY
			document.onmousemove=function(e){
			console.log(e);
			// if(div.offsetLeft<1){
			// 	alert("超出")
			// }
			//获取盒子点击的地方距离document点击的坐标的差
			var moveX =e.clientX - downX
			var moveY =e.clientY - downY
			//获取移动鼠标想放哪放哪
			// div.style.left =divX+moveX+"px"
			// div.style.top =divY+moveY+"px"
			//这个的中心点在中间
			div.style.left =e.clientX - div.offsetWidth/2+"px"
			div.style.top =e.clientY - div.offsetHeight/2+"px"
			//拿他们的宽高去判断坐标
			if(e.clientX<div1.offsetWidth+div.offsetWidth/2+200&&e.clientX>div1.offsetWidth+div.offsetWidth/2&&e.clientY>div1.offsetTop/2&&e.clientY<div1.offsetTop*2+40){
				console.log(div1.offsetTop);
				div1.style.backgroundColor="green"
				}else{
					div1.style.backgroundColor="pink"
				}
				//拿他们的距离去判断
			 if(e.clientX<div2.offsetWidth+div.offsetWidth/2+200&&e.clientX>div1.offsetWidth+div.offsetWidth/2&&e.clientY>div2.offsetTop-div.offsetHeight/2&&e.clientY<div2.offsetTop+div.offsetHeight+div.offsetHeight/2){
				div2.style.backgroundColor="blue"
				}else {
					div2.style.backgroundColor="orange"
				}
			}
		}
		//如果按起就不动
		div.onmouseup = function(){
			document.onmousemove = null
		}
	</script>
</html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88

效果图
请添加图片描述

感谢大家的阅读,如有不对的地方,可以向我提出,感谢大家!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/266072
推荐阅读
相关标签
  

闽ICP备14008679号