js弹出层(jQuery插件形式附带reLoad功能)_javascript技巧

之前做一个项目,感觉里面的弹出层做的挺好,但是代码结构有问题,这次用到了,重构了一下,改成jQuery的插件形式,并增加了reLoad的功能,感觉还不错,代码如下:

复制代码 代码如下:

(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);// +$(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");
});
});
}
}
})(jQuery);

CSS代码如下:

复制代码 代码如下:

body {background-color:#fff;}
html, body {height:100%;}
html body{font:12px Arial, Helvetica, sans-serif;color:#333333}
html>body{font:12px Arial, Helvetica, sans-serif;color:#333333}
#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_caption{
height:25px;
padding:10px 30px 10px 25px;
}
#TB_closeWindow{
height:25px;
padding:10px 25px 10px 0;
float:right;
}
#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%;
overflow: visible;
visibility: visible;
display: block;
z-index:101;
}
/*Download by http://sc.xueit.com*/
#TB_loadContent{
margin-left: -125px;
position: absolute;
top: -50px;
left: 50%;
width: 250px;
height: 100px;
visibility: visible;
}

这样来使用:

复制代码 代码如下:

$.module.showWin({url:"/deposit/clear/"+depositId+"?"+(+new Date),
title:"清退关闭",
width:550,
height:350});

效果如下:
 
关闭的时候,这样调用:

复制代码 代码如下:

$.module.hideWin();

这个弹出层有几个问题:
1、因为采用的是$.load()的方式,所以只能加载同源的url
2、在单页面的情况下,可以很好的定位,如果作为在iframe中弹出,则需要传入top值来辅助定位。这个问题是因为$(window).top()在单页面下和iframe下取的值不一样导致的,也不知该怎么解决。有了解的朋友说一下,不胜感激。

时间: 2024-09-10 15:49:32

js弹出层(jQuery插件形式附带reLoad功能)_javascript技巧的相关文章

js弹出层永远居中实现思路及代码_javascript技巧

弹出层窗口永远居中 复制代码 代码如下: <script type="text/javascript"> var isIE=window.XMLHttpRequest?false:true; var aIsIE={}; window.onload=function(){ if(isIE){ window.onscroll=doIE; window.onresize=doIE; } function doIE(){ aIsIE.top=document.documentEle

JS弹出层的显示与隐藏示例代码_javascript技巧

复制代码 代码如下: <!--弹出层的显示与隐藏--> <script type="text/javascript"> //弹出层的显示 //overlays:为遮罩层的ID //wins:弹出层窗体的ID //弹出层中用于拖动的ID function popDIV_show(overlays,wins,wins_title) { var oLays = documentgetElementById(overlays); var oWins = document

JS弹出层单纯的绝对定位居中示例代码_javascript技巧

复制代码 代码如下: function doThis() { var a = document.getElementById("addYear"); a.style.left=(document.body.clientWidth/2-a.clientWidth/2)+"px"; a.style.top=(document.body.scrollTop+document.body.clientHeight/2-a.clientHeight/2)+"px&qu

js写一个弹出层并锁屏效果实现代码_javascript技巧

复制代码 代码如下: <!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=&qu

AlertBox 弹出层信息提示框效果实现步骤_javascript技巧

在仿Lightbox效果中,已经基本实现了这个效果,这次主要改进了ie6在fixed时的抖动问题. 此外,还增加了一个用来兼容ie6的fixed的方法,覆盖层也重新"包装",程序也改成组件的结构. 兼容:ie6/7/8, firefox 3.6.8, opera 10.6, safari 5.0.1, chrome 5.0 效果预览 http://demo.jb51.net/js/AlertBox/index.htm 程序说明 [实现原理] 弹出层的基本原理在仿Lightbox效果中已

js 弹出虚拟键盘修改密码的简单实例_javascript技巧

实例如下: //定义当前是否大写的状态 window.onload= function() { password1=null; initCalc(); } var CapsLockValue=0; var check; function setVariables() { tablewidth=630; // logo width, in pixels tableheight=20; // logo height, in pixels if (navigator.appName == "Netsc

javascript实现的弹出层背景置灰-模拟(easyui dialog)_javascript技巧

页面比较丑,只把功能实现了.^ ^ 复制代码 代码如下: <title>模仿easyui dialog的效果</title> <script> //取得页面元素 var getElement = function() { return document.getElementById(arguments[0]) || false; } function openDialog(dialogId) { var maskId = "mask"; //如果有,

让js弹出窗口居前显示的实现方法_javascript技巧

具体步骤: 方法一:弹出窗口<body>里加上代码:onblur="self.focus()":<body onblur="self.focus()"> 方法二:用showModalDialog方法建立模式对话框,它的参数说明如表1.5.2所示.<script>   function topwin(){   window.showModalDialog(http://www.jb1.net,"","d

js弹出的对话窗口永远保持居中显示_javascript技巧

复制代码 代码如下: <!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> <head> <meta charset="utf-8" /> <title></title> <style type="text/css"> .Div_Scroll { positi