您的位置:首页 > Web前端 > JavaScript

javascript 鼠标拖动层。限制拖动范围

2008-07-16 17:08 253 查看
<div id="divPeiL" style="position:absolute;width:350px; height:80px;left:0px;top:10px; font-family:'宋体'; font-size:12px; border:1px solid #003399; background-color:#FFFFFF; "> </div>
<script language="javascript">
//-div 层可以拖动-------------
var dragapproved=false
var minrestore=0 //该变量表示窗口目前的状态,0表示初始化状态,1表示最大化状态
var initialwidth,initialheight
//若Client浏览器为IE5.0以上版本的
var ie5=document.all&&document.getElementById
//若Client浏览器为NetsCape6。0版本以上的
var ns6=document.getElementById&&!document.all

function iecompattest(){
return (!window.opera&&document.compatMode&&document.compatMode!="BackCompat")?document.documentElement:document.body }

function drag_drop(e){
if (ie5&&dragapproved&&event.button==1){
var temp_left=tempx+event.clientX-offsetx;
var temp_top=tempy+event.clientY-offsety;
if(temp_left>=600){temp_left=600} else if(temp_left<1){temp_left=1}
if(temp_top>=500){temp_top=500} else if(temp_top<1){temp_top=1}
document.getElementById("divPeiL").style.left=temp_left+"px";
document.getElementById("divPeiL").style.top=temp_top+"px";
}
else if (ns6&&dragapproved){
document.getElementById("divPeiL").style.left=tempx+e.clientX-offsetx+"px"
document.getElementById("divPeiL").style.top=tempy+e.clientY-offsety+"px"
}
}

function initializedrag(e){
offsetx=ie5? event.clientX : e.clientX
offsety=ie5? event.clientY : e.clientY

tempx=parseInt(document.getElementById("divPeiL").style.left)
tempy=parseInt(document.getElementById("divPeiL").style.top)

dragapproved=true
document.getElementById("divPeiL").onmousemove=drag_drop;
}

function closeit(){
try{
document.getElementById("divPeiL").style.display="none";
team[0]=null;team[1]=null;team[2]=null;
zhuAvg=0;keAvg=0;heAvg=0;
}
catch(exception){}
}
function stopdrag(){
dragapproved=false;
document.getElementById("divPeiL").onmousemove=null;
}

var html_divPl="<center>";
html_divPl=html_divPl+"<table width=/"350/" border=/"0/" cellpadding=/"0/" cellspacing=/"0/" align=/"center/" style=/"font-size:12px;/">";
html_divPl=html_divPl+"<tr align=/"center/" bgcolor=/"#ABD3FB/">";
html_divPl=html_divPl+"<td width=/"285/" id=/"/" align=/"left/" valign=/"middle/" onMousedown=/"initializedrag(event)/" onMouseup=/"stopdrag()/" onSelectStart=/"return false/" style=/"cursor:pointer;/" title=/"按下右键,移动鼠标。可以移动此窗口。/">按下右键,移动鼠标。可以移动此窗口。</td>";
html_divPl=html_divPl+"</tr></table></center>"
document.getElementById("divPeiL").innerHTML=html_divPl;
</script>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: