关于JavaScript的问题拖拽的问题
为什么设定了onmousedown和onmousemove移动但是div不会移动<script>window.onload=function(){varoDiv=docu...
为什么设定了onmousedown和onmousemove 移动 但是div不会移动
<script>
window.onload=function()
{var oDiv=document.getElementById('div1')
var disX=0;
var disY=0;
oDiv.onmousedown=function(ev)
{
var oEvent=ev||event;
disX=oEvent.clientX-oDiv.offsetLeft;
disY=oEvent.clientY-oDiv.offsetTop;};
oDiv.onmousemove=function(ev)
{
var oEvent=ev||event;
oDiv.style.left=oEvent.clientX-disX+'px';
oDiv.style.top=oEvent.clientY-disY+'px';}
</script>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>#div1{width: 100px;height: 100px;background:red;position: absolute;}</style>
<script>
.....
</script>
</head>
<body>
<div id="div1"></div>
</body>
</html> 展开
<script>
window.onload=function()
{var oDiv=document.getElementById('div1')
var disX=0;
var disY=0;
oDiv.onmousedown=function(ev)
{
var oEvent=ev||event;
disX=oEvent.clientX-oDiv.offsetLeft;
disY=oEvent.clientY-oDiv.offsetTop;};
oDiv.onmousemove=function(ev)
{
var oEvent=ev||event;
oDiv.style.left=oEvent.clientX-disX+'px';
oDiv.style.top=oEvent.clientY-disY+'px';}
</script>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>#div1{width: 100px;height: 100px;background:red;position: absolute;}</style>
<script>
.....
</script>
</head>
<body>
<div id="div1"></div>
</body>
</html> 展开
展开全部
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>#div1{width: 100px;height: 100px;background:red;position: absolute;}</style>
<script>
window.onload=function()
{
var oDiv=document.getElementById('div1')
var disX=0;
var disY=0;
var isDrag = false;
oDiv.onmousedown=function(ev)
{
var oEvent=ev||event;
isDrag = true;
disX=oEvent.clientX-oDiv.offsetLeft;
disY=oEvent.clientY-oDiv.offsetTop;
};
oDiv.onmouseup = function(){
isDrag = false;
};
oDiv.onmousemove=function(ev)
{
if(isDrag){
var oEvent=ev||event;
oDiv.style.left=oEvent.clientX-disX+'px';
oDiv.style.top=oEvent.clientY-disY+'px';
}
};
}
</script>
</head>
<body>
<div id="div1"></div>
</body>
</html>
用个变量做标记,检测是否按下鼠标.
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询