欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

js 对联广告、漂浮广告封装类(IE,FF,Opera,Safari,Chrome

程序员文章站 2023-12-01 14:36:34
/* 对联广告调用 第一个参数为id,第二个参数图片,第三个参数广告距离顶部的距离, 第四个参数表示左右(true代表左,false代表右),第五个参数对联广告的宽度 */ new float_ad(...
/*
对联广告调用
第一个参数为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:"https://www.jb51.net",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;
}
//对联广告类
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);
}
}