本文實(shí)例講述了js實(shí)現(xiàn)簡單的左右兩邊固定廣告效果的方法。分享給大家供大家參考。具體分析如下:
大多數(shù)網(wǎng)站都有左右兩邊的固定廣告位,下面呢就是實(shí)現(xiàn)這個(gè)效果的最簡單的代碼,可能在ie下滾動(dòng)的時(shí)候會(huì)有一點(diǎn)抖動(dòng),這個(gè)問題以后再解決了,先實(shí)現(xiàn)再說。
要點(diǎn)一:
var adtop = adleft.offsetTop;
獲得元素距離上邊的位置,在滾動(dòng)的時(shí)候需要用到。
要點(diǎn)二:
<!DOCTYPE html><html><head><meta charset="utf-8" /><title>無標(biāo)題文檔</title><style>body{margin:0; padding:0}#adleft,#adright{width:30px;height:100px;padding:20px;font:14px/20px arial;text-align:center;background:#06c;position:absolute;cursor:pointer;color:#fff}#adleft{left:0; top:150px; }#adright{right:0; top:150px;}</style><script>window.onload = function(){ var adleft = document.getElementById("adleft"); var adright = document.getElementById("adright"); var adtop = adleft.offsetTop; window.onscroll = function(){  adleft.style.top = adtop + (document.documentElement.scrollTop || document.body.scrollTop) +"px";   adright.style.top = adtop + (document.documentElement.scrollTop || document.body.scrollTop) +"px";  }} </script></head><body style="height:2000px;"><h1>左右廣告</h1><div id="adleft">左邊廣告</div><div id="adright">右邊廣告</div></body></html>希望本文所述對大家的javascript程序設(shè)計(jì)有所幫助。
新聞熱點(diǎn)
疑難解答
網(wǎng)友關(guān)注