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

拖拽效果实现

程序员文章站 2022-04-28 11:27:18
...

效果
视频地址https://www.bilibili.com/video/BV1xA411h7pJ/

// An highlighted block
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style type="text/css">
			#box1{
				width: 100px;
				height: 100px;
				background-color: aqua;
				position: absolute;
			}
		</style>
	</head>
	<body>
		<div id="box1"></div>
	</body>
	<script type="text/javascript">
		window.onload = function(){
			var box1 = document.getElementById("box1")
			
			box1.onmousedown = function(){
				// alert("鼠标按下");
				document.onmousemove = function(event){
					event = event || window.event
					var left = event.clientX
					var top = event.clientY
					
					box1.style.left = left + "px"
					box1.style.top = top + "px"
				}
				box1.onmouseup = function(){
					document.onmousemove = null;
				}
			}
		}
	</script>
</html>

相关标签: 前端 javascript