Hong
2017-01-11 10:39
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<style type="text/css">
*{
margin: 0 0;
padding: 0 0;
}
div{
width: 400px;
height: 250px;
background: black;
/*margin: 0 auto;*/
text-align: center;
line-height: 250px;
position: absolute;
left: 0;
top: 0;
}
span{
width: 100px;
height: 40px;
background: white;
color: black;
}
</style>
<title></title>
</head>
<body>
<div id="div1">
<span id="span1">按住我拖拽</span>
</div>
<script type="text/javascript">
var fubiaoqian=document.getElementById("div1")
var benshen=document.getElementById("span1")
benshen.style.cursor="pointer"
function tuodong(){
document.onmousemove=function(weizhi){
var weizhi=weizhi||window.weizhi;
var juliX=benshen.offsetLeft
juliY=benshen.offsetTop
fubiaoqian.style.left=weizhi.clientX+"px"
fubiaoqian.style.top=weizhi.clientY+"px"
document.title=juliX+","+juliY
}
}
benshen.onmousedown=tuodong
</script>
</body>
</html>
因为你用的是span,是行内标签,所以宽度和高度不是像块元素那样是固定的,是根据内容大小改变的,也就是说,你设置的宽度对span不起作用,高度也是,行内元素和块元素是不一样的,给span设置高度和宽度没有意义
DOM事件探秘
99544 学习 · 1197 问题
相似问题