具备兼容性的拖动代码
当前位置:点晴教程→知识管理交流
→『 技术文档交流 』
[p]特点:[br]1、兼容 ie6、ff、opear(ie7还没有机会测试)[br]2、拖动流畅[br]3、起点与终点之间有过渡,使移动更平滑(可调)[/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] 拖动开始[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; //元素移动从起点到终点之间过渡的级数,大于0的整数[br] var speed = 10; //毫秒,每次移动的间隔时间,数字越大,动画感越强,但跳跃感也越大[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] 拖动结束[br]*/
网友评论
[li]
[b]ann[/b]说:
天哪,昨天tianya.cn来我的博客访问居然有120次~!![br][br]原来我贴了百度空间图片上去,没有显示,可能有些人找到属性,直接到我博客欣赏了,嘻嘻,无心插柳柳成荫.[i](2007-05-11 09:51:03)[/i][/li]
[li]
[b]mantou[/b]说:
拖动到窗口最边缘的时候就会出错了。。。[i](2007-05-26 17:27:37)[/i][/li]
[li]
[b]skyakira[/b]说:
增加一个[br]document.onselectstart=function(){[br]returnfalse;[br]}[br]可以防止拖动时选中文字;[br][br]结束的时候[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]在页面内容很多的时候这两句话错误;没找到具体原因,而且页面内容多了,那个过度的效果基本看不出来;[br][br]还有就是经常移动的div会粘到鼠标上,单独测试的时候没任何问题;[br][br]引用这个之前引用了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]增加了虚框存在的判断,防止鼠标滑出页面以后,导致事件丢失,无法取消虚框的问题;[i](2007-06-11 17:18:04)[/i][/li]
[li]
[b]skyakira[/b]说:
d.style.cursor="move";[br][br]继续增加一个样式,使鼠标在虚框移动时保持移动状态;[br]mail:sky.zhao.crm[at]gmail.com[i](2007-06-12 09:08:54)[/i][/li]
[li]
[b]安静[/b]说:
happyshow 网页改版啦~~恭喜一下快乐笛子~~一如继往支持你哦,大哥.[i](2007-06-19 16:46:21)[/i][/li]
[code]
该文章在 2010/4/30 17:09:08 编辑过 |
关键字查询
相关文章
正在查询... |