W3C标准下兼容的JS对联广告代码实现

作者:拾光时间:2026-10-26 11:14:44浏览:89

本文提供了在W3C标准下可运行的JS对联广告代码。通过JavaScript控制广告随滚动条移动,并包含关闭功能。代码经过修改以兼容不同浏览器,适用于需要左右两侧或单侧展示的广告场景。

W3C标准下兼容的JS对联广告代码实现

最终提供适配代码: 核心实现:

具体实现:

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);
} 
sidebar1= "<div id=leftDIV style='left:0px; PosITION:absolute; TOP:0px;'><embed src=2010L.swf width=150 height=400 quality=high pluginspage=http://www.macromedia.com/go/getflashplayer type=application/x-shockwave-flash scale=exactfit wmode=transparent id=EccoolAD></embed></div>"
sidebar2= "<div id=rightDIV style='right:0px; PosITION:absolute; TOP:0px;'><embed src=2010R.swf width=150 height=400 quality=high pluginspage=http://www.macromedia.com/go/getflashplayer type=application/x-shockwave-flash scale=exactfit wmode=transparent id=EccoolAD></embed></div> "
document.write(sidebar1);
document.write(sidebar2);
//若移除此段,对联广告将失去屏幕跟随效果。
window.setInterval("heartBeat()",1);
//--> 
function closeAD()
{ 
document.getElementById("leftDIV").style.display='none';
document.getElementById("rightDIV").style.display='none';
}

为规避冲突,优化后的代码:


鉴于客户仅需右侧广告,左侧区域留空。


具体实现:

lastScrollY=0;
function heartBeat2(){
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("leftDIV2").style.top=parseInt(document.getElementById("leftDIV2").style.top)+percent+"px";
document.getElementById("rightDIV2").style.top=parseInt(document.getElementById("leftDIV2").style.top)+percent+"px";
lastScrollY=lastScrollY+percent;
//alert(lastScrollY);
} 
sidebar1= "<div id=leftDIV2 style='left:0px; PosITION:absolute; TOP:0px;'></div>"
sidebar2= "<div id=rightDIV2 style='right:0px; PosITION:absolute; TOP:0px;'><embed src=http://www.pcqc.com.cn/web/rdgqcq/dl/images/rdcq.swf width=150 height=400 quality=high pluginspage=http://www.macromedia.com/go/getflashplayer type=application/x-shockwave-flash scale=exactfit wmode=transparent id=EccoolAD></embed></div> "
document.write(sidebar1);
document.write(sidebar2);
//若移除此段,对联广告将失去屏幕跟随效果。
window.setInterval("heartBeat2()",1);
//--> 
function closeAD()
{ 
document.getElementById("leftDIV2").style.display='none';
document.getElementById("rightDIV2").style.display='none';
}

经网络检索,另有一款优质代码


<!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> <style> html,body{margin:0;font-family:Arial;font-size:12px;} #ad1,#ad2{position:absolute;float:left;width:100px;overflow:hidden; } #ad2{right:0;} #killAd1,#killAd2{position:absolute;bottom:10px;right:10px;cursor:pointer;border:1px solid #990000;padding:0 5px;background:red;color:#fff; } </style>  </head> <body> <div id="ad1"> <span id="killAd1">x</span> <img src="/upload/201107/20110713115423968.jpg" /> </div> <div id="ad2"> <span id="killAd2">x</span> <img src="/upload/201107/20110713115423968.jpg" /> </div> <p style="height:20000px;"></p> </body> </html>

[Ctrl+A 全选 注:引入外部Js需再刷新一下页面才能执行]


Js对联广告代码


经典对联广告方案,基于JS实现,结构精简且通用性强,随滚动条自动适配,尺寸由CSS控制,便于修改,无需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> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>标准对联广告Js代码</title> <style type="text/css"> <!-- #lovexin12,#lovexin14{ width:90px; height:230px; background-color:#ededed; border:1px solid #ddd; } html,body{ height:1203px; } #mm{ height:1000px; } --> </style>  </head> <body> <div id="mm"></div> </body> </html> 

[Ctrl+A 全选 注:引入外部Js需再刷新一下页面才能执行]

作者最新文章