有固定宽度页面的背景广告JS代码
作者:青锋幽灵 日期:2009-07-06 15:37
以下是已经封装的JS类,点击这里查看效果。
JavaScript代码
- /*
- Powered By CMSDream Copyright © All rights reserved.
- */
- var backgroundAd = {
- Div: null,
- IE: 10, /*IE版本*/
- 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, /*0-IE6 背景固定 不等于0-IE6背景浮动*/
- 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);
- }
标签: 背景广告
纯CSS解决DIV垂直居中的样式
作者:青锋幽灵 日期:2009-07-01 16:43
XML/HTML代码
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
- <title>无标题文档</title>
- <style type="text/css">
- .a {width:200px;height:200px;border:1px solid #333;display:table;vertical-align:middle;}
- .a p {position:relative;left:0;top:50%;width:100%;text-align:center;display:table-cell;vertical-align:middle;}
- .a img {height:50px;border:1px solid #f30;position:relative;top:-50%;}
- </style>
- </head>
- <div
- <body>
- <div class="a"><p><img src="http://www.google.com/intl/en/images/logo.gif" /></p></div>
- </body>
- </html>
- 1
