今天做了一个小小的js效果:让广告总在浏览器的右下角。代码如下:
html code:
<div id="RightDownImg" style="position:absolute; display:none;">
<div id="closeDiv" style="position:relative; width:18px; height:18px; cursor:pointer;display:block;background:url(http://web.duowan.com/s/i/close.png) no-repeat; *background: none;
filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='http://web.duowan.com/s/i/close.png' ,sizingMethod='crop');" οnclick="closeAd()" title="关闭"></div>
<a href="http://www.zonyou.com/dol/iReg.aspx?zyid=70&sid=814" target="_blank">
<img src="http://dol.wanjiaba.com/images/260x200_1.gif" id="fanimg" border="0" />
</a>
</div>
<script type="text/javascript" language="javascript" src="rd.js"></script>
js code:
var FanpaiExpand = null;
var ImgeDiv = null;
var CloseDiv = null;
window.onload = InitSet;
window.onscroll = SetRightAdPostion;
window.onresize = SetRightAdPostion;
function InitSet()
{
//设置宽和高
FanpaiExpand = document.getElementById("RightDownImg");
ImgeDiv = document.getElementById("fanimg");
CloseDiv = document.getElementById("closeDiv");
//alert(ImgeDiv.width);
FanpaiExpand.style.display="";
FanpaiExpand.style.width = ImgeDiv.width + "px" ;
FanpaiExpand.style.height = ImgeDiv.height + "px";
CloseDiv.style.left = ImgeDiv.width - parseInt(CloseDiv.style.width) + "px";
CloseDiv.style.top =parseInt(CloseDiv.style.height)+"px";
SetRightAdPostion();
}
function SetRightAdPostion()
{
//设置left 和 top
if(FanpaiExpand)
{
FanpaiExpand.style.left = document.documentElement.clientWidth + document.documentElement.scrollLeft - parseInt(FanpaiExpand.style.width)-10 + "px";
FanpaiExpand.style.top = document.documentElement.clientHeight + document.documentElement.scrollTop - parseInt(FanpaiExpand.style.height)-10 + "px";
}
}
function closeAd ()
{
FanpaiExpand.style.display = "none";
}
当把window.onload事件去掉,直接在js的最后调用InitSet方法的时候,就会出现问题,页面首次加载的时候取不到图片的大小,也就是ImgeDiv.width 和ImgeDiv.height,但刷新一下就可以了。还有如果在js代码里加一个alert后,图片的大小也取到了,开始还没反映过来,后来一想,哎!真笨啊,图片加载也要时间啊,图片还没加载完毕就去取它的大小当然取不到。只要把window的加载完毕事件改为图片加载完毕的事件就可以啦: window.onload 改为document.getElementById("fanimg").onload