js源码--层动态变化

来源:互联网 发布:木工建模软件 编辑:程序博客网 时间:2024/05/17 01:12
<html>
<head>
<style type="text/css">
body 
{
height:
100%;
}

#window, #window1 
{
z
-index:99;
position:absolute;
height:100px;
width:100px;
background
-color:#CCCCCC;
}

#window1 
{
z
-index:100;
background
-color:#00FF00;
}

#show 
{
height:2000px;
width:1px;
}

</style>
<script type="text/javascript">
var scrollGS = {
move : 
function (o) {
if (document.getElementById(o.id)) {
var left = document.body.scrollLeft - o.oscrollLeft;
var top = document.body.scrollTop - o.oscrollTop;
/*
滑动特效,暂时无做。。。
var leftFH = left / Math.abs(left);
var topFH = top / Math.abs(top);
*/

o.style.left 
= o.offsetLeft + left;
+ document.body.clientWidth - o.oclientWidth + "px";
o.style.top 
= o.offsetTop + top;
+ document.body.clientHeight - o.oclientHeight + "px";
o.oscrollLeft 
= document.body.scrollLeft;
o.oscrollTop 
= document.body.scrollTop;
o.oclientWidth 
= document.body.clientWidth;
o.oclientHeight 
= document.body.clientHeight;
window.status 
= o.style.top;
window.timeout(scrollGS.move, 
50, o);
}

}
,

closemove : 
function (o) {
if (o.offsetTop - o.oscrollTop - document.body.clientHeight < 0{
o.style.top 
= o.offsetTop + 1 + "px";
window.timeout(scrollGS.closemove, 
10, o);
}
 else {
window.status 
= "已经移除.."
document.body.removeChild(o);
}

}
,

add : 
function (w) {
w.oscrollLeft 
= document.body.scrollLeft;
w.oscrollTop 
= document.body.scrollTop;
w.oclientWidth 
= document.body.clientWidth;
w.oclientHeight 
= document.body.clientHeight;
w.style.left 
= w.oscrollLeft + document.body.clientWidth - w.offsetWidth;
w.style.top 
= w.oscrollTop + document.body.clientHeight - w.offsetHeight;
w.onmousedown 
= function () {
scrollGS.closemove(w);
}
;
scrollGS.move(w);
}

}
;

(
function () {
window.timeout 
= function (a, b) {
var fun = a;
if ("function" == typeof a) {
var arg = Array.prototype.slice.call(arguments, 2);
fun 
= function () {
a.apply(
null, arg);
}

}

window.setTimeout(fun, b);
}

}
)();
window.onload 
= function () {
scrollGS.add(document.getElementById(
"window"));
scrollGS.add(document.getElementById(
"window1"));
}

</script>
</head>
<body>
<div id="window"></div>
<div id="window1"></div>
<div id="show"></div>
</body>
</html>

 今天偶然看到一个帖子,如果是我的写法会

<html>
<head>
<style type="text/css">
body 
{
height:
100%;
}

#window, #window1 
{
z
-index:99;
position:absolute;
height:100px;
width:100px;
background
-color:#CCCCCC;
}

#window1 
{
z
-index:100;
background
-color:#00FF00;
}

#show 
{
height:2000px;
width:1px;
}

</style>
<script type="text/javascript">
window.onload 
= function () {
add(document.getElementById(
"window"));
add(document.getElementById(
"window1"));
}


function add(w) {
w.oscrollLeft 
= document.body.scrollLeft;
w.oscrollTop 
= document.body.scrollTop;
w.oclientWidth 
= document.body.clientWidth;
w.oclientHeight 
= document.body.clientHeight;
w.style.left 
= w.oscrollLeft + document.body.clientWidth - w.offsetWidth;
w.style.top 
= w.oscrollTop + document.body.clientHeight - w.offsetHeight;
w.onmousedown 
= function () {
closemove(w);
}

move(w);
}


function closemove(o) {
if (o.offsetTop - o.oscrollTop - document.body.clientHeight < 0{
o.style.top 
= o.offsetTop + 1 + "px";
window.timeout(closemove, 
10, o);
}

else {
window.status 
= "已经移除.."
document.body.removeChild(o);
}

}


function move (o) {
if (document.getElementById(o.id)) {
var left = document.body.scrollLeft - o.oscrollLeft;
var top = document.body.scrollTop - o.oscrollTop;
/*
滑动特效,暂时无做。。。
var leftFH = left / Math.abs(left);
var topFH = top / Math.abs(top);
*/

o.style.left 
= o.offsetLeft + left;
+ document.body.clientWidth - o.oclientWidth + "px";
o.style.top 
= o.offsetTop + top;
+ document.body.clientHeight - o.oclientHeight + "px";
o.oscrollLeft 
= document.body.scrollLeft;
o.oscrollTop 
= document.body.scrollTop;
o.oclientWidth 
= document.body.clientWidth;
o.oclientHeight 
= document.body.clientHeight;
window.status 
= o.style.top;
window.timeout(move, 
50, o);
}

}


(
function () {
window.timeout 
= function (a, b) {
var fun = a;
if ("function" == typeof a) {
var arg = Array.prototype.slice.call(arguments, 2);
fun 
= function () {
a.apply(
null, arg);
}

}

window.setTimeout(fun, b);
}

}
)();
</script>
</head>
<body>
<div id="window"></div>
<div id="window1"></div>
<div id="show"></div>
</body>
</html>


虽然差不多但是感觉需时越兔的结果性更好一些,确实是值得学习,非常棒的事例

原创粉丝点击