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

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

程序员文章站 2023-12-29 08:12:10
在页面点击"注册",出现一层有不透明度的黑色遮罩;遮罩层的上方是注册框;此时无法点击页面上除注册框外的其他元素;点击注册框上的"随便逛逛",...

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

预览地址:

https://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( 需测试足够高的文档 ):

 

代码如下:


<p id="wrap">
    <p id="rbox"><a class="go" href="#" onclick="return false;">先逛逛>></a></p>
    <p id="mask"></p>
   
    <p class="content">   
        <p id="usertie">
            <span>
                <a id="register" href="#" onclick="return false;">注册</a>
            </span>
        </p>
        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.
    </p>
</p>

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();
    });
});

至此功能完成。

上一篇:

下一篇: