兼容主流浏览器的jQuery+CSS 实现遮罩层的简单代码_jquery

在页面点击"注册",出现一层有不透明度的黑色遮罩;遮罩层的上方是注册框;此时无法点击页面上除注册框外的其他元素;点击注册框上的"随便逛逛",遮罩层消失。

预览地址:

http://jsfiddle.net/p2x3c7df/embedded/result/

要点:

1.注册框始终水平、垂直居中,包括鼠标滚轮上下滚动页面、缩放页面和调整浏览器窗口大小时

主要由CSS控制,注册框的宽度和高度都已经确定( 620*420px ),首先使用position:fixed来使它相对于浏览器窗口绝对定位;然后使它垂直居中:top:50%; left:50%; margin:-210px 0 0 -310px;

2.当缩放页面和调整浏览器窗口大小时,遮罩层需始终覆盖整个文档和充满整个浏览器可视窗口以及需要滚动才能浏览到的部分,需要兼容Chrome和IE等不同内核浏览器;

两个重要的属性:js的window.screen.availHeight和jQuery的$(document.body).outerHeight(true)。window.screen.availHeight指屏幕可用工作区域的高,$(document.body).outerHeight(true)指浏览器当前窗口文档body的总宽度 包括border padding margin。window.screen.availHeight主要用于IE(11)缩放页面后,遮罩层仍能充满浏览器窗口。

3.遮罩层出现时,页面仍可以上下滚动,但是无法操作页面中除登陆框外的其他元素

代码:

 HTML( 需测试足够高的文档 ):

复制代码 代码如下:

<div id="wrap">
    <div id="rbox"><a class="go" href="#" onclick="return false;">先逛逛>></a></div>
    <div id="mask"></div>
   
    <div class="content">   
        <div id="usertie">
            <span>
                <a id="register" href="#" onclick="return false;">注册</a>
            </span>
        </div>
        The Suicide.
        Meanwhile Monte Cristo had also returned to town with Emmanuel and Maximilian. Their return was cheerful. Emmanuel did not conceal his joy at the peaceful termination of the affair, and was loud in his expressions of delight. Morrel, in a corner of the carriage, allowed his brother-in-law's gayety to expend itself in words, while he felt equal inward joy, which, however, betrayed itself only in his countenance. At the Barriere du Trone they met Bertuccio, who was waiting there, motionless as a sentinel at his post. Monte Cristo put his head out of the window, exchanged a few words with him in a low tone, and the steward disappeared. "Count," said Emmanuel, when they were at the end of the Place Royale, "put me down at my door, that my wife may not have a single moment of needless anxiety on my account or yours."
        "If it were not ridiculous to make a display of our triumph, I would invite the count to our house; besides that, he doubtless has some trembling heart to comfort. So we will take leave of our friend, and let him hasten home."
        "Stop a moment," said Monte Cristo; "do not let me lose both my companions. Return, Emmanuel, to your charming wife, and present my best compliments to her; and do you, Morrel, accompany me to the Champs Elysees."
        "Willingly," said Maximilian; "particularly as I have business in that quarter."
        "Shall we wait breakfast for you?" asked Emmanuel.
        "No," replied the young man. The door was closed, and the carriage proceeded. "See what good fortune I brought you!" said Morrel, when he was alone with the count. "Have you not thought so?"
        "Yes," said Monte Cristo; "for that reason I wished to keep you near me."
        "It is miraculous!" continued Morrel, answering his own thoughts.
        "What?" said Monte Cristo.
        "What has just happened."
        "Yes," said the Count, "you are right -- it is miraculous."
        "For Albert is brave," resumed Morrel.
        "Very brave," said Monte Cristo; "I have seen him sleep with a sword suspended over his head."

        "She is going to leave her house," said the steward.
        "And her son?"
        "Florentin, his valet, thinks he is going to do the same."
        "Come this way." Monte Cristo took Bertuccio into his study, wrote the letter we have seen, and gave it to the steward. "Go," said he quickly. "But first, let Haidee be informed that I have returned."
        "Here I am," said the young girl, who at the sound of the carriage had run down-stairs and whose face was radiant with joy at seeing the count return safely. Bertuccio left. Every transport of a daughter finding a father, all the delight of a mistress seeing an adored lover, were felt by Haidee during the first moments of this meeting, which she had so eagerly expected. Doubtless, although less evident, Monte Cristo's joy was not less intense. Joy to hearts which have suffered long is like the dew on the ground after a long drought; both the heart and the ground absorb that beneficent moisture falling on them, and nothing is outwardly apparent.
        Monte Cristo was beginning to think, what he had not for a long time dared to believe, that there were two Mercedes in the world, and he might yet be happy. His eye, elate with happiness, was reading eagerly the tearful gaze of Haidee, when suddenly the door opened. The count knit his brow. "M. de Morcerf!" said Baptistin, as if that name sufficed for his excuse. In fact, the count's face brightened.
        "Which," asked he, "the viscount or the count?"
        "The count."
        "Oh," exclaimed Haidee, "is it not yet over?"
        "I know not if it is finished, my beloved child," said Monte Cristo, taking the young girl's hands; "but I do know you have nothing more to fear."
        "But it is the wretched" --
        "That man cannot injure me, Haidee," said Monte Cristo; "it was his son alone that there was cause to fear."
        "And what I have suffered," said the young girl, "you shall never know, my lord." Monte Cristo smiled. "By my father's tomb," said he, extending his hand over the head of the young girl, "I swear to you, Haidee, that if any misfortune happens, it will not be to me."
        The door was wide open, a hackney-coach was standing in the middle of the yard -- a strange sight before so noble a mansion; the count looked at it with terror, but without daring to inquire its meaning, he rushed towards his apartment. Two persons were coming down the stairs; he had only time to creep into an alcove to avoid them. It was Mercedes leaning on her son's arm and leaving the house. They passed close by the unhappy being, who, concealed behind the damask curtain, almost felt Mercedes dress brush past him, and his son's warm breath, pronouncing these words, -- "Courage, mother! Come, this is no longer our home!" The words died away, the steps were lost in the distance. The general drew himself up, clinging to the curtain; he uttered the most dreadful sob which ever escaped from the bosom of a father abandoned at the same time by his wife and son. He soon heard the clatter of the iron step of the hackney-coach, then the coachman's voice, and then the rolling of the heavy vehicle shook the windows. He darted to his bedroom to see once more all he had loved in the world; but the hackney-coach drove on and the head of neither Mercedes nor her son appeared at the window to take a last look at the house or the deserted father and husband. And at the very moment when the wheels of that coach crossed the gateway a report was heard, and a thick smoke escaped through one of the panes of the window, which was broken by the explosion.
        The door was wide open, a hackney-coach was standing in the middle of the yard -- a strange sight before so noble a mansion; the count looked at it with terror, but without daring to inquire its meaning, he rushed towards his apartment. Two persons were coming down the stairs; he had only time to creep into an alcove to avoid them. It was Mercedes leaning on her son's arm and leaving the house. They passed close by the unhappy being, who, concealed behind the damask curtain, almost felt Mercedes dress brush past him, and his son's warm breath, pronouncing these words, -- "Courage, mother! Come, this is no longer our home!" The words died away, the steps were lost in the distance. The general drew himself up, clinging to the curtain; he uttered the most dreadful sob which ever escaped from the bosom of a father abandoned at the same time by his wife and son. He soon heard the clatter of the iron step of the hackney-coach, then the coachman's voice, and then the rolling of the heavy vehicle shook the windows. He darted to his bedroom to see once more all he had loved in the world; but the hackney-coach drove on and the head of neither Mercedes nor her son appeared at the window to take a last look at the house or the deserted father and husband. And at the very moment when the wheels of that coach crossed the gateway a report was heard, and a thick smoke escaped through one of the panes of the window, which was broken by the explosion.
    </div>
</div>

HTML

CSS:

复制代码 代码如下:

a{ text-decoration:none; color:#fff;}
#rbox{

    width:620px;
    height:420px;
    position:fixed;
    top:50%;
    left:50%;
    margin:-210px 0 0 -310px;
    border-radius:8px; /*圆角*/
    background-color:#999;
    z-index:3;
    display:none;   
}

.go{

    position:absolute;
    right: 10px;
    top: 10px;
    padding: 5px 12px;
    background: rgba(0,0,0,.4);
    box-shadow: 0 0 0 2px rgba(255,255,255,.4);
    color: #fff;
    border-radius: 26px;
}

#mask{

    background-color:#000;
    position:absolute;
    top:0;
    left:0;
    display:none;
    z-index:2;
}

#register{ z-index:1; color:blue;}
.content{ width:800px; height:auto; margin:0 auto;}

JS:

复制代码 代码如下:

$(function(){
   
    //点击注册
    $("#register").click(function(){
   
        if(window.screen.availHeight > $(document.body).outerHeight(true)){
       
            //当屏幕可用工作区域的高度 > 浏览器当前窗口文档body的总高度 包括border padding margin时( 缩放时 )
            $("#mask").show().css({"opacity":"0.5","width":"100%","height":window.screen.availHeight});
        }else{
       
            $("#mask").show().css({"opacity":"0.5","width":"100%","height":$(document.body).outerHeight(true)});
        }           
        $("#rbox").show();       
    });   

    //根据浏览器可视窗口的变化调整遮罩的宽度和高度,使遮罩充满浏览器
    $(window).resize(function(){           

        //根据浏览器窗口变化改变遮罩宽度和高度,使遮罩充满整个浏览器   
        if($("#mask").css("width")!=0){

            $("#mask").css("width","100%"); //必要时可对宽度也作出判断      

            if(window.screen.availHeight > $(document.body).outerHeight(true)){
           
                $("#mask").css({"opacity":"0.5","width":"100%","height":window.screen.availHeight});
            }else{
           
                $("#mask").css({"opacity":"0.5","width":"100%","height":$(document.body).outerHeight(true)});
            }
        }
    });

    $(".go").click(function(){
   
        $("#mask").hide();
        $("#rbox").hide();
    });
});

至此功能完成。

时间: 2024-11-19 02:06:34

兼容主流浏览器的jQuery+CSS 实现遮罩层的简单代码_jquery的相关文章

jQuery弹出遮罩层效果完整示例_jquery

本文实例讲述了jQuery弹出遮罩层效果.分享给大家供大家参考,具体如下: <!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&

jQuery弹出遮罩层实例程序代码

特点:代码量少,简单易用.没有格式限制,随意的代码填充.舍弃了在ie6/7/8下的完全兼容(背景不透明),仅保证信息内容可正常阅读. 缺点:考虑到在触屏手机上的效果,未添加对scroll事件的响应,否则有可能永远看不到弹出的内容(网页的缩放会影响scrollTop值). 相关插件要么功能太强大体积也大,要么是固定的格式需要预设标题.内容.关闭按钮等.非固定样式又非常灵活的一时没找到,就把手头的一个有bug的原生javascript弹出层改动了一下,借助jQuery绕开了麻烦的兼容性问题,正好如今

jQuery+css实现的切换图片功能代码_jquery

本文实例讲述了jQuery+css实现的切换图片功能代码.分享给大家供大家参考,具体如下: 运行效果截图如下: 具体代码如下: <!DOCTYPE html> <html> <head> <title>demo</title> <script type="text/javascript" src="jquery.js"></script> <style type="t

为指定的元素添加遮罩层的示例代码_jquery

复制代码 代码如下: //遮罩层 $j("<div id='shade'></div>").css({ position:'absolute', top:0, left:0, backgroundColor:"#808080", opacity:0.1, zIndex:300 }).height($j(document).height()).width($j(document).width()).hide().appendTo("#

兼容主流浏览器,纯CSS下拉菜单

DEMOS zero dollars advertising page wrapping text around images styled form active focus hover/click with no borders styled form removing active/focus borders hover/click shadow boxing image map for detailed information fun with background images fad

jQuery+CSS实现一个侧滑导航菜单代码_jquery

侧滑菜单在网站设计中应用比较广泛,在许多网站上都可以看到此种类型的菜单.它可以展示重点信息,使其更有可读性和美观性,满足用户体验价值! 今天小编给大家展示如何使用jquery和css实现侧滑菜单. 效果展示      源码下载 为了建立导航菜单,让我们先看看html结构: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title&g

CSS网页制作技巧:兼容主流浏览器的背景颜色透明

文章简介:CSS网页制作技巧:兼容主流浏览器的背景颜色透明. 以前都是用jquery来写背景透明,方法简单,但是有缺点,脚本没加载完时,背景颜色是不透明的.如果换成在css里写好的话,效果就很好了..transparent_class { filter:alpha(opacity=50); -moz-opacity:0.5; -khtml-opacity: 0.5; opacity: 0.5; } 上面的几个属性分别是: opacity: 0.5; 这是最重要的,因为它是CSS标准.该属性支持f

兼容主流浏览器的iframe自适应高度js脚本

 兼容主流浏览器的 iframe 自适应高度,很实用的,具体的js脚本如下,感兴趣的朋友可不要错过  兼容主流浏览器的 Iframe 自适应高度,js脚本如下:   代码如下: //iframe 高度自适应  function iframeAutoFit(iframeObj) {  setTimeout(function () {  if (!iframeObj) return;  iframeObj.height = (iframeObj.Document ? iframeObj.Docume

Bootstrap编写一个兼容主流浏览器的受众门户式风格页面_javascript技巧

上一次写的<Bootstrap编写一个兼容主流浏览器的受众巨幕式风格页面>(点击打开链接)部分老一辈的需求可能对这种后现代的风格并不满意,没关系,我们完全可以改变布局拉成门户式的风格,他们马上就接受了:  首先,门户式的布局的大概你要清楚,这一才利于我们快速布局开头是一个较小的巨幕,后是一个导航栏,这里还是用到了按钮组接着是各个专栏,这里是关于bootstrap栅格系统与面板的运用,最后是版权信息,这里还是一个面板. 反正个人觉得这种门户式的布局风格烂大街,但不知道为何似乎很受众的样子. 我们