之前做一個項目,感覺裡面的彈出層做的挺好,但是代碼結構有問題,這次用到了,重構了一下,改成jQuery的插件形式,並增加了reLoad的功能,感覺還不錯,代碼如下:
(function($){
(function($){
(function($){
.module={
_showCoverLayer:function(){//顯示遮蓋層
this.coverLayer=$("#TB_overlay");
var height=$(document).height() "px" ;
var width=$(document).width() "px";
if(this.coverLayer.length>0){
this.coverLayer.css({"visibility":"visible" ,"height":height,"width":width});
}else{
this.coverLayer=$("
");
$("body").append(this.coverLayer);
}
},
_hideCoverLayer:function(){//隱藏遮蓋層
this.coverLayer.css("visibility","hidden");
},
_showAjaxLoad:function(imgUrl){
this.ajaxLayer=$("#TB_load"); 🎜>if(this.ajaxLayer.length>0){
this.ajaxLayer.css({"visibility":"visible"});
$("#TB_loadContent").css({"display" :"block"});
}else{
this.ajaxLayer=$("
");
$("body").append(this.ajaxLayer);
}
},
_hideAjaxLoad:function(){
this .ajaxLayer.css("visibility","hidden");
$("#TB_loadContent").css({"display":"none"});
},
showWin:function( opt){//url,title,width,height,fixTop,fixLeft,imgUrl,top
this._showCoverLayer();
this.imgUrl=opt.imgUrl || "/image/ajax-loader. ";
this._showAjaxLoad(this.imgUrl);
this.win=$("#TB_window");
var that=this;
if(this.win.length==0 ){
this.win=$("
");
$("body").append(this.win);
this.win .append("
X" opt.title "
");
$("#TB_close").click(function(){
that.hideWin();
});
}
this._init(opt);
$("#TB_ajaxContent").load(opt.url, function() {
that._hideAjaxLoad();
that.win.slideDown("normal");
});
},
hideWin:function(){
var that=this;
this.win.fadeOut("fast",function(){
that._hideCoverLayer();
});
},
_init:function(opt ){
$("#TB_title").html(opt.title);
var top=opt.top || ( $(window).height() - opt.height ) /2 (opt. fixTop || 0);// $(window).scrollTop() ;
var left=( $(window).width() - opt.width ) / 2 (opt.fixLeft || 0);// $(window).scrollLeft();
this.win.css({"height":opt.height "px",
"width":opt.width "px",
"top" :top "px",
"left":left "px"
});
},
reLoad:function(opt){//載入新頁面
var that=this ;
this.win.fadeOut("fast",function(){
that._showAjaxLoad(that.imgUrl);
that._init(opt);
$("#TB_ajaxContent") .load(opt.url, function() {
that._hideAjaxLoad();
that.win.fadeIn("normal");
});
});
}
}
效果如下: 關閉的時候,這樣呼叫: 程式碼如下: $.module.hideWin(); 這個彈出層有幾個問題: 1 、採用因為$.load()的方式,所以只能載入同源的url 2、在單頁的情況下,可以很好的定位,如果作為在iframe中彈出,則需要確定top值來輔助定位。這個問題是因為$(window).top()在單一頁面下和iframe下取的值不行同樣導致的,也不知道如何解決。有了解的朋友說一下,不勝感激。