符合W3C标准布局的js对联跟随广告(js版+css)

 1、改进版 
   < div >
     < script  type ="text/javascript">
        lastScrollY = 0;
         function  heartBeat(){
         var  diffY;
         if  (document.documentElement && document.documentElement.scrollTop)
         diffY = document.documentElement.scrollTop;
         else  if  (document.body)
         diffY = document.body.scrollTop
         else
            { /*Netscape stuff*/ }  
         //alert(diffY);
        percent=.1*(diffY-lastScrollY);
         if (percent>0)percent=Math.ceil(percent);
         else  percent=Math.floor(percent);
        document.getElementById( "aleft" ).style.top = parseInt(document.getElementById( "aleft" ).style.top)+percent+ "px" ;
        document.getElementById( "afd" ).style.top = parseInt(document.getElementById( "aleft" ).style.top)+percent+ "px" ;
        document.getElementById( "afd" ).style.right =  "15px" ;
        lastScrollY=lastScrollY+percent;
         //alert(lastScrollY);
        }
     // 下面这段删除后,对联将不跟随屏幕而移动。
        window.setInterval( "heartBeat()" ,1);
     //-->
     script >
     < a  id ="aleft"  style =" position : absolute ; top : 175px ;  left : 15px ;  "> a >
     < a  href ="movie.aspx"  id ="afd"  class ="afd"  style =" position : absolute ; right : 15px ; top : 175px ; display : block ; width : 148px ; height : 72px ; background : url(images/introduce.png) ; "> a >
  div >

2、原来

DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>符合标准的正常工作的对联广告title>
<script type="text/javascript">
    lastScrollY = 0;
    function heartBeat(){
    var diffY;
    if (document.documentElement && document.documentElement.scrollTop)
        diffY = document.documentElement.scrollTop;
    else if (document.body)
        diffY = document.body.scrollTop
    else
        {/*Netscape stuff*/}
    
    //alert(diffY);
    percent=.1*(diffY-lastScrollY);
    if(percent>0)
        percent=Math.ceil(percent);
    else
        percent=Math.floor(percent);
    document.getElementById("leftDiv").style.top = parseInt(document.getElementById("leftDiv").style.top)+percent+"px";
    document.getElementById("rightDiv").style.top = parseInt(document.getElementById("leftDiv").style.top)+percent+"px";

    lastScrollY=lastScrollY+percent;
    //alert(lastScrollY);
    }

    //下面这段删除后,对联将不跟随屏幕而移动。
    window.setInterval("heartBeat()",1);
script>

<style type="text/css">
    /* 这里定义滴是页面高度,用来测试对联跟随效果滴,跟广告无关*/
    html,body{height:1000px;}
    #mm{height:1000px;}

    /* 这里是设置对联样式滴*/
    #leftDiv,#rightDiv{
       width:120px; /* 宽度*/
       height:250px; /* 高度*/
       background-color:#e5e5e5; /* 背景颜色*/
       border:1px solid #ddd; /* 边框样式*/
       position:absolute; /* 绝对定位激活*/
    }
style>
head>

<body>

<div id="leftDiv" style="top:120px; /* 距离顶部px */ left:2px; /* 距离左侧px */ ">
        左侧广告内容div>
<div id="rightDiv" style="top:120px; /* 距离顶部px */ right:2px; /* 距离右侧px */ ">
        右侧广告内容div>

<div id="mm">
div>
body>
html>

你可能感兴趣的:(JS)