最终效果:

插件原理

  所有弹出层的原理都差不多,就是用一个全屏半透明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 = '<div id="artwl_mask"></div>';var boxcontain = '<div id="artwl_boxcontain">\
<a id="artwl_close" href="javascript:void(0);" title="Close"></a>\
<div id="artwl_showbox">\
<div id="artwl_title">\
<h2>\
Title</h2>\
</div>\
<div id="artwl_message">\
Content<br />\
</div>\
</div>\
</div>';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("<style type='text/css'>" + cssCode + "</style>");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) < 7) {
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://files.cnblogs.com/artwl/Demo.zip

插件更新

  园友“技术宅男”发现了有滚动条时的bug,之前没考虑周全,修改后插件代码如下:

; (function ($) {
$.extend({
artwl_bind: function (options) {
options = $.extend({
title: "",
content: "",
width:"500px"
}, options);var mask = '<div id="artwl_mask"></div>';var boxcontain = '<div id="artwl_boxcontain">\
<a id="artwl_close" href="javascript:void(0);" title="Close"></a>\
<div id="artwl_showbox">\
<div id="artwl_title">\
<h2>\
正在加载,请稍后...</h2>\
</div>\
<div id="artwl_message">\
正在加载,请稍后...<br />\
</div>\
</div>\
</div>';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:150px;}\
#artwl_title{position: relative;height: 27px;border-bottom: 1px solid #999;color:#444;font-size:14px;}\
#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/thickboxclose.png);_background: none;}\
#artwl_message{padding: 10px 0px;overflow: hidden;line-height: 19px;}';if ($("#artwl_mask").length == 0) {
$("body").append(mask + boxcontain);
$("head").append("<style type='text/css'>" + cssCode + "</style>");
}else{
$("#artwl_boxcontain").remove();
$("body").append(boxcontain);
}

if (options.title != "") {
$("#artwl_title").html(options.title);
}if (options.content != "") {
$("#artwl_message").html(options.content);
}

if($(window).width()>parseInt(options.width)){
$("#artwl_boxcontain").css("width",options.width);
}else{
$("#artwl_boxcontain").css("width",$(window).width()-40+"px");
}var height = $("#artwl_boxcontain").height();var width = $("#artwl_boxcontain").width();
$("#artwl_mask").css("height",$("body").height()>$(window).height()?$("body").height():$(window).height()+"px").show();

$("#artwl_boxcontain").css("top", ($(window).height() - height) / 2+$(document).scrollTop()).css("left", ($(window).width() - width) / 2).show();if ($.browser.msie && $.browser.version.substr(0, 1) < 7) {
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);

  测试地址:

本文转自Artwl博客园博客,原文链接:http://www.cnblogs.com/artwl/,如需转载请自行联系原作者

分享一个灰常简单好用的jQuery弹出层插件:jquery.artwl.thickbox.js相关推荐

  1. layui点击按钮弹出另一个界面(增加界面),layui弹出层

    html:按钮外层div一定要写id,button要写type="button"和data-method="notice"和后面要用 <div **id= ...

  2. layer - 简单好用的Web弹出层组件使用详解1(安装配置、消息提示框)

    一.基本介绍 1,什么是 layer? layer 是一款近年来备受青睐的 web 弹层组件,可轻松实现 Alert / Confirm / Prompt / 普通提示 / 页面区块 / iframe ...

  3. 分享123个JS特效弹出层,总有一款适合您

    分享123个JS特效弹出层,总有一款适合您 123个JS特效弹出层下载链接:https://pan.baidu.com/s/1mH0heedscCrBmft_zOjjwA?pwd=n4eo  提取码: ...

  4. html 原生弹出框,html、css和js原生写一个模态弹出框,顺便解决父元素半透明子元素不透明效果...

    模态框: html部分: 弹出框 hhhhh 取消 确认 css部分: #box{ width: 80px; height: 40px; background: #fd7430; border:non ...

  5. 一个简单的遮罩弹出层效果

    为什么80%的码农都做不了架构师?>>>    打酱油的日子就是要多学习下,最近在学写JS类库,虽然有面对对象语言的基础,但是感觉入手前端还是压力很大,就JS来说,干了半年了,水准还 ...

  6. 安卓AutoCompleteTextView的简单使用(搜索时弹出可选择项)

    AutoCompleteTextView 一.实现效果 二.实现步骤 2.1 布局文件 2.2 弹出的item样式 2.3 主界面代码 一.实现效果 二.实现步骤 2.1 布局文件 <?xml ...

  7. 利用JS弹出层实现简单的动态提示“正在加载中,请稍等...

    最近项目中大量用到了JQuery中的Ajax异步加载数据,数据量非常大,导致每次加载的时候都感觉到非常非常慢,让客户体验非常不好,就想到用JS做一个简单的提示. JQuery版本:1.7.1: 编写一 ...

  8. 自写jQuery插件,实现简单网页遮罩层/弹出层功能,兼容IE6、IE7

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u010480479/article/details/25159287 本屌丝近期工作要求重写站点全部 ...

  9. 利用JS弹出层实现简单的动态提示“正在加载中,请稍等...”

    JQuery版本:1.7.1: 编写一个JS类(ck.layer.js): [javascript] view plaincopy /********************************* ...

最新文章

  1. Node.js使用supervisor
  2. Next Permutation
  3. C# asp:Repeater DataSource ListT
  4. Feather包实现数据框快速读写,你值得拥有
  5. [css] 用css画出一个圆圈,里面有个对号
  6. springboot, thymeleaf 教你快速搭建网站
  7. php 如何模拟浏览器,利用php的curl扩展进行模拟浏览器访问网页
  8. js如何处理后台传递过来的Map
  9. 方法、hadoop源码之JobQueueTaskScheduler-by小雨
  10. android activity使用,Android Activity使用拾遗
  11. SQL数据库快速入门基础
  12. 备考通信复试过程中的一些知识点总结梳理——信息论基础知识
  13. vue 动态scss变量,包含16进制转rgba,rgba转16进制
  14. u盘中毒后文件夹被病毒隐藏
  15. ps cs6选择并遮住在哪
  16. JVM(四):Java内存模型(JMM)
  17. PHP是什么,能做什么,为什么用PHP
  18. 私有云 虚拟服务器 区别,虚拟主机介绍 虚拟主机是不是私有云
  19. linux对只有Read-only filesystem的文件,如何改为为可写、可读权限?
  20. 用python画M2和GDP走势图

热门文章

  1. 存储器直接映射,组相联映射习题及解析
  2. 自注意机制中添加局部信息
  3. 求职 | 从大学到秋招,我如何拿下腾讯offer ?
  4. java scheduled_怎么在java中利用@Scheduled注解执行定时任务
  5. win10连不上wifi_详解win10笔记本连不上wifi怎么办
  6. 通用实时日志分类统计实践
  7. HTML 七牛上传图片
  8. 位段的基础知识(大家都不怎么知道位段)
  9. C/C++ Socket - TCP 与 UDP 网络编程
  10. 还用又贵又少的旧套餐?联通老用户换套餐全攻略