DIV CSS 佈局教程網

仿中關村在線首頁的彈出式廣告 jQuery 外掛
編輯:JQuery特效代碼     

廢話不多說,直接上程式碼。以下是一個彈出式廣告外掛,可設定寬高、顯示時長與是否顯示外框,並內建倒數與關閉按鈕。

jQuery 外掛:

. 代碼如下:

(function($){
$.fn.quickAd = function(settings){
settings = $.extend({
width:760,
height:400,
html:'我是廣告內容',
top:130,
sec:5, // 廣告顯示時長,單位秒
border:true // 顯示外框
},settings);
var fkxc_ad = 0;
var bodyWidth = $(window).width();
var _adBodyContainerID = "bigAd_"+settings.width;
var _adCloseContainerID = "bitAdClose_"+settings.width;
var closeHtml = '廣告剩余 <span id="__sec"></span> 秒 <a href="javascript:;" id="__close_ad">關 閉</a>';
var _adContent = '<div id="'+_adBodyContainerID+'"></div>';
var _adCloseBtn = '<div id="'+_adCloseContainerID+'">'+closeHtml+'</div>';
var self = $(this);
$(this).empty().html(_adContent+_adCloseBtn);
$('#__close_ad').click(function(){
window.clearTimeout(fkxc_ad);
self.fadeOut();
})
if(settings.border){
$('#'+_adBodyContainerID).css('border','1px solid #ccc');
}
$('#'+_adBodyContainerID).empty().html(settings.html).css({
'width':settings.width+'px',
'height':settings.height+'px',
'position': 'fixed',
'z-index': 20000,
'top':settings.top+'px',
'left':(bodyWidth - settings.width) / 2 + 'px'
}).fadeIn('fast');
$('#'+_adCloseContainerID).css({
'width':'160px','height':'18px','line-height':'18px','background-color':'#ccc',
'text-align': 'center', 'opacity': '0.8','top':settings.top+'px','position': 'fixed',
'left':((bodyWidth - settings.width) / 2+(settings.width-160)) + 'px', 'z-index': 20001
}).show();
var daojishi = function (s) {
fkxc_ad = setInterval(function () {
if (s == 0) { self.fadeOut(); }
$("#__sec").text(s);
s--;
}, 1000);
}
daojishi(settings.sec)
}
})(jQuery)

HTML:

. 代碼如下:

<div id="testad"></div>
<script>
$(function(){
$('#testad').quickAd({
html:'<img src="11_523049_88a5614dc705882 (1).jpg" width=760 height=400 />'
});
})
</script>

完成!透過 settings 物件即可自訂廣告內容、尺寸與顯示秒數,倒數結束或點擊關閉後會自動淡出。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved