跟随鼠标指针跑的div拖拽效果
Posted 鱼樱、、
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了跟随鼠标指针跑的div拖拽效果相关的知识,希望对你有一定的参考价值。
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<style>
#div1 {
height:100px;
width:100px;
background-color:#0094ff;
position:absolute;
}
</style>
</head>
<body>
<div id="div1"></div>
<div id="div2"></div>
<script>
var div1 = document.getElementById("div1");
var div2 = document.getElementById("div2");
div1.onmousedown=function(e) {
var er=e||event;
var disx=er.offsetX;
var disy = er.offsetY;
div1.onmousemove=function(e1) {
var er1 = e1 || event;
var x = er1.clientX - disx;
var y = er1.clientY - disy;
div1.style.left = x + "px";
div1.style.top = y + "px";
}
}
div1.onmouseup = function () {
div1.onmousemove = null;
}
</script>
</body>
</html>
以上是关于跟随鼠标指针跑的div拖拽效果的主要内容,如果未能解决你的问题,请参考以下文章