有固定宽度页面的背景广告JS代码
作者: 青锋幽灵
日期: 2009-07-06 15:37:33
阅读: http://ghost.cmsdream.com/index.php?load=read&id=654916
以下是已经封装的JS类,点击这里查看效果。
JavaScript代码
-
-
-
- var backgroundAd = {
- Div: null,
- IE: 10,
- CD: new Array(1),
- AD: new Array(1),
- Config: {
- adTime: {
- begin: "2009/7/15 00:00:00",
- end: "2009/7/16 23:59:59"
- },
- Type: 0,
- minWidth: 930,
- totalWidth: 0,
- adWidth: 283,
- adImage:{
- left: 'l.gif',
- right: 'r.gif'
- },
- adURL: 'http://get.766.com/go.php?id=2303',
- adClick: 0,
- bgImage: 'bg.jpg',
- bgColor: '#6EE6FE',
- oldBackground: '',
-
- closeImage: {
- src: 'close.jpg',
- width: 13,
- height: 13
- }
- },
-
- createAd: function(u){
- var self = this;
- var h = document.body.clientHeight;
- var div = document.createElement('div');
- with(div.style){
- if(this.Config.adClick){
- cursor = 'pointer';
- }
- position = 'absolute';
- width = this.Config.adWidth + 'px';
- height = h + 'px';
- backgroundImage = "url(" + this.Config.adImage[u] + ")";
- backgroundRepeat = 'no-repeat';
- backgroundPosition = u + ' 0';
- backgroundAttachment = 'fixed';
- top = 0;
- zIndex = 0;
- eval(u + ' = '' + (-this.Config.adWidth) + 'px'');
- }
- if(this.Config.adClick){
- div.onclick = function(){
- window.open(self.Config.adURL, '_blank');
- };
- }
-
- return div;
- },
-
- createClose: function(u){
- var self = this;
- var a = document.createElement('a');
- a.href = '#close';
- with(a.style){
- position = 'absolute';
- width = self.Config.closeImage.width + 'px';
- height = self.Config.closeImage.height + 'px';
- top = 0;
- zIndex = 0;
- eval(u + ' = '' + (-this.Config.adWidth) + 'px'');
- }
- a.onclick = function(){
- var pp = self.Div.parentNode;
- pp.removeChild(self.Div);
- if(self.Config.oldBackground.length > 0)
- document.body.style.background = self.Config.oldBackground;
- return false;
- };
- a.innerHTML = '<img src="'+this.Config.closeImage.src+'" '+
- 'style="border:0;width:'+this.Config.closeImage.width+'px;height:'+this.Config.closeImage.height+'px;" />';
- return a;
- },
-
- Resize: function(){
- var w = document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.scrollWidth;
- var c = w - this.Config.totalWidth;
- c = c < 0 ? 0 : c;
- var w1 = (w - this.Config.minWidth - c) / 2 - 2;
- var w2 = w1 + 2;
-
- this.AD[0].style.left = (-w1) + 'px';
- this.AD[0].style.backgroundPosition = - (this.Config.adWidth - w2) + 'px 0';
- this.AD[1].style.right = (-w1) + 'px';
- if(this.IE <= 6)
- this.AD[1].style.backgroundPosition = (this.Config.adWidth - w2) + 'px 0';
- else
- this.AD[1].style.backgroundPosition = (w2 + this.Config.minWidth) + 'px 0';
-
- this.CD[0].style.left = (-w1) + 'px';
- this.CD[1].style.right = (-w1) + 'px';
- },
-
- Scroll: function(){
- var t = document.body.scrollTop ? document.body.scrollTop : document.documentElement.scrollTop;
- if(this.IE <= 6 && this.Config.Type > 0){
- this.AD[0].style.backgroundPosition = '0 ' + t + 'px';
- this.AD[1].style.backgroundPosition = 'right ' + t + 'px';
- }
- this.CD[0].style.top = t + 'px';
- this.CD[1].style.top = t + 'px';
- this.Resize();
- },
-
- Init: function(){
- var nd = new Date();
- var bd = new Date(Date.parse(this.Config.adTime.begin));
- var ed = new Date(Date.parse(this.Config.adTime.end));
- if(!(nd.getTime()>bd.getTime() && nd.getTime()<ed.getTime()))return;
-
- document.body.style.backgroundColor = this.Config.bgColor;
- document.body.style.background = 'url('+this.Config.bgImage+') fixed';
- if(navigator.appName == "Microsoft Internet Explorer"){
- var userAgent = navigator.userAgent;
- var s = 'MSIE';
- this.IE = parseFloat(userAgent.substr(userAgent.indexOf(s) + s.length));
- }
- this.Config.totalWidth = this.Config.minWidth + this.Config.adWidth * 2;
-
- this.Div = document.createElement('div');
- with(this.Div.style){
- position = 'relative';
- width = (this.Config.minWidth + 2) + 'px';
- margin = 'auto';
- top = '0';
- zIndex = '0';
- }
- document.body.insertBefore(this.Div, document.body.childNodes[0]);
- this.AD[0] = this.createAd('left');
- this.AD[1] = this.createAd('right');
- this.CD[0] = this.createClose('left');
- this.CD[1] = this.createClose('right');
- this.Div.appendChild(this.AD[0]);
- this.Div.appendChild(this.AD[1]);
- this.Div.appendChild(this.CD[0]);
- this.Div.appendChild(this.CD[1]);
-
- var self = this;
-
- if(document.all){
- window.attachEvent("onscroll", function(){self.Scroll();});
- window.attachEvent("onresize", function(){self.Resize();});
- }else{
- window.addEventListener("scroll", function(){self.Scroll();},true);
- window.addEventListener("resize", function(){self.Resize();},true);
- }
-
- this.Scroll();
- }
- };
调用方法:
JavaScript代码
- if(document.all){
- window.attachEvent("onload", function(){backgroundAd.Init();});
- }else{
- window.addEventListener("load", function(){backgroundAd.Init();}, true);
- }