Drag div in standard
<div id="md1"
style='position:absolute;width:200;height:200;background:black;left:0px;top:200px'
class="moveable">aa</div>
<div id="md2" style='position:absolute;width:200;height:200;background:blue;left:400px;top:200px' class="moveable">blue</div>
<script>
var divs=document.getElementsByTagName("div");
for(i in divs)if(divs[i].className=="moveable"){
if(!document.all){
// document.captureEvents(Event.MOUSEDOWN);
}
divs[i].onmousedown = mDown;
divs[i].onmousemove = mMove;
divs[i].onmouseup = mUp;
}
function mDown(e){
this.catched=true;
var x,y;
if(!document.all){
x=e.pageX;
y=e.pageY;
}else{
x=document.body.scrollLeft+event.clientX;
y=document.body.scrollTop+event.clientY;
}
this._x=x;
this._y=y;
this._cx=x-parseInt(this.style.left);
this._cy=y-parseInt(this.style.top);
}
function mMove(e){//alert(this.style.left);
if(this.catched){
var x,y;
if(!document.all){
x=e.pageX;
y=e.pageY;
}else{
x=document.body.scrollLeft+event.clientX;
y=document.body.scrollTop+event.clientY;
}
this.style.left=(x-this._cx)+"px";
this.style.top=(y-this._cy)+"px";
}
}
function mUp(){
this.catched=false;
}
</script>
一会儿去封装一下。
很常用的。
<div id="md2" style='position:absolute;width:200;height:200;background:blue;left:400px;top:200px' class="moveable">blue</div>
<script>
var divs=document.getElementsByTagName("div");
for(i in divs)if(divs[i].className=="moveable"){
if(!document.all){
// document.captureEvents(Event.MOUSEDOWN);
}
divs[i].onmousedown = mDown;
divs[i].onmousemove = mMove;
divs[i].onmouseup = mUp;
}
function mDown(e){
this.catched=true;
var x,y;
if(!document.all){
x=e.pageX;
y=e.pageY;
}else{
x=document.body.scrollLeft+event.clientX;
y=document.body.scrollTop+event.clientY;
}
this._x=x;
this._y=y;
this._cx=x-parseInt(this.style.left);
this._cy=y-parseInt(this.style.top);
}
function mMove(e){//alert(this.style.left);
if(this.catched){
var x,y;
if(!document.all){
x=e.pageX;
y=e.pageY;
}else{
x=document.body.scrollLeft+event.clientX;
y=document.body.scrollTop+event.clientY;
}
this.style.left=(x-this._cx)+"px";
this.style.top=(y-this._cy)+"px";
}
}
function mUp(){
this.catched=false;
}
</script>
一会儿去封装一下。
很常用的。
posted on 2005-08-27 22:04 civ3's .NET studying 阅读(358) 评论(1) 编辑 收藏 举报