JS實現(xiàn)的自定義網(wǎng)頁拖動類
更新時間:2015年11月06日 10:06:30 作者:企鵝
這篇文章主要介紹了JS實現(xiàn)的自定義網(wǎng)頁拖動類,涉及頁面元素響應(yīng)鼠標(biāo)事件動態(tài)改變屬性的相關(guān)實現(xiàn)技巧,需要的朋友可以參考下
本文實例講述了JS實現(xiàn)的自定義網(wǎng)頁拖動類。分享給大家供大家參考,具體如下:
先來看運行效果截圖如下:

在線演示地址如下:
http://demo.jb51.net/js/2015/js-zdy-web-drug-pic-style-codes/
具體代碼如下:
<!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>
<script type="text/javascript">
var d=document;//給document對象一個通用的事件偵聽方法
d.addListener=function(e,f,b){
this.attachEvent?this.attachEvent('on'+e,f):this.addEventListener(e,f,b);
}
d.removeListener=function(e,f,b){
this.detachEvent?this.detachEvent('on'+e,f):this.removeEventListener(e,f,b);
}
function $(){//接收一個id參數(shù),返回帶有startDrag方法的對象
var o=document.getElementById(arguments[0]);
o.addListener=function(e,f,b){
this.attachEvent?this.attachEvent('on'+e,f):this.addEventListener(e,f,b);
}
o.removeListener=function(e,f,b){
this.detachEvent?this.detachEvent('on'+e,f):this.removeEventListener(e,f,b);
}
o.startDrag=function(obj){//參數(shù)obj默認(rèn)為o本身,可以傳其它參數(shù)以確定要移動的對象
var obj=obj?obj:o;
var sx,sy;
o.style.cursor="move";
o.addListener("mousedown",function(e){
e||event;
if(e.button==1||e.button==0){
sx=e.clientX-obj.offsetLeft;sy=e.clientY-obj.offsetTop;
d.addListener("mousemove",move,false);
d.addListener("mouseup",stopDrag,false);
}
},false);
var stopDrag=function(){
d.removeListener("mousemove",move,false);
d.removeListener("mouseup",stopDrag,false);
}
var move=function(e){
e||event;
window.getSelection ? window.getSelection().removeAllRanges() :
document.selection.empty();
if(e.preventDefault)e.preventDefault();//這兩句便是解決firefox拖動問題的.
with (obj.style){
position="absolute"
left=e.clientX-sx+"px";
top=e.clientY-sy+"px";
}
}
}
return o;
}
window.onload=function(){$("ok").startDrag($("os"))}//本例中拖動ok元素,移動其父元素
</script>
<style type="text/css">
*{margin:0;padding:0}
#ok{width:215px;height:170px;background:url(images/sample1.gif)}
#os{width:400px;height:300px;background:#09f;left:300px}
#os2{width:400px;height:300px;background:#f90;}
</style>
</head>
<body>
<div id="os"><p id="ok"></p></div>
<div id="os2"></div>
</body>
</html>
希望本文所述對大家JavaScript程序設(shè)計有所幫助。
您可能感興趣的文章:
- JS實現(xiàn)超簡單的鼠標(biāo)拖動效果
- JS響應(yīng)鼠標(biāo)點擊實現(xiàn)兩個滑塊區(qū)間拖動效果
- JS實現(xiàn)左右拖動改變內(nèi)容顯示區(qū)域大小的方法
- js實現(xiàn)div拖動動畫運行軌跡效果代碼分享
- avalon js實現(xiàn)仿微博拖動圖片排序
- JS實現(xiàn)彈出浮動窗口(支持鼠標(biāo)拖動和關(guān)閉)實例詳解
- JS實現(xiàn)動態(tài)移動層及拖動浮層關(guān)閉的方法
- JS實現(xiàn)可縮放、拖動、關(guān)閉和最小化的浮動窗口完整實例
- js用拖動滑塊來控制圖片大小的方法
- Js可拖拽放大的層拖動特效實現(xiàn)方法
- JS+CSS實現(xiàn)可拖動的彈出提示框
- js實現(xiàn)圖片拖動改變順序附圖
相關(guān)文章
JavaScript中使用stopPropagation函數(shù)停止事件傳播例子
這篇文章主要介紹了JavaScript中使用stopPropagation函數(shù)停止事件傳播例子,即阻止事件冒泡的一個方法,需要的朋友可以參考下2014-08-08
mpvue 頁面預(yù)加載新增preLoad生命周期的兩種方式
這篇文章主要介紹了mpvue 頁面預(yù)加載新增preLoad生命周期的兩種方式,本文重點給大家講解了第一種方式,需要的朋友可以參考下2019-10-10
JavaScript中blob對象和file對象的區(qū)別及相互轉(zhuǎn)換實例
在JavaScript中,File和Blob是Web?API提供的兩個重要對象,用于處理文件和二進(jìn)制數(shù)據(jù),這篇文章主要介紹了JavaScript中blob對象和file對象的區(qū)別及相互轉(zhuǎn)換的相關(guān)資料,需要的朋友可以參考下2025-04-04
javascript自定義in_array()函數(shù)實現(xiàn)方法
這篇文章主要介紹了javascript自定義in_array()函數(shù)實現(xiàn)方法,涉及javascript數(shù)組的遍歷與查找相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-08-08

