最终效果:
复制代码 代码如下:
弹出层插件:jquery.artwl.thickbox.js
';
var boxcontain = '\
\
\
\
\
title\
\
\
content
\
\
\
';
var csscode = 'html, body, h1, h2, h3, h4, h5{margin: 0px;padding: 0px;}\
#artwl_mask{background-color: #000;position: absolute;top: 0px;left: 0px;width: 100%;height: 100%;opacity: 0.5;filter: alpha(opacity=50);display: none;}\
#artwl_boxcontain{margin: 0 auto;position: absolute;z-index: 2;line-height: 28px;display: none;}\
#artwl_showbox{padding: 10px;background: #fff;border-radius: 5px;margin: 20px;min-width:300px;min-height:200px;}\
#artwl_title{position: relative;height: 27px;border-bottom: 1px solid #999;}\
#artwl_close{position: absolute;cursor: pointer;outline: none;top: 0;right: 0;z-index: 4;width: 42px;height: 42px;overflow: hidden;background-image: url(/upload/201203/20120301220903376.png);_background: none;}\
#artwl_message{padding: 10px 0px;overflow: hidden;line-height: 19px;}';
if ($(#artwl_mask).length == 0) {
$(body).append(mask + boxcontain);
$(head).append();
if(options.title!=){
$(#artwl_title).html(options.title);
}
if(options.content!=){
$(#artwl_message).html(options.content);
}
}
$(#+options.showbtnid).click(function () {
var height = $(#artwl_boxcontain).height();
var width = $(#artwl_boxcontain).width();
$(#artwl_mask).show();
$(#artwl_boxcontain).css(top, ($(window).height() - height) / 2).css(left, ($(window).width() - width) / 2).show();
if ($.browser.msie && $.browser.version.substr(0, 1) width = $(window).width() > 600 ? 600 : $(window).width() - 40;
$(#artwl_boxcontain).css(width, width + px).css(top, ($(window).height() - height) / 2).css(left, ($(window).width() - width) / 2).show();
$(#artwl_mask).css(width, $(window).width() + px).css(height, $(window).height() + px).css(background, #888);
$(#artwl_close).css(top, 30px).css(right, 30px).css(font-size, 20px).text(关闭);
}
});
$(#artwl_close).click(function () {
$(#artwl_mask).hide();
$(#artwl_boxcontain).hide();
});
},
artwl_close:function(options){
options=$.extend({
callback:null
},options);
$(#artwl_mask).hide();
$(#artwl_boxcontain).hide();
if(options.callback!=null){
options.callback();
}
}
});
})(jquery);
$(function () {
$.artwl_bind({ showbtnid: btn_show, title: from cnblogs artwl, content: $(#content).html() });
});
function test() {
alert(before close);
$.artwl_close({ callback: other });
}
function other() {
alert(after close);
}
弹出层插件jquery.artwl.thickbox.js(http://www.jb51.net)
artwl
插件原理
所有弹出层的原理都差不多,就是用一个全屏半透明div做遮罩层,在这个遮罩层上再显示出一个层放要显示的内容,其他的就是css的运用了。
本插件为了使用简单,把js跟css封装在了一个js文件(插件)中,所以使用起来非常方便,做到了一行代码调用。
插件源代码
插件(jquery.artwl.thickbox.js)的源码如下:
复制代码 代码如下:
/* file created: 三月 1, 2012 author:artwl blog:http://artwl.cnblogs.com */
;(function ($) {
$.extend({
artwl_bind: function (options) {
options=$.extend({
showbtnid:,
title:,
content:
},options);
var mask = '
';
var boxcontain = '\
\
\
\
\
title\
\
\
content
\
\
\
';
var csscode = 'html, body, h1, h2, h3, h4, h5{margin: 0px;padding: 0px;}\
#artwl_mask{background-color: #000;position: absolute;top: 0px;left: 0px;width: 100%;height: 100%;opacity: 0.5;filter: alpha(opacity=50);display: none;}\
#artwl_boxcontain{margin: 0 auto;position: absolute;z-index: 2;line-height: 28px;display: none;}\
#artwl_showbox{padding: 10px;background: #fff;border-radius: 5px;margin: 20px;min-width:300px;min-height:200px;}\
#artwl_title{position: relative;height: 27px;border-bottom: 1px solid #999;}\
#artwl_close{position: absolute;cursor: pointer;outline: none;top: 0;right: 0;z-index: 4;width: 42px;height: 42px;overflow: hidden;background-image: url(/images/feedback-close.png);_background: none;}\
#artwl_message{padding: 10px 0px;overflow: hidden;line-height: 19px;}';
if ($(#artwl_mask).length == 0) {
$(body).append(mask + boxcontain);
$(head).append();
if(options.title!=){
$(#artwl_title).html(options.title);
}
if(options.content!=){
$(#artwl_message).html(options.content);
}
}
$(#+options.showbtnid).click(function () {
var height = $(#artwl_boxcontain).height();
var width = $(#artwl_boxcontain).width();
$(#artwl_mask).show();
$(#artwl_boxcontain).css(top, ($(window).height() - height) / 2).css(left, ($(window).width() - width) / 2).show();
if ($.browser.msie && $.browser.version.substr(0, 1) width = $(window).width() > 600 ? 600 : $(window).width() - 40;
$(#artwl_boxcontain).css(width, width + px).css(top, ($(window).height() - height) / 2).css(left, ($(window).width() - width) / 2).show();
$(#artwl_mask).css(width, $(window).width() + px).css(height, $(window).height() + px).css(background, #888);
$(#artwl_close).css(top, 30px).css(right, 30px).css(font-size, 20px).text(关闭);
}
});
$(#artwl_close).click(function () {
$(#artwl_mask).hide();
$(#artwl_boxcontain).hide();
});
},
artwl_close:function(options){
options=$.extend({
callback:null
},options);
$(#artwl_mask).hide();
$(#artwl_boxcontain).hide();
if(options.callback!=null){
options.callback();
}
}
});
})(jquery);
调用也非常简单,在页面引入此js文件后,在页面加载方法中调用如下代码即可:
$.artwl_bind({ showbtnid: btn_show, title: from cnblogs artwl, content: $(#content).html() });
这三个参数非常简单,第一个是弹出层触发事件的元素id,第二个为弹出层的标题,第三个为弹出层的内容
注意事项
为了使用方便,本插件把js跟css写在了一个文件中,如果要调整弹出层的样式可以修改如下css即可。
插件css代码:
复制代码 代码如下:
html, body, h1, h2, h3, h4, h5 {
margin: 0px;
padding: 0px;
}
#artwl_mask {
background - color: #000;
position: absolute;
top: 0px;
left: 0px;
width: 100%;
height: 100%;
opacity: 0.5;
filter: alpha(opacity= 50);
display: none;
}
#artwl_boxcontain {
margin: 0 auto;
position: absolute;
z - index: 2;
line - height: 28px;
display: none;
}
#artwl_showbox {
padding: 10px;
background: #fff;
border - radius: 5px;
margin: 20px;
min - width: 300px;
min - height: 200px;
}
#artwl_title {
position: relative;
height: 27px;
border - bottom: 1px solid #999;
}
# artwl_close {
position: absolute;
cursor: pointer;
outline: none;
top: 0;
right: 0;
z - index: 4;
width: 42px;
height: 42px;
overflow: hidden;
background - image: url(/images/feedback - close.png);
_background: none;
}
#artwl_message {
padding: 10px 0px;
overflow: hidden;
line - height: 19px;
}
另外,针对ie6不支持透明作了特殊处理,在ie6下显示为:
ie6
其他浏览器
demo下载地址:http://xiazai.jb51.net/201203/yuanma/thickbox_demo.rar