具備兼容性的拖動(dòng)代碼
當(dāng)前位置:點(diǎn)晴教程→知識(shí)管理交流
→『 技術(shù)文檔交流 』
[p]特點(diǎn):[br]1、兼容 ie6、ff、opear(ie7還沒有機(jī)會(huì)測試)[br]2、拖動(dòng)流暢[br]3、起點(diǎn)與終點(diǎn)之間有過渡,使移動(dòng)更平滑(可調(diào))[/p]
[p][url=http://happyshow.org/sample/20070430/move.html]演示[/url][/p]
/*[br] author:misshjn[br] homepage:http://happyshow.org[br] date:2007-04-30[br][br] 拖動(dòng)開始[br]*/[br]function _getstyle(element,styleprop){[br] if (element.currentstyle){[br] var y = element.currentstyle[styleprop];[br] }else if (window.getcomputedstyle){[br] var y = document.defaultview.getcomputedstyle(element,null).getpropertyvalue(styleprop.replace(/([a-z])/g,"-$1").tolowercase());[br] }[br] return y;[br]}[br]function _elementonmousedown(evt,blockid){[br] var obj, temp;[br] obj=document.getelementbyid(blockid);[br] var x = evt.clientx ││ evt.pagex;[br] var y = evt.clienty ││ evt.pagey;[br] obj.startx=x-obj.offsetleft;[br] obj.starty=y-obj.offsettop;[br][br] var d = document.createelement("div");[br] d.style.position = "absolute";[br] d.style.width = obj.clientwidth + parseint(_getstyle(obj,"borderleftwidth"),10) + parseint(_getstyle(obj,"borderrightwidth")) -2 + "px";[br] d.style.height = obj.clientheight + parseint(_getstyle(obj,"bordertopwidth"),10) + parseint(_getstyle(obj,"borderbottomwidth")) -2 + "px";[br] d.style.border = "1px dashed #666";[br] d.style.top = _getstyle(obj,"top");[br] d.style.left = _getstyle(obj,"left");[br] d.style.zindex = "9999";[br] document.body.appendchild(d);[br] document.onmousemove=function(evt){[br] d.style.left= (evt?evt.pagex:event.clientx) - obj.startx + "px";[br] d.style.top= (evt?evt.pagey:event.clienty) - obj.starty + "px";[br] };[br] document.onmouseup=function(){[br] var objl = parseint(_getstyle(obj,"left"),10);[br] var objt = parseint(_getstyle(obj,"top"),10);[br] var obj2l = parseint(d.style.left,10);[br] var obj2t = parseint(d.style.top,10);[br][br] var todolist = [];[br] var level = 10; //元素移動(dòng)從起點(diǎn)到終點(diǎn)之間過渡的級(jí)數(shù),大于0的整數(shù)[br] var speed = 10; //毫秒,每次移動(dòng)的間隔時(shí)間,數(shù)字越大,動(dòng)畫感越強(qiáng),但跳躍感也越大[br] for (i=1; i<=level; i++){[br] todolist.push(function(t){[br] settimeout(function(){[br] obj.style.left = objl + (obj2l-objl)*(t/level) + "px";[br] obj.style.top = objt + (obj2t-objt)*(t/level) + "px";[br] if(t==i)todolist=null;[br] },speed*arguments[0]);[br] });[br] }[br] for (i=1; i<=level; i++){[br] todolist[i-1](i);[br] }[br] document.body.removechild(d);[br] document.onmousemove = null;[br] document.onmouseup = null;[br] };[br]}[br][br]/*[br] 拖動(dòng)結(jié)束[br]*/
網(wǎng)友評(píng)論
[li]
[b]ann[/b]說:
天哪,昨天tianya.cn來我的博客訪問居然有120次~!![br][br]原來我貼了百度空間圖片上去,沒有顯示,可能有些人找到屬性,直接到我博客欣賞了,嘻嘻,無心插柳柳成蔭.[i](2007-05-11 09:51:03)[/i][/li]
[li]
[b]mantou[/b]說:
拖動(dòng)到窗口最邊緣的時(shí)候就會(huì)出錯(cuò)了。。。[i](2007-05-26 17:27:37)[/i][/li]
[li]
[b]skyakira[/b]說:
增加一個(gè)[br]document.onselectstart=function(){[br]returnfalse;[br]}[br]可以防止拖動(dòng)時(shí)選中文字;[br][br]結(jié)束的時(shí)候[br]document.onselectstart=null;[i](2007-06-11 13:46:16)[/i][/li]
[li]
[b]skyakira[/b]說:
d.style.width=obj.clientwidth parseint(_getstyle(obj,"borderleftwidth"),10) parseint(_getstyle(obj,"borderrightwidth"))-2 "px";[br]d.style.height=obj.clientheight parseint(_getstyle(obj,"bordertopwidth"),10) parseint(_getstyle(obj,"borderbottomwidth"))-2 "px";[br][br]在頁面內(nèi)容很多的時(shí)候這兩句話錯(cuò)誤;沒找到具體原因,而且頁面內(nèi)容多了,那個(gè)過度的效果基本看不出來;[br][br]還有就是經(jīng)常移動(dòng)的div會(huì)粘到鼠標(biāo)上,單獨(dú)測試的時(shí)候沒任何問題;[br][br]引用這個(gè)之前引用了prototype.js'[br]mail:sky.zhao.crm[at]gmail.com[i](2007-06-11 13:58:45)[/i][/li]
[li]
[b]skyakira[/b]說:
if($("linediv"))[br]{[br]document.body.removechild($("linediv"));[br]}[br]vard=document.createelement("div");[br]d.setattribute("id","linediv");[br]d.style.position="absolute";[br][br]增加了虛框存在的判斷,防止鼠標(biāo)滑出頁面以后,導(dǎo)致事件丟失,無法取消虛框的問題;[i](2007-06-11 17:18:04)[/i][/li]
[li]
[b]skyakira[/b]說:
d.style.cursor="move";[br][br]繼續(xù)增加一個(gè)樣式,使鼠標(biāo)在虛框移動(dòng)時(shí)保持移動(dòng)狀態(tài);[br]mail:sky.zhao.crm[at]gmail.com[i](2007-06-12 09:08:54)[/i][/li]
[li]
[b]安靜[/b]說:
happyshow 網(wǎng)頁改版啦~~恭喜一下快樂笛子~~一如繼往支持你哦,大哥.[i](2007-06-19 16:46:21)[/i][/li]
[code]
該文章在 2010/4/30 17:09:08 編輯過 |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |