JavaScript中onmousemove鼠标截取?

<!DOCTYPEhtml><html><head><title>Dom8Work</title><style>html,body{margin:0;padding:0;... <!DOCTYPE html>
<html>
<head>
<title>Dom8Work</title>
<style>
html,body{
margin:0;
padding:0;
width:100%;
height:100%;
background:black;
font-size:50px;

}
#san{
position:relative;
width:0.1px;
height:0.1px;
border-left:50px solid transparent ;
border-right:50px solid transparent ;
border-top:50px solid transparent;
border-bottom:50px solid red;

}
#san:hover{
cursor:crosshair;
}
</style>
</head>
<body>
<div id="san">

</div>
<script type="text/javascript">

var san = document.getElementById("san");

var xia = false;
san.onmousedown = function(e){
xia = true;
console.log("click down",xia)
}
san.onmouseup = function(e){
if(xia==true){
xia=false;

}
console.log("click up");
}

document.body.onmousemove= function(e){
if(xia==true){
console.log(a.clientX);
san.style.left=e.clientX+"px"
san.style.top =e.clientY+"px"
}

}

</script>
</body>
</html>
上面这是一个小的三角形DIV,当我鼠标点击上去的时候可以拖动,松开停下,但是我每次点击移动的是后鼠标指针老师会自己跑到左上角去,不能点在三角形中间,也就是不能我想放在哪拖动就放在哪拖动。
展开
 我来答
yugi111
2015-08-06 · TA获得超过8.1万个赞
知道大有可为答主
回答量:5.1万
采纳率:70%
帮助的人:1.4亿
展开全部
<!DOCTYPE html>
<html>
<head>
<title>
Dom8Work
</title>
<style>
html,body{
margin:0;
padding:0;
width:100%;
height:100%;
background:black;
font-size:50px;

}
#san{
position:relative;
width:0.1px;
height:0.1px;
border-left:50px solid transparent ;
border-right:50px solid transparent ;
border-top:50px solid  transparent;
border-bottom:50px solid red;

}
#san:hover{
cursor:move;
}
</style>
</head>
<body>
<div id="san">
</div>
<script type="text/javascript">
san.onmousedown = function(e){
e = window.event || e;
var deltaX = e.clientX - this.offsetLeft;
var deltaY = e.clientY - this.offsetTop;
document.onmousemove= function(e){
e = window.event || e;
san.style.left = e.clientX - deltaX + "px";
san.style.top = e.clientY - deltaY + "px";
}
};
document.onmouseup = function(){
this.onmousemove = null;
};
</script>
</body>
</html>
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式