js上下浮动广告

<!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>
<title>div+css</title>
<meta http-equiv=Content-Type content="text/html; charset=gb2312">
<style type="text/css">
#demo1{left:22px;position:absolute;top:180px;width:120px;height:200px;background:red;}
</style>
</head>
<BODY>
<P>&nbsp;</P>
<P>&nbsp;</P>
<P>&nbsp;</P>
<P>&nbsp;</P>
<P>&nbsp;</P>
<P>&nbsp;</P>
<P>&nbsp;</P>
<P>&nbsp;</P>
<P>&nbsp;</P>
<P>&nbsp;</P>
<P>&nbsp;</P>
<P>&nbsp;</P>
<P>&nbsp;</P>

<div id="demo1"></div>

<script type="text/javascript">
var num=0;
var getObj=document.getElementsByTagName("div")[0];//获取滚动对象;
function scrollAdver()
{
    var pageTop=document.documentElement.scrollTop;//滚动条距页面顶部距离;
    var demo=(pageTop-num);//这次减去上次增加demo像素;
    if(demo>0){demo=Math.ceil(demo)}else{demo=Math.floor(demo)};
    getObj.style.top=parseInt(getObj.currentStyle.top)+demo+"px";//用上次定位距离加上两次滚动距离的差得到当前的滚动距离;
    num=num+demo;//获取前一次滚动条的距离;
}
setInterval(scrollAdver,1);
</script>
</body>
</html>
posted @ 2011-06-28 14:33  $雨$  阅读(957)  评论(0编辑  收藏  举报