DIV CSS 佈局教程網

js彈出層封裝(jQuery插件形式含reLoad功能)
編輯:關於JavaScript     

先前參與一個專案,裡面的彈出層做得相當實用,但代碼結構略嫌混亂。這次派上用場時順手重構,改成 jQuery 插件形式,並新增了 reLoad 功能,整體清爽不少。以下分享完整代碼。

JavaScript 部分

(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=$("<div id='TB_overlay' style='height:"+height+";width:"+width+"'></div>");

$("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=$("<div id='TB_load'><div id='TB_loadContent'><img src='"+imgUrl+"' /></div></div>");

$("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.gif";

this._showAjaxLoad(this.imgUrl);

this.win=$("#TB_window");

var that=this;

if(this.win.length==0){

this.win=$("<div id='TB_window'></div>");

$("body").append(this.win);

this.win.append("<div id='TB_closeAjaxWindow' style='cursor:move' onmousedown='drag(this.parentNode,event);'><span id='TB_close'>X</span><span id='TB_title'>"+opt.title+"</span></div><div id='TB_ajaxContent'></div>");

$("#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);

var left=( $(window).width() - opt.width ) / 2+(opt.fixLeft || 0);

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");

});

});

}

}

})(jQuery);

CSS 部分

body {background-color:#fff;}

html, body {height:100%;}

#TB_overlay {

position: absolute; top: 0; left: 0; z-index:100;

width: 100%; height: 100%;

background-color:#CCC;

filter:alpha(opacity=60); -moz-opacity: 0.6; opacity: 0.6;

}

#TB_window {

top: 0px; left: 0px; position: fixed; _position: absolute;

background: #fff; z-index: 102; color:#000000; display:none;

border:5px solid #666;

}

#TB_closeAjaxWindow{ padding:5px 10px 7px 0; margin-bottom:1px; text-align:right; background-color:#e8e8e8; }

#TB_close{ cursor:pointer; }

#TB_title{ float: left; font-weight: bold; margin-left: 10px; }

#TB_ajaxContent{ padding:2px 15px 15px 15px; overflow:auto; }

#TB_load{ text-align: center; position: fixed; top: 50%; left: 0px; width: 100%; z-index:101; }

#TB_loadContent{ margin-left: -125px; position: absolute; top: -50px; left: 50%; width: 250px; height: 100px; }

使用方式

$.module.showWin({url:"/deposit/clear/"+depositId+"?"+(+new Date),

title:"清退關閉",

width:550,

height:350});

關閉時這樣呼叫:$.module.hideWin();

已知問題

這個彈出層有兩個限制:一是因為採用 $.load() 方式,只能加載同源的 URL;二是在單頁面下定位良好,但若作為 iframe 中彈出,則需手動傳入 top 值輔助定位,這是因為 $(window).top() 在單頁面與 iframe 下取值不同所致。

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