We often see large sites, during holidays or promotions, slide the whole page down and show a banner image at the very top, which slowly retracts after a few seconds. This is actually easy to do. The idea: put a div at the very top of the page with an initial height of zero so it stays hidden; once the page finishes loading, run a script that slowly increases the div’s height, holds for a few seconds, then shrinks it. Below is the code — you can copy it directly into an HTML file and run it.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<a href="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd</a>"> <html xmlns="<a href="http://www.w3.org/1999/xhtml">http://www.w3.org/1999/xhtml</a>"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>头部公告</title> <script type="text/javascript"> var intervalId = null; function slideAd(id,sState,nMaxHth,nMinHth) { this.id=id; this.state=sState || "down" ; this.maxHeigth=nMaxHth || 250; //公告高度 this.minHeigth=nMinHth || 0; var facter = 5; var obj = document.getElementById(id); if(intervalId != null) { window.clearInterval(intervalId); } function openBox() { var h = obj.offsetHeight; facter += 0.1; //下滑速度 var h = ((this.state == "down") ? (h +facter ) : (h - facter)) ; if(h >=this.maxHeigth) { h = this.maxHeigth; window.clearInterval(intervalId); } if(h < this.minHeigth) { h = 0; window.clearInterval(intervalId); } obj.style.height = h +"px"; } function closeBox() { slideAd(id,"up",nMaxHth,nMinHth); } intervalId = window.setInterval(openBox,60); try{ setTimeout(closeBox, 6000); //停留时间 } catch(e) {} } </script> </head> <body> <!--头部滑动公告开始--> <div id="top-gonggao" style="width:980px;height:0px;overflow:hidden;margin-left:auto;margin-right:auto;"> <a href="#" target="_blank"> <img src="<a href="http://www.neilren.com/gonggao/img/gonggao.jpg">http://www.neilren.com/gonggao/img/gonggao.jpg</a>" width="980" height="250" border="0"/> </a> </div> <!--头部滑动公告结束--> 页面内容<br/> 公告图片规范:宽度980px 高度:250px <script type="text/javascript">slideAd("top-gonggao")</script> </body> </html>
Just copy it and it works. Because of the syntax-highlighting plugin, the image link seems a bit off — I wrote src="<http://www.neilren.com/gonggao/img/gonggao.jpg>" but it shows up as an <a> tag.
