JavaScript 對聯廣告、漂浮廣告封裝類(IE,FF,Opera,Safari,Chrome)

來源:互聯網
上載者:User

/*
對聯廣告調用
第一個參數為ID,第二個參數圖片,第三個參數廣告距離頂部的距離,
第四個參數表示左右(true代表左,false代表右),第五個參數對聯廣告的寬度
*/
new float_ad("ad_l","<img src='/System/uploads/allimg/090504/2346400.gif' />",50,true,64);
new float_ad("ad_r","<img src='/System/uploads/allimg/090504/2346400.gif' />",50,false,64);

/*
漂浮廣告調用
第一個參數中的url代表漂浮廣告的圖片URL地址(必選項),link連結到頁面網址,alt圖片提示文字,width圖片的寬度(可選項,預設120),heihgt圖片的高度(可選項,預設120)
第二個參數中的代表漂浮廣告的初始位置,由參數left和top指定
第三個參數代表浮動速度,0為靜止,越小浮動速度越快
*/
new move_ad({url:"/System/uploads/allimg/090504/2346400.gif",link:"http://jzyouth.org.cn/html/zixun/zuixingonggao/2009327/zxgg47926848.html",alt:"關於招募“2009中國·大連國際櫻桃節”志願者的通知",width:200,height:200},{left:40,top:160},20);

//核心代碼
function $(element){
    if(arguments.length>1){
        for(var i=0,elements=[],length=arguments.length;i<length;i++)
            elements.push($(arguments[i]));
        return elements;
    }
    if(typeof element=="string")
        return document.getElementById(element);
    else
        return element;
}
Function.prototype.bind=function(object){
    var method=this;
    return function(){
        method.apply(object,arguments);
    }
}
var Class={
    create:function(){
        return function(){
            this.initialize.apply(this,arguments);
        }
    }
}
Object.extend=function(destination,resource){
    for(var property in resource){
        destination[property]=resource[property];   
    }
    return destination;
}
//對聯廣告類http://www.knowsky.com/article.asp?typeid=36
var float_ad=Class.create();
float_ad.prototype={
    initialize:function(id,content,top,left,width){
        document.write('<div id='+id+' style="position:absolute;">'+content+'</div>');
        this.id=$(id);
        this.top=top;
        if(!!left){
            this.id.style.left="8px";   
        }else{
            this.id.style.left=(document.documentElement.clientWidth-width-8)+"px";
            window.onresize=function(){
                this.id.style.left=(document.documentElement.clientWidth-width-8)+"px";
            }.bind(this);
        }
        this.id.style.top=top+"px";
       
        this.interId=setInterval(this.scroll.bind(this),20);
    },
    scroll:function(){
        this.stmnStartPoint = parseInt(this.id.style.top, 10);
        this.stmnEndPoint =document.documentElement.scrollTop+ this.top;
        if(navigator.userAgent.indexOf("Chrome")>0){
            this.stmnEndPoint=document.body.scrollTop+this.top;   
        }
        if ( this.stmnStartPoint != this.stmnEndPoint ) {
                this.stmnScrollAmount = Math.ceil( Math.abs( this.stmnEndPoint - this.stmnStartPoint ) / 15 );
                this.id.style.top = parseInt(this.id.style.top, 10) + ( ( this.stmnEndPoint<this.stmnStartPoint ) ? -this.stmnScrollAmount : this.stmnScrollAmount )+"px";
        }
    }
}
//漂浮廣告類
var move_ad=Class.create();
move_ad.prototype={
    initialize:function(imgOption,initPosition,delay){
        this.imgOptions=Object.extend({url:"",link:"",alt:"",width:120,height:120},imgOption||{});
        this.adPosition=Object.extend({left:40,top:120},initPosition||{});
        this.delay =delay;
        this.step = 1;
        this.herizonFlag=true;
        this.verticleFlag=true;
        this.id="ad_move_sg";
        var vHtmlString="<div id='"+this.id+"' style='position:absolute; left:"+this.adPosition.left+"px; top:"+this.adPosition.top+"px; width:"+this.imgOptions.width+"px;";
        vHtmlString+=" height:"+this.imgOptions.height+"px; z-index:10;'><a href='"+this.imgOptions.link+"' target='_blank' title='"+this.imgOptions.alt+"'><img src='"+this.imgOptions.url+"' width='"+this.imgOptions.width+"' height='"+this.imgOptions.height+"' style='border:none;' alt='"+this.imgOptions.alt+"' /></a></div>";
        document.write(vHtmlString);
        this.id=$(this.id);
        this.intervalId=setInterval(this.scroll.bind(this),this.delay);
        this.id.onmouseover=this.stop.bind(this);
        this.id.onmouseout=this.start.bind(this);
    },
    scroll:function(){
        var L=T=0;
        var B=document.documentElement.clientHeight-this.id.offsetHeight;
        var R=document.documentElement.clientWidth-this.id.offsetWidth;
        this.id.style.left=this.adPosition.left+document.documentElement.scrollLeft+"px";
        this.id.style.top=this.adPosition.top+document.documentElement.scrollTop+"px";
        this.adPosition.left =this.adPosition.left + this.step*(this.herizonFlag?1:-1);
        if (this.adPosition.left < L) { this.herizonFlag = true; this.adPosition.left = L;}
        if (this.adPosition.left > R){ this.herizonFlag = false; this.adPosition.left = R;}
        this.adPosition.top =this.adPosition.top + this.step*(this.verticleFlag?1:-1);
        if(this.adPosition.top <= T){ this.verticleFlag=true; this.adPosition.top=T;}
        if(this.adPosition.top >= B){ this.verticleFlag=false; this.adPosition.top=B; }
    },
    stop:function(){
        clearInterval(this.intervalId);   
    },
    start:function(){
        this.intervalId=setInterval(this.scroll.bind(this),this.delay);   
    }
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.