相思资源网 Design By www.200059.com
这个例子简单讲就是鼠标拖动图标到它任意想去的地方
代码
复制代码 代码如下:
<body>
<div id="block" style="width:100px; height:100px; position:relative; background-color:#FF0000";></div>
<script>
var bb = document.getElementById("block");
bb.onmousedown = function(){
//获取鼠标当前坐标
var pageX = event.clientX;
var pageY = event.clientY;
//获取block的坐标,左边界和上边界
var offX = parseInt(this.style.left)||0;
var offY = parseInt(this.style.top)||0;
//计算出鼠标坐标相对于block坐标的间距
var offLX = pageX-offX;
var offLY = pageY-offY;
if(!document.onmousemove){
document.onmousemove = function(){
bb.style.left=event.clientX-offLX; //设置block的X坐标
bb.style.top=event.clientY-offLY; //设置block的Y坐标
}
}
}
document.onmouseup = function(){document.onmousemove = null;} //鼠标弹起
</script>
</body>
代码
复制代码 代码如下:
<body>
<div id="block" style="width:100px; height:100px; position:relative; background-color:#FF0000";></div>
<script>
var bb = document.getElementById("block");
bb.onmousedown = function(){
//获取鼠标当前坐标
var pageX = event.clientX;
var pageY = event.clientY;
//获取block的坐标,左边界和上边界
var offX = parseInt(this.style.left)||0;
var offY = parseInt(this.style.top)||0;
//计算出鼠标坐标相对于block坐标的间距
var offLX = pageX-offX;
var offLY = pageY-offY;
if(!document.onmousemove){
document.onmousemove = function(){
bb.style.left=event.clientX-offLX; //设置block的X坐标
bb.style.top=event.clientY-offLY; //设置block的Y坐标
}
}
}
document.onmouseup = function(){document.onmousemove = null;} //鼠标弹起
</script>
</body>
标签:
javascript,鼠标拖动
相思资源网 Design By www.200059.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
相思资源网 Design By www.200059.com
暂无javascript 鼠标拖动图标技术的评论...