当前位置: 首页 > 图文教程 > 网络编程 > Javascript > javascript 鼠标拖动图标技术

Javascript
form中限制文本字节数js代码
use jscript with List Proxy Server Information
use jscript List Installed Software
List Installed Software Features
List Information About the Binary Files Used by an Application
List the Codec Files on a Computer
List the UTC Time on a Computer
List Installed Hot Fixes
excel操作之Add Data to a Spreadsheet Cell
Add Formatted Data to a Spreadsheet
Apply an AutoFormat to an Excel Spreadsheet
JavaScript语法着色引擎(demo及打包文件下载)
类之Prototype.js学习
一款JavaScript压缩工具:X2JSCompactor
iis6+javascript Add an Extension File
jscript之Open an Excel Spreadsheet
jscript之Read an Excel Spreadsheet
jscript之List Excel Color Values
去除图像或链接黑眼圈的两种方法总结
Add a Formatted Table to a Word Document

Javascript 中的 javascript 鼠标拖动图标技术


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2010-02-27   浏览: 361 ::
收藏到网摘: n/a

刚刚学习了个js的小技术,还蛮好玩的,以后会把做的一些小例子贴上来,方便自己也可以跟大家一起分享学习成果,哈哈~~ 这个例子简单讲就是鼠标拖动图标到它任意想去的地方
代码
复制代码 代码如下:

<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>