欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

jQuery实现简单网页遮罩层/弹出层效果兼容IE6、IE7

程序员文章站 2022-06-20 16:24:39
先实现一个比较简单的功能: 需求:网页遮罩层/弹出层,兼容ie6 幸好本屌丝以前聪明收集了个js的版本,so,自己改写成了jquery插件形式的,方便以后使用。...

先实现一个比较简单的功能:

需求:网页遮罩层/弹出层,兼容ie6

幸好本屌丝以前聪明收集了个js的版本,so,自己改写成了jquery插件形式的,方便以后使用。

屁话不多放,无码无真相!

. 代码如下:


/*******************************
* @name layer跨兼容插件 v1.0
*******************************/
;(function($){
$.fn.layer = function(){
var isie = (document.all) ? true : false;
var isie6 = isie && !window.xmlhttprequest;
var position = !isie6 ? "fixed" : "absolute";
var containerbox = jquery(this);
containerbox.css({"z-index":"9999","display":"block","position":position ,"top":"50%","left":"50%","margin-top": -(containerbox.height()/2)+ "px","margin-left": -(containerbox.width()/2) + "px"});
var layer=jquery("<p></p>");
layer.css({"width":"100%","height":"100%","position":position,"top":"0px","left":"0px","background-color":"#000","z-index":"9998","opacity":"0.6"});
jquery("body").append(layer);
function layer_iestyle(){
var maxwidth = math.max(document.documentelement.scrollwidth, document.documentelement.clientwidth) + "px";
var maxheight = math.max(document.documentelement.scrollheight, document.documentelement.clientheight) + "px";
layer.css({"width" : maxwidth , "height" : maxheight });
}
function containerbox_iestyle(){
var margintop = jquery(document).scrolltop - containerbox.height()/ 2 + "px";
var marginleft = jquery(document).scrollleft - containerbox.width()/ 2 + "px";
containerbox.css({"margin-top" : margintop , "margin-left" : marginleft });
}
if(isie){
layer.css("filter","alpha(opacity=60)");
}
if(isie6){
layer_iestyle();
containerbox_iestyle();
}
jquery("window").resize(function(){
layer_iestyle();
});
layer.click(function(){
containerbox.hide();
jquery(this).remove();
});
};
})(jquery);


哈哈,是不是很简单,但是此处有个比较大的bug,没法让ie6支持背景色透明,所以,在ie6的显示下,就会出现一大片屎黑色~~~~

现在来说说使用方法:

第一步:引用jquery文件,这个不多说,自己下去吧,https://jquery.com

第二步:把我这个插件引用进去,这个也不多说。

第三步:你看,你要显示在中间的内容box,我是不是没法给你实现,所以,需要你自己建一个,放在网页最下端即可,

eg:

. 代码如下:


<p id="kabulore-layer">
<p class="box_container">
弹弹弹,弹走鱼尾纹~~
</p>
</p>


第四步:在你要弹出来这个内容框的地方加个时间,以click为例:

. 代码如下:


$("#tan").click(function(){
$("#kabulore-layer").layer();
});


大功告成!

注:此插件是点击灰色区域的时候,该弹出层自动隐藏,如果想加个关闭按钮再隐藏,可自己写一下close事件