Javascript 广告显示封装类

by 清泉 15. 十月 2008 08:50
        在各相对大一点的网站中都有不少JS广告,下面是一个广告显示的JS封装类。 


下面是Javascript 广告显示封装类
// ad.js
//广告项目
function AdItem(url, link, title)
{
    this.imgURL = url;
    this.link = link;
    this.title = title;
}

function AdShower(uiImg, uiText, spanSeconds)
{
    //属性
    this.spanSeconds = spanSeconds;
    this.uiImg = uiImg;
    this.uiText = uiText;
   
   
    //私有成员
    this.items = [];
    this.adNum=0; 
    this.theTimer = null;
   
   
    this.setTransition = function() 
    { 
        if (document.all) 
        { 
            this.uiImg.filters.revealTrans.Transition=23;//Math.floor(Math.random()*23) 
            this.uiImg.filters.revealTrans.apply(); 
        } 
    } 
   
    this.playTransition = function () 
    { 
        if (document.all) 
        this.uiImg.filters.revealTrans.play() 
    } 

   
    this.jump2url = function() 
    { 
        jumpUrl=this.items[this.adNum].link;jumpTarget='_blank'; 
        if (jumpUrl != '') 
        {
            if (jumpTarget != '') 
                  window.open(jumpUrl,jumpTarget); 
              else
                  location.href=jumpUrl; 
        } 
    } 
    this.displayStatusMsg = function () 
    { 
        window.status=this.items[this.adNum].title; 
        document.returnValue = true; 
    } 
}

//生成委托
AdShower.createDelegate = function(instance, method)
{
    return function()
    {
        method.apply(instance, arguments);
    }
}

//得到当前的广告
AdShower.prototype.currentAd = function()
{
    return this.items[this.adNum];
}

//添加一个广告
AdShower.prototype.appendAd = function(url, link, title)
{
    this.items.push(new AdItem(url,link,title));
}


//下一个广告
AdShower.prototype.nextAd = function()
{
    this.adNum = (++this.adNum) % 3;
   
    var bPlay = this.uiImg.src != '';
    if(bPlay)
    {
        this.setTransition(); 
    }
 
    this.uiImg.src=this.items[this.adNum].imgURL;
    this.uiImg.alt = '今日头条:'+ this.items[this.adNum].title;
    this.uiImg.onclick = AdShower.createDelegate(this,this.jump2url);
    this.uiImg.onmouseover = AdShower.createDelegate(this,this.displayStatusMsg);
    this.uiImg.onmouseout = function(){window.status = '';};
    this.uiText.innerHTML=this.items[this.adNum].link; 
    this.uiText.innerHTML= "" + this.items[this.adNum].title + "";
    if(bPlay)
    {
        this.playTransition(); 
    }
    this.theTimer=window.setTimeout(AdShower.createDelegate(this,this.nextAd),this.spanSeconds,this);
}

//预载入图片
AdShower.prototype.preLoadImages = function()
{
    var imgPre = []; 
    for (i=0;i<this.items.length;i++) 
    { 
        imgPre[i]=new Image(); 
        imgPre[i].src=this.items[i].imgURL; 
    } 
}

下面是HTML中的调用代码:
<!--begin:图片轮换开始--> 
<script language=JavaScript src='js.js'></script> 
<table border="0" cellpadding="0" cellspacing="0" id=newsTable style="position:relative; top:-10px;right:0px; font-size:12px;" align="center"> 
<tr><td><img style="FILTER: revealTrans(duration=1,transition=18); border:1px solid #146FA6"  border=0 name=imgUrlrotator ></td></tr> 
<tr><td bgcolor="#f0f0f0" align="center" style="border:1px solid #146FA6;" height="20">
<label id="linktext"></label>
</td></tr></table>
<script type="text/javascript">
    var adshower = new AdShower(document.getElementById('imgUrlrotator'),
                                document.getElementById('linktext'),
                                2000);
   
    adshower.appendAd("1.jpg","http://mokuai.net/#","山东高耗能GDP跃进难再现");
    adshower.appendAd("2.jpg","http://mokuai.net/#","山年收本QQ抢占沪黑市场");
    adshower.appendAd("1.jpg","http://mokuai.net/#","山东高耗能GDP跃进难再现");
   
    adshower.preLoadImages();
   
    adshower.nextAd();
/*   
*/
</script>
<!--begin:图片轮换结束--> 

Tags:

JS相关技术

添加评论



(将显示你的Gravatar头像)  

biuquote
微笑得意调皮害羞酷大笑惊讶发呆喜欢可怜尴尬闭嘴噘嘴皱眉伤心抓狂呕吐坏笑漫骂发怒
Loading



Supidea.com 晨飞的梦 @ All Rights Reserved. Powered by BlogYi.NET ver:1.8.0.0. 苏ICP备09011404号

关于博主

kamau
抱着美好的理想背井离乡,这酸甜苦辣只能默默忍受。既然选择了路,就得风雨兼程……

Calendar

<<  五月 2012  >>
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

在日历中查看文章

最近的评论

Comment RSS

声明

      本博所发一切破解相关附件只作学习研究交流之用,严禁用于商业用途,请在下载24小时内删除。
      本博所有网友评论不代表本博立场,版权归其作者所有。

© Copyright 2009