神奇简洁的拖动js代码

在蓝色论坛发现这个,有点看不懂,研究一下先。
测试地址:code

<!--
* { font-size:12px; font-family:Verdana,宋体; }
html { margin:0px; padding:0px; overflow:auto; }
body { margin:0px; padding:15px; background-color:buttonface; }
#w { position:absolute; width:480px; height:270px; overflow:hidden; border:2px groove #281; cursor:default; -moz-user-select:none; }
#t { line-height:20px; height:20px; width:480px; overflow:hidden; background-color:#27C; color:white; font-weight:bold; border-bottom:1px outset blue; text-align:center; }
#winBody { height:248px; width:480px; overflow-x:hidden; overflow-y:auto; border-top:1px inset blue; padding:10px; text-indent:10px; background-color:white; }
-->
<div id="w">
<div id="t">Demo Win - Drag me</div>
<div id="winBody">Hello world</div>
</div>
<script type="text/javascript"><!--mce:0--></script>

把js代码单独拿出来,认真看一下就会发现原理很简单,用”()”把函数括起来然后在后面直接加”(参数)”运行函数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
(function(o,s,x,y,d){
	s = o.style;//id为w的元素直接充当这里的o了
	d = document;//
	o.onselectstart = function(){ return false; }; //阻止选择
	o.onmousedown = function(e){
		e = e||event;
		x = e.clientX-o.offsetLeft;
		y = e.clientY-o.offsetTop;
		d.onmousemove = function(e){
			e = e||event;
			s.left = e.clientX - x + "px";
			s.top = e.clientY - y + "px";
		}
		d.onmouseup = function(){ d.onmouseup = d.onmousemove = ""; }
	}
})(document.getElementById("w"))

No Response to 神奇简洁的拖动js代码 »

还没有任何评论。

Leave a Reply

Email address is not published

You should say a Chinese word to pass spam check. If you can not input Chinese, just copy 你好 and paste them into comment text box.