为了做好对联广告,从网上下过一些相关代码,都不是那么太好用,在老公的帮助下精简了JS代码,特整理出来以备将来拿来就用。(我觉得这个的好处就是可以自动适应宽屏和普屏分辨率,用不着为广告的定位费半天劲了。)
function hide(){ document.getElementById('adLeft').style.visibility='hidden'; document.getElementById('adRight').style.visibility='hidden'; } 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("adLeft").style.top=parseInt(document.getElementById("adLeft").style.top)+percent+"px"; document.getElementById("adRight").style.top=parseInt(document.getElementById("adLeft").style.top)+percent+"px"; lastScrollY=lastScrollY+percent; } if(window.screen.width==1280){ if(document.body.clientWidth>=1024){ suspendcode12="<DIV id=\"adLeft\" style='left:55px;POSITION:absolute;TOP:150px;'><object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0' width='90' height='270'><param name='movie' value='images/ad.swf' /><param name='quality' value='high' /><param name='wmode' value='transparent' /><embed src='images/ad.swf' width='90' height='270' quality='high' pluginspage='http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash' type='application/x-shockwave-flash' wmode='transparent'></embed></object><a href='#' onclick='hide();'><img src='images/close.gif' border='0' /></a></div>"; suspendcode14="<DIV id=\"adRight\" style='right:55px;POSITION:absolute;TOP:150px;'><object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0' width='90' height='270'><param name='movie' value='images/ad.swf' /><param name='quality' value='high' /><param name='wmode' value='transparent' /><embed src='images/ad.swf' width='90' height='270' quality='high' pluginspage='http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash' type='application/x-shockwave-flash' wmode='transparent'></embed></object><a href='#' onclick='hide();'><img src='images/close.gif' border='0' /></a></div>"; }else{ suspendcode12="<DIV id=\"adLeft\" style='left:5px;POSITION:absolute;TOP:150px;'><object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0' width='90' height='270'><param name='movie' value='images/ad.swf' /><param name='quality' value='high' /><param name='wmode' value='transparent' /><embed src='images/ad.swf' width='90' height='270' quality='high' pluginspage='http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash' type='application/x-shockwave-flash' wmode='transparent'></embed></object><a href='#' onclick='hide();'><img src='images/close.gif' border='0' /></a></div>"; suspendcode14="<DIV id=\"adRight\" style='right:5px;POSITION:absolute;TOP:150px;'><object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0' width='90' height='270'><param name='movie' value='images/ad.swf' /><param name='quality' value='high' /><param name='wmode' value='transparent' /><embed src='images/ad.swf' width='90' height='270' quality='high' pluginspage='http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash' type='application/x-shockwave-flash' wmode='transparent'></embed></object><a href='#' onclick='hide();'><img src='images/close.gif' border='0' /></a></div>"; } }else{ suspendcode12="<DIV id=\"adLeft\" style='left:5px;POSITION:absolute;TOP:150px;'><object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0' width='90' height='270'><param name='movie' value='images/ad.swf' /><param name='quality' value='high' /><param name='wmode' value='transparent' /><embed src='images/ad.swf' width='90' height='270' quality='high' pluginspage='http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash' type='application/x-shockwave-flash' wmode='transparent'></embed></object><a href='#' onclick='hide();'><img src='images/close.gif' border='0' /></a></div>"; suspendcode14="<DIV id=\"adRight\" style='right:5px;POSITION:absolute;TOP:150px;'><object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0' width='90' height='270'><param name='movie' value='images/ad.swf' /><param name='quality' value='high' /><param name='wmode' value='transparent' /><embed src='images/ad.swf' width='90' height='270' quality='high' pluginspage='http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash' type='application/x-shockwave-flash' wmode='transparent'></embed></object><a href='#' onclick='hide();'><img src='images/close.gif' border='0' /></a></div>"; } document.write(suspendcode12); document.write(suspendcode14); window.setInterval("heartBeat()",2);
<!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> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>滚动FLASH对联广告</title> <style> <!-- #adLeft,#adRight{ width:90px; height:270px; z-index:1000; border:1px solid #ccc; } #wrap { height:1000px; width:780px; border:1px solid #ccc; margin:0 auto } --> </style> </head> <body> <DIV id=wrap></DIV> <script language="javascript" src="js/adv.js"></script> </body> </html>
这里我引用了两个images/文件,ad.swf和close.gif,用到具体的地方相关DIV的属性还需要再酌情添加修改,这个懒不能偷。