<script language="JavaScript" type="text/javascript"> //// // 點擊拖動視窗- 調用請使用: onmousedown='Move_obj("objId")' objId: 表示你想拖動的視窗ID //// var drag_=false; //擷取輸入:ID, 獲得objId對象 var D=new Function('obj','return document.getElementById(obj);'); var oevent=new Function('e','if (!e) e = window.event;return e'); var obj_move=false; function Move_obj(obj){ var x,y; obj_move=true; //註冊obj對象滑鼠按下事件 D(obj).onmousedown=function(e){ //取消拖動 if(!obj_move)return false; drag_=true; //with(this): 為地區語句設定預設對象。style等價於 this.style with(this){ //obj對象offsetLeft距離左邊距離 不包括marginLeft寬度, offsetTop同理 style.position="absolute";var temp1=offsetLeft;var temp2=offsetTop; //clientX 事件屬性返回當事件被觸發時滑鼠指標向對於瀏覽器頁面(或當前視窗)的水平座標。 x=oevent(e).clientX;y=oevent(e).clientY; document.onmousemove=function(e){ //取消拖動 if(!drag_)return false; with(this){ //obj距離左邊距離 + 外邊距 + 滑鼠移動距離 style.left=temp1-Number(style.marginLeft.substring(0,style.marginLeft.length-2))+oevent(e).clientX-x+"px"; style.top=temp2-Number(style.marginTop.substring(0,style.marginTop.length-2)) +oevent(e).clientY-y+"px"; } } } document.onmouseup=new Function("drag_=false;obj_move=false;"); } } </script>