转摘_用JavaScript实现网页上的浮动广告

介绍一下简单的浮动广告(右侧)做法。
  以下这段代码可放在<body></body>之间,其间我加入了一些注释(即“//”后的文字及“<!—”“-->”之间的文字)。
<script language="JScript">
          
<!--
              
function initAd() 
              
{
                  document.all.AdLayer.style.posTop 
= -200;//设置onLoad事件激发以后,广告层相对于固定后的y方向位置
                  document.all.AdLayer.style.visibility = 'visible'//设置层为可见
                  MoveLayer('AdLayer');//调用函数MoveLayer()
                  
              }

              
              
function MoveLayer(layerName) 
              
{
                  
                  
var x = 1;//(window.screen.width -778)/2 + 728;//浮动广告层固定于浏览器的x方向位置
                  var y = 300;//浮动广告层固定于浏览器的y方向位置
                  var diff = (document.body.scrollTop + y - document.all.AdLayer.style.posTop)*.40;
                  
var y = document.body.scrollTop + y - diff;
                  eval(
"document.all." + layerName + ".style.posTop = y");
                  eval(
"document.all." + layerName + ".style.posRight = x");//移动广告层
                  setTimeout("MoveLayer('AdLayer');"20);//设置20毫秒后再调用函数MoveLayer()
                  
              }

              
              
function   showDiv()   
              
{   
                    
var   oDiv=document.getElementById("AdLayer");   
                    oDiv.style.display
=(oDiv.style.display=="none")?"block":"none";   
                }
 
          
//-->
            
</script>
            
<!--下面为一个ID为AdLayer的层(如ID名不为AdLayer,上面MoveLayer()内的AdLayer也要作相应修改),包括一张带链接的图片-->
<div align="center" id="AdLayer" style="Z-INDEX: 20; RIGHT: 1px; VISIBILITY: hidden; WIDTH: 180px; POSITION: absolute; TOP: 300px; HEIGHT: 259px">
                
<table id="aaaaa" cellSpacing="0" cellPadding="0" border="0" width="100%" bgColor="#BADEFF">
                    
<tr>
                        
<td align="center"><IMG src="images/index_float_tit.gif" border="0"></td>
                    
</tr>
                    
<tr>
                        
<td height="5"></td>
                    
</tr>
                    
<tr>
                        
<td><asp:datalist id="Datalist1" runat="server">
                                
<ItemTemplate>
                                    
<div align="center">
                                        
<table cellpadding="4" cellspacing="0" border="0" width="98%">
                                            
<tr>
                                                
<td class="90v">
                                                    
<IMG src="images/dot002.gif" border="0"><a  href='news_view.aspx?id=<%# DataBinder.Eval(Container.DataItem, "id") %>&index=1'  target =_blank class="ca_01">
                                                        
<%#DataBinder.Eval(Container.DataItem, "titles")%>
                                                    
</a>
                                                
</td>
                                            
</tr>
                                        
</table>
                                    
</div>
                                
</ItemTemplate>
                            
</asp:datalist></td>
                    
</tr>
                    
<tr>
                        
<td class="90v"><div align="right"><a  href="javascript:showDiv()">关闭</a></div>
                        
</td>
                    
</tr>
                
</table>
            
</div>

        在这里,你可以设置x、y的值来设定所固定层的位置,改变setTimeout("MoveLayer('AdLayer');", 20)中20的值为你希望调用MoveLayer()的时间间隔。还有要注意的是,使用的图片最好为透明背景的gif图,以使图片的背景颜色不至于遮住后面的的内容。

posted on 2007-05-14 11:25    阅读(624)  评论(0编辑  收藏  举报

导航