87,907
社区成员
发帖
与我相关
我的任务
分享
<iframe style="width: 100%; height: 300px" src="http://jsfiddle.net/jikeytang/yKeAk/embedded/" allowfullscreen="allowfullscreen" frameborder="0"></iframe>
Y0=parseInt(moveObj.style.top+0);//?不清楚为什么一定要+0
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"></HEAD>
<BODY style="height:800px;">
<div id="testDiv" style="width:200px; height:200px; background:#f00; position:absolute; cursor:move"></div>
<script type="text/javascript">
var start =
(function(){
var i, // count
o, // HTML DOM
x,y;
function moving(e)
{
if(i++^6)//like if(!(i++===6))
return;
o.style.left=(e.screenX-x)+'px';
o.style.top=(e.screenY-y)+'px';
i=0;
}
function end(e)
{
if(window.addEventListener){
window.document.removeEventListener('mousemove',moving,false);
document.removeEventListener('mouseup',arguments.callee,false);
}else{
window.document.detachEvent('onmousemove',moving);
window.document.detachEvent('onmouseup',arguments.callee);
}
window.document.body.focus() // ff 3.0
}
return function(e,d)
{
i = 0;
x = e.screenX-d.offsetLeft;
y = e.screenY-d.offsetTop;
o = d;
if(window.addEventListener){
window.document.addEventListener('mousemove',moving,false)
window.document.addEventListener('mouseup',end,false)
}else{
window.document.attachEvent('onmousemove',moving)
window.document.attachEvent('onmouseup',end)
}
}
})();
document.getElementById('testDiv').onmousedown=function(e){
e=e || event;
start(e,this);
e.cancelBubble=true;
return false;
}
</script>
</BODY>
</HTML>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<style>
.move{
cursor:move;
position:absolute;
top:10px;/*必须要设置,不然在IE里获取的.top为auto,无法进行算术运算*/
left:400px;
width:100px;
height:100px;
background:green;
-moz-user-select:none;/*火狐禁止选中*/
}
.food{background:blue;width:100px;height:100px;}
</style>
<script language="javascript">
var X0,Y0,eX0,eY0;
var dragAble=false;
var dragObj;
var ie = (document.all) ? true : false;//是否为IE
//开始拖拽
function dragStart(e){
var oDragHandle;
if(ie){
oDragHandle = e.srcElement;
}else{
oDragHandle = e.target;
}
if(oDragHandle.className=='move'){
dragObj=oDragHandle;
eX0= e.clientX;
eY0= e.clientY;
X0=getNowStyle(dragObj,"left");//获取当前样式值
Y0=getNowStyle(dragObj,"top");
show(X0,Y0);
dragAble=true;
}
}
//拖拽
function drag(e){
if(dragAble){
var eX1= e.clientX;
var eY1= e.clientY;
dragObj.style.left=X0+eX1-eX0+"px";
dragObj.style.top=Y0+eY1-eY0+"px";
}
}
//停止拖拽
function dragEnd(){
dragAble=false;
}
//事件监听浏览器兼容
function addListener(element,eName,fn){
if(ie){// ie
element.attachEvent("on"+eName,fn);
}else {// firefox , w3c
element.addEventListener(eName,fn,false);
}
}
//添加事件监听
window.onload = function(){
addListener(document,"mousedown",dragStart);//鼠标按下开始拖拽
addListener(document,"mousemove",drag);//拖拽
addListener(document,"mouseup",dragEnd);//弹起停止拖拽
}
//当前渲染样式
function getNowStyle(element,styleName){
var nowValue=parseInt(ie?element.currentStyle[styleName]:window.getComputedStyle(element,null)[styleName]);
return nowValue;
}
</script>
</head>
<body>
<div class="move"><!--<br/>如果样式规定里此div可选,并且没有内容的话,火狐会有bug--></div>
<div class="food"></div>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<style>
.move{cursor:move; position:absolute; width:100px; height:100px; background:green;-moz-user-select:none;/*火狐禁止选中*/}
</style>
<script language="javascript">
var X0,Y0,eX0,eY0;
var dragAble=false;
var dragObj;
var ie = (document.all) ? true : false;//是否为IE
function dragStart(e){//现在参数e在IE里也是是直接可以供函数使用的,比如第16行。
var oDragHandle;
if(ie){
oDragHandle = e.srcElement;
}else{
oDragHandle = e.target;
}
if(oDragHandle.className=='move')
{
dragObj=oDragHandle;
eX0= e.clientX;
eY0= e.clientY;
X0=parseInt(dragObj.style.left+0);
Y0=parseInt(dragObj.style.top+0);
dragAble=true;
}
}
//拖拽
function drag(e){
if(dragAble)
{
var eX1= e.clientX;
var eY1= e.clientY;
dragObj.style.left=X0+eX1-eX0+"px";
dragObj.style.top=Y0+eY1-eY0+"px";
}
}
//停止拖拽
function dragEnd(){
dragAble=false;
}
//事件监听浏览器兼容
function addListener(element,eName,fn){
if(ie){// ie
element.attachEvent("on"+eName,fn);
}else {// firefox , w3c
element.addEventListener(eName,fn,false);
}
}
//添加事件监听
window.onload = function(){
addListener(document,"mousedown",dragStart);//鼠标按下开始拖拽
addListener(document,"mousemove",drag);//拖拽
addListener(document,"mouseup",dragEnd);//弹起停止拖拽
}
//注:添加了事件监听之后,IE里也可以像火狐里那样直接拿事件对象当函数的参数来用了,不必再window.event。
//但是不大清楚这是为什么,或者是说我还有什么地方理解错了,求交流。QQ:1140215489
</script>
</head>
<body>
<div class="move"><!--<br/>如果样式规定里此div可选,并且没有内容的话,火狐会有bug--></div>
<div class="move" style="background:blue;left:200px"></div>
</body>
</html>