`
xttifqqk
  • 浏览: 47433 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

基于JQuery的广告代码

阅读更多
/**
 * JQuery广告代码
 */
ad ={
	basePath: '',
	content: [],
	floatDivs: [],
	closeHtml : '<img class="close" width="66px" height="22px" src="'+this.basePath+'/html/images/close.gif">',
	showAD: function(){
		for(var i =0; i<this.content.length; i++){
			var adConfig = this.content[i];
			if(adConfig && adConfig.isOpen){ 
				this.generateHtml(adConfig);
			}
		}
		
		this.maxTop = jQuery("body").attr("clientHeight") + jQuery(window).scrollTop() - 1;  //设置浮动时的最大高度
		this.maxLeft = jQuery("body").attr("clientWidth") + jQuery(window).scrollLeft() - 1; //设置浮动时的最大宽度
		this.floatStrat();
	},
	generateHtml: function(o){	/*生成包含关闭的Html代码*/
		var adHtml = this.generateADHtml(o);
		var adDiv = jQuery("<div class='ad'/>");
		var divHeight = o.height + (o.allowClose? 22 : 0);
		adDiv.css({height: divHeight +"px", width: o.width+"px"});
		adDiv.top = o.top || (jQuery(window).height() - divHeight);
		var tableHtml = "";
		switch(o.position){
			case "left":
				adDiv.css({left:'0px', top:o.top+'px'});
				tableHtml = this.generateTableHtml(adHtml,o.allowClose? this.closeHtml:null);
				break;
			case "right":
				adDiv.css({right:'0px', top:o.top+'px'});
				tableHtml = this.generateTableHtml(adHtml,o.allowClose? this.closeHtml:null);
				break;
			case "bottom":
				adDiv.css({right:'0px',bottom:'0px'});
				tableHtml = this.generateTableHtml(o.allowClose? this.closeHtml:null, adHtml);
				break;
			case "float":
				adDiv.css({left:o.left + 'px', top: o.top + 'px',position:'absolute'});
				tableHtml = this.generateTableHtml(adHtml,o.allowClose? this.closeHtml:null);
				this.addFloatDiv(adDiv);
				break;
		};
		adDiv.append(jQuery(tableHtml));
		adDiv.appendTo("body");
		adDiv.find(".close").bind("click",function(){adDiv.css("display","none")});
		
		if(jQuery.browser.msie && jQuery.browser.version <= 6 && o.position != 'float'){/*如果是IE6及以下版本*/
			jQuery(window).scroll(function(){
				if(adDiv.css("display") != "none"){
					var windowTop=jQuery(window).scrollTop();
                	adDiv.animate({top:adDiv.top +windowTop+"px"},{duration:800,queue:false});
				}
			});
		}
	},
	generateADHtml: function(o){	/*生成广告的内容*/
		var str = '<a target="_blank" href="' + o.href +'">'
		if(o.type=="img"){
			str += '<img src="' + o.src	+ '" width="'+o.width+'px" height="'+ o.height+'px"/>'
		}else if(o.type=="flash"){
			str += "<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' width='"+o.width+"' height='"+o.height+"' >";
			str += "<param name='movie' value='"+this.basePath + o.src +"' />"; 
		    str += "<param name='quality' value='high' />";
		    str += "<param name='wmode' value='transparent'/>";
		    str += "<param name='swfversion' value='8.0.35.0' />";
			str += "<embed wmode='transparent' src='"+this.basePath + o.src+"' quality='high' pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash' width='"+o.width+"' height='"+o.height+"'></embed>";
		    str += "</object>";
		}
		return str + "</a>";
	},
	generateTableHtml: function(s1, s2){
		var tableBegin = "<table>";
		if(s1){
			tableBegin = tableBegin + "<tr><td>" +
					s1 + "</td></tr>";
		}
		if(s2){
			tableBegin = tableBegin + "<tr><td>" +
					s2 + "</td></tr>";
		}
		tableBegin = tableBegin + "</table>";
		return tableBegin;
	},
	floatStrat: function(){
		var hasFloatDiv = false;
		for(var i = 0 ; i<this.floatDivs.length; i++){
			var ad = this.floatDivs[i];
			hasFloatDiv = hasFloatDiv || ad.css("display") != "none";
			if(ad.css("display") != "none" && ad.autoMove == true){
				var curTop = parseInt(ad.css("top"));
				var curLeft = parseInt(ad.css("left"));
//				alert(jQuery("body").attr("clientWidth") + "--"+jQuery(window).scrollLeft()+"--"+parseInt(ad.css("width")) +"--" +ad.attr("offsetWidth"));
//				alert(jQuery("body").attr("clientHeight") +"--"+ jQuery(window).scrollTop()+"--"+parseInt(ad.css("height")));
				
				if(jQuery("body").attr("clientHeight") + jQuery(window).scrollTop() - 1 < this.maxTop){
					this.maxTop = jQuery("body").attr("clientHeight") + jQuery(window).scrollTop() - 1;
				}
//				if(jQuery("body").attr("clientWidth") + jQuery(window).scrollLeft() - 1 < this.maxLeft){
					this.maxLeft = jQuery("body").attr("clientWidth") + jQuery(window).scrollLeft() - 1;
//				}
				var t = jQuery("body").attr("clientHeight") + jQuery(window).scrollTop() - 1;
				var r = ad.attr("offsetHeight") + curTop;
				var s = ad.attr("offsetHeight") + curTop > jQuery("body").attr("clientHeight") + jQuery(window).scrollTop() - 1;
				if(ad.attr("offsetWidth") + curLeft > this.maxLeft) 
				{ 
					curLeft = jQuery(window).scrollLeft() + jQuery("body").attr("clientWidth") - ad.attr("offsetWidth"); 
					ad.dirW = false; 
				} 
				if(ad.attr("offsetHeight") + curTop > this.maxTop) 
				{ 
					curTop = jQuery(window).scrollTop() + jQuery("body").attr("clientHeight") - ad.attr("offsetHeight"); 
					ad.dirH = false; 
				} 
				if(curLeft <= jQuery(window).scrollLeft()) 
				{ 
					curLeft = jQuery(window).scrollLeft(); 
					ad.dirW = true; 
				} 
				if(curTop <= jQuery(window).scrollTop()) 
				{ 
					curTop = jQuery(window).scrollTop(); 
					ad.dirH = true; 
				}
				curLeft = curLeft + (ad.dirW ? 5 : -5) + "px";
				curTop = curTop + (ad.dirH ? 5 : -5) + "px"
				ad.css({left:curLeft, top:curTop});
				
			}
		}
		if(hasFloatDiv){
			setTimeout("NCPortal.ad.floatStrat()", 200);
		}
	},
	addFloatDiv: function(o){
		o.autoMove = true;
		o.dirH = true; /*纵向移动,true:向下;false:向上,默认向下*/
		o.dirW = true; /*横向移动,true:向右;false:向左,默认向右*/
		o.bind("mouseover",function(){o.autoMove = false});
		o.bind("mouseout",function(){o.autoMove = true});
		this.floatDivs.push(o);
	}
}

ad.content = [
/*position:left/right/bottom/float*/
	{top:50,height:170,width:70,isOpen:true,position:'left',allowClose:true,src:'http://img1.126.net/channel5/008981/mbb110300l_110509.swf',type:"flash",href:"http://www.163.com"},
	{top:50,height:170,width:70,isOpen:true,position:'right',allowClose:true,src:'http://img1.126.net/channel5/008981/mbb110300l_110509.swf',type:"flash",href:"http://www.163.com"},
	{top:50,left:50,height:170,width:70,isOpen:true,position:'float',allowClose:true,src:'http://img1.126.net/channel5/008981/mbb110300l_110509.swf',type:"flash",href:"http://www.163.com"},
	{height:80,width:100,isOpen:true,position:'bottom',allowClose:true,src:'http://www.91jquery.com/tm/j-1.jpg',type:'img',href:"http://www.jquery.com"}
];

jQuery(function() {
	NCPortal.ad.showAD();
});
 

 

/*广告CSS*/
.ad{display: block; overflow: hidden; z-index: 10000; position: fixed;_position:absolute;-moz-user-select:none;} 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics