兼容各主流浏览器的简单拖拽drag
2009-09-24 10:26
330 查看
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style type='text/css'>
#d{border:1px solid green;width:90px;height:57px;position:absolute;}
#h{border:1px solid green;width:90px;height:17px;position:absolute;}
</style>
<script type='text/javascript' src="/js/jquery/jquery-1.2.6.min.js"></script>
<script type='text/javascript'>
(function($) {
$.fn.extend({
drag: function(id) {
$(this).mousedown(function(event) {
var Obj, pX,pY;
document.onmouseup = MUp;
document.onmousemove = MMove;
MDown(id, event);
function MDown(id, event) {
Obj = document.getElementById(id);
if (Obj.setCapture)
Obj.setCapture();
else
window.captureEvents(Event.MOUSEMOVE | Event.MOUSEUP);
pX = event.clientX - Obj.offsetLeft;
pY = event.clientY - Obj.offsetTop;
}
function MMove(event) {
if (window.event) event = window.event;
if (Obj) {
Obj.style.left = event.clientX - pX + "px";
Obj.style.top = event.clientY - pY + "px";
}
}
function MUp(event) {
if (Obj) {
if (Obj.releaseCapture)
Obj.releaseCapture();
else
window.captureEvents(Event.MOUSEMOVE | Event.MOUSEUP);
Obj = null;
}
}
});
}
});
})(jQuery);
</script>
</head>
<body>
<div id="d">
<div id="h">Header</div>
</div>
<script type='text/javascript' >
$("#h").drag('d'); //IE下,点击id='h'可拖动id='d',FF兼容待完善
</script>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style type='text/css'>
#d{border:1px solid green;width:90px;height:57px;position:absolute;}
#h{border:1px solid green;width:90px;height:17px;position:absolute;}
</style>
<script type='text/javascript' src="/js/jquery/jquery-1.2.6.min.js"></script>
<script type='text/javascript'>
(function($) {
$.fn.extend({
drag: function(id) {
$(this).mousedown(function(event) {
var Obj, pX,pY;
document.onmouseup = MUp;
document.onmousemove = MMove;
MDown(id, event);
function MDown(id, event) {
Obj = document.getElementById(id);
if (Obj.setCapture)
Obj.setCapture();
else
window.captureEvents(Event.MOUSEMOVE | Event.MOUSEUP);
pX = event.clientX - Obj.offsetLeft;
pY = event.clientY - Obj.offsetTop;
}
function MMove(event) {
if (window.event) event = window.event;
if (Obj) {
Obj.style.left = event.clientX - pX + "px";
Obj.style.top = event.clientY - pY + "px";
}
}
function MUp(event) {
if (Obj) {
if (Obj.releaseCapture)
Obj.releaseCapture();
else
window.captureEvents(Event.MOUSEMOVE | Event.MOUSEUP);
Obj = null;
}
}
});
}
});
})(jQuery);
</script>
</head>
<body>
<div id="d">
<div id="h">Header</div>
</div>
<script type='text/javascript' >
$("#h").drag('d'); //IE下,点击id='h'可拖动id='d',FF兼容待完善
</script>
</body>
</html>
相关文章推荐
- jquery实现简单的拖拽效果实例兼容所有主流浏览器(优化篇)
- jquery实现简单的拖拽效果实例兼容所有主流浏览器(优化篇)
- jquery实现简单的拖拽效果实例兼容所有主流浏览器
- jquery实现简单的拖拽效果实例兼容所有主流浏览器
- 【转】图片上传简单JS预览,兼容IE火狐等主流浏览器
- 兼容主流浏览器的jQuery+CSS 实现遮罩层的简单代码
- js鼠标滑轮滚动事件绑定的简单实例(兼容主流浏览器)
- 兼容主流浏览器的jQuery+CSS 实现遮罩层的简单代码
- jquery实现简单的拖拽效果,兼容所有浏览器(一)
- 简单漂亮的js弹窗可自由拖拽且兼容大部分浏览器
- 简单漂亮的js弹窗可自由拖拽且兼容大部分浏览器
- js鼠标滑轮滚动事件绑定的简单实例(兼容主流浏览器)
- 简单漂亮的js弹窗 - 自由拖拽 - 兼容大部分浏览器
- 封装的一个JS拖拽效果,兼容所有主流浏览器
- 简单几步原生javascript实现元素拖拽功能兼容所有浏览器
- jquery实现简单的拖拽效果,兼容所有浏览器(二)
- 简单漂亮的js弹窗可自由拖拽且兼容大部分浏览器
- jQuery+CSS 简单代码实现遮罩层( 兼容主流浏览器 )
- 一段简单 javascript 的代码,用来验证日期的格式,支持闰年,支持 IE、Fixfox、Chrome 等主流浏览器
- 兼容各大主流浏览器的常用CSS Hack汇总