/** * * 浮动广告js插件 * w 广告宽度 * h 广告高度 * x 广告横坐标 auto时移动显示 * y 广告纵坐标 auto时移动显示 * imgsrc 图片路径 * url 广告地址 * isfloat 是否浮动 * basepoint x坐标的基准点(1 左,2中,3右) * showclosebutton 是否显示关闭按钮 * */ function advertise(w,h,x,y,imgsrc,url,isfloat,basepoint,showclosebutton) { this._w = w; this._h = h; this._x = x.tolowercase(); this._y = y.tolowercase(); this._imgsrc = imgsrc; this._url = url; this.uid = advertisehelper.getid(); this.mbox = null; this._movertype = 0;//0 不移动 this._isfloat = isfloat; this._bp = basepoint; this._showcb = showclosebutton; this.steep=50; advertisehelper.instance[this.uid] = this; } advertise.prototype = { _createbox : function () { if(!document.getelementbyid(this.uid+"_mbox")){ var box = document.createelement("div"); box.id = this.uid+"mbox"; box.style.cursor = "pointer"; box.style.position = "absolute"; box.style.width = this._w+"px"; box.style.height= this._h+"px"; box.style.zindex="100000"; document.body.appendchild(box); var boxinhtml = ""; //添加关闭按钮 if(this._showcb){ var eventstr = "advertisehelper.instance['"+this.uid+"'].close()"; var btnstyle ='width:23px;height:17px;position:absolute;top:0px;right:0px;background-image:url(/resource/images/close.png)'; boxinhtml += "
"; } box.innerhtml=boxinhtml; this.mbox = box; }else{ this.mbox = document.getelementbyid(this.uid+"_mbox"); } }, _static : function () { var y = this._y; var x = this._x=="auto" ? 0: parseint(this._x); if(this._bp == "2") { x = (document.documentelement.scrollwidth / 2) + x; }else if(this._bp == "3") { x = document.documentelement.clientwidth + x ; } if (this._y == 'auto'){ y = 0; } this.mbox.style.left = x+"px"; this.mbox.style.top = y+"px"; /*跟随滚动条移动*/ var uid = this.uid; addeventload(window, "scroll", function(){ advertisehelper.instance[uid]._bodyscroll(); }); }, _float : function () { var x = this._x; var y = this._y; var scrolltop = document.documentelement.scrolltop || window.pageyoffset || document.body.scrolltop; var scrollleft = document.documentelement.scrollleft || window.pagexoffset || document.body.scrollleft; if (this._x == 'auto' && this._y != 'auto'){ //横向移动 this._movertype = 1; x = scrollleft; } else if (this._y == 'auto' && this._x != 'auto'){ //纵向移动 this._movertype = 2; y = scrolltop; } else { //双向移动 this._movertype = 3; x = (math.random() * (document.documentelement.clientwidth-this._w)); y = (math.random() * (document.documentelement.clientheight-this._h)); } this.mbox.style.left = x+"px"; this.mbox.style.top = y+"px"; var uid = this.uid; this._mover(); this.mbox.onmousemove = function(){clearinterval(this.mover);}; this.mbox.onmouseout = function(){ advertisehelper.instance[uid]._mover();}; var uid = this.uid; addeventload(window, "scroll", function(){ advertisehelper.instance[uid]._bodyscroll(); }); }, _bodyscroll : function () { if(this.mbox.style.display == "none"){return;} var scrolltop = document.documentelement.scrolltop || window.pageyoffset || document.body.scrolltop; var scrollleft = document.documentelement.scrollleft || window.pagexoffset || document.body.scrollleft; var top = left = ""; if(!this.mbox._isfloat && this._bp!="2"){ if(!this.oldscrolltop){this.oldscrolltop = 0;} if(!this.oldscrollleft){this.oldscrollleft = 0;} top = (this.mbox.offsettop + scrolltop - this.oldscrolltop); left = (this.mbox.offsetleft + scrollleft - this.oldscrollleft); this.mbox.style.top = top+"px"; this.mbox.style.left = left+"px"; this.oldscrolltop = scrolltop; this.oldscrollleft = scrollleft; } else { top = (this._y == "auto" ? 0 : parseint(this._y)) + (scrolltop); this.moveelement(this.mbox.id,"",top,10); } }, _mover : function () { if(this.mbox.mover){clearinterval(this.mbox.mover)} if(this._movertype==0){return;} var scrolltop = document.documentelement.scrolltop || window.pageyoffset || document.body.scrolltop; var scrollleft = document.documentelement.scrollleft || window.pagexoffset || document.body.scrollleft; var x = this.mbox.offsetleft; var y = this.mbox.offsettop ; if(this._movertype==3 || this._movertype == 1){ if(x + this._w >= document.documentelement.clientwidth + scrollleft - 5 ){ this.isxjia = false; } else if(x == scrollleft ){this.isxjia = true;} x = this.isxjia ? x+1 : x-1; } if(this._movertype==3 || this._movertype == 2){ if(y + this._h >= document.documentelement.clientheight + scrolltop - 5 && y + this._h >=0 ){ this.isyjia = false; } else if( y == scrolltop){this.isyjia = true;} y = this.isyjia ? y+1 : y-1; } this.mbox.style.left = (x) + "px"; this.mbox.style.top = (y) + "px"; this.mbox.mover = setinterval("advertisehelper.instance['"+this.uid+"']._mover()",this.steep); }, close : function () { if(this.mbox.mover){clearinterval(this.mbox.mover)} this.mbox.style.display="none"; }, show : function () { this._createbox(); if(this._isfloat) this._float(); else this._static(); }, moveelement : function (elementid, final_x,final_y,interval) { if (!document.getelementbyid(elementid)) {return false; } var elem = document.getelementbyid(elementid); if (elem.movement) { clearinterval(elem.movement);} if (!elem.style.top) { elem.style.top = "0px"; } if (!elem.style.left) { elem.style.left = "0px"; } if(final_x != ""){ var xpos = parseint(elem.style.left); if (xpos < final_x) { var dist = math.ceil((final_x - xpos)/10); xpos = xpos + dist; } if (xpos > final_x) { var dist = math.ceil((xpos - final_x)/10); xpos = xpos - dist; } elem.style.left = xpos + "px"; } if(final_y != ""){ var ypos = parseint(elem.style.top); if (ypos < final_y) { var dist = math.ceil((final_y - ypos)/10); ypos = ypos + dist; } if (ypos > final_y) { var dist = math.ceil((ypos - final_y)/10); ypos = ypos - dist; } elem.style.top = ypos + "px"; } if (xpos == final_x && ypos == final_y) { return true; } var eventstr = "advertisehelper.instance['"+this.uid+"']"; var repeat = eventstr + ".moveelement('"+elementid+"','"+final_x+"','"+final_y+"',"+interval+")"; elem.movement = setinterval(repeat,interval); } } var advertisehelper = { count: 0, instance: {}, getid: function() { return '_advertise-' + (this.count++); } }; function addeventload(target, type, func){ if (target.addeventlistener) target.addeventlistener(type, func, true); else if (target.attachevent) target.attachevent("on" + type, func); else target["on" + type] = func; }