如何让那个一个div可以拖动

[code]
<!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>
<title> </title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
body{background:#ccc;}
.block{position:absolute;left:0;top:100px;border:1px solid #000;background:red;width:30px;height:30px;}
#google{width:300px;height:300px;border:2px inset #fff;background:#fff;position:relative;overflow:hidden;}
</style>
</head>
<body>
<div id="google"><div id="main" class="block"></div></div>
</body>
<script type="text/javascript">
//<![CDATA[
function Drag(title,body,range){
var w=window,win=body||title,x,y,_left,_top,range=range||function(x){return x};
title.style.cursor='move';
title.οnmοusedοwn=function (e){
e=e||event;
x=e.clientX,y=e.clientY,_left=win.offsetLeft,_top=win.offsetTop;
this.οndragstart=function(){return false};
document.οnmοusemοve=e_move;
document.οnmοuseup=undrag
};
function e_move(e){
e=e||event;
var cl=range(_left+e.clientX-x,'x'),ct=range(_top+e.clientY-y,'y');
win.style.left=cl+'px';
win.style.top=ct+'px';
w.getSelection?w.getSelection().removeAllRanges():
document.selection.empty();
};
function undrag(){this.οnmοusemοve=null};
};

function $(x){return typeof x=='string'?document.getElementById(x):x};
var google=$("google"),main=$('main');
var max={
x:google.offsetWidth-main.offsetWidth-4,
y:google.offsetHeight-main.offsetHeight-4
}
Drag(
main,
false,
function(x,type){return Math.max(0,Math.min(max[type],x))}
)
//]]>
</script>
</html>


[/code]


或者另外一种方法:

[code]

<!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=gb2312" />
<title>无标题文档</title>
<style>
#big{ border:1px solid #FF3300; width:300px; height:300px; position:relative;}
#small{ background:#99CC00; width:50px; height:50px; position:absolute; cursor: move;}
</style>
<script language="javascript">
function small_down(e){
var obig=document.getElementById("big");
var osmall=document.getElementById("small");
var e=e||window.event;

document.οnmοusemοve=small_move;
document.οnmοuseup=small_up;

osmall.startX=e.clientX-osmall.offsetLeft;
osmall.startY=e.clientY-osmall.offsetTop;
}
function small_move(e){
var obig=document.getElementById("big");
var osmall=document.getElementById("small");
var e=e||window.event;

osmall.style.left=e.clientX-osmall.startX+"px";
osmall.style.top=e.clientY-osmall.startY+"px";

if(e.clientX-osmall.startX<=0){ osmall.style.left=0+"px";}
if(e.clientY-osmall.startY<=0){ osmall.style.top=0+"px";}
if(e.clientX-osmall.startX>=250){ osmall.style.left=250+"px";}
if(e.clientY-osmall.startY>=250){ osmall.style.top=250+"px";}
}
function small_up(){
document.οnmοusemοve="";
document.οnmοuseup="";
}
</script>
</head>
<body>
<div id= "big">
<div id="small" οnmοusedοwn="small_down(event)"></div>
</div>
</body>
</html>


[/code]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值