国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

JS實現的自定義網頁拖動類

2019-11-20 11:20:05
字體:
來源:轉載
供稿:網友

本文實例講述了JS實現的自定義網頁拖動類。分享給大家供大家參考,具體如下:

先來看運行效果截圖如下:

在線演示地址如下:

http://demo.VeVB.COm/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參數,返回帶有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){//參數obj默認為o本身,可以傳其它參數以確定要移動的對象  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程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 江源县| 南丹县| 宁晋县| 枞阳县| 奉新县| 平湖市| 龙泉市| 延川县| 大荔县| 新巴尔虎左旗| 固始县| 改则县| 茌平县| 民勤县| 平阳县| 北辰区| 嘉义市| 靖边县| 汉寿县| 拉孜县| 花垣县| 班戈县| 来凤县| 正宁县| 万源市| 奎屯市| 扎鲁特旗| 沙洋县| 类乌齐县| 宜兰市| 焦作市| 万安县| 象山县| 伊金霍洛旗| 乌恰县| 柞水县| 卢湾区| 乌审旗| 平顶山市| 高平市| 湖北省|