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


Posted in Javascript onOctober 14, 2014

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

预览地址:

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

    });

});

至此功能完成。

Javascript 相关文章推荐
js Date自定义函数 延迟脚本执行
Mar 10 Javascript
dojo学习第二天 ajax异步请求之绑定列表
Aug 29 Javascript
js分解url参数(面向对象-极简主义法应用)
Aug 09 Javascript
JavaScript函数作用域链分析
Feb 13 Javascript
jQuery获得指定元素坐标的方法
Apr 14 Javascript
js数组的五种迭代方法及两种归并方法(推荐)
Jun 14 Javascript
jQuery grep()方法详解及实例代码
Oct 30 Javascript
jQuery实现移动端Tab选项卡效果
Mar 15 Javascript
$.browser.msie 为空或不是对象问题的多种解决方法
Mar 19 Javascript
JS使用插件cryptojs进行加密解密数据实例
May 11 Javascript
解决vue-router 二级导航默认选中某一选项的问题
Nov 01 Javascript
vue.js iview打包上线后字体图标不显示解决办法
Jan 20 Javascript
从数据库读取数据后将其输出成html标签的三种方法
Oct 13 #Javascript
JavaScript使用循环和分割来替换和删除元素实例
Oct 13 #Javascript
javascript实现分栏显示小技巧附图
Oct 13 #Javascript
jquery引用方法时传递参数原理分析
Oct 13 #Javascript
form.submit()不能提交表单的错误原因及解决方法
Oct 13 #Javascript
js的touch事件的实际引用
Oct 13 #Javascript
JQuery 使用attr方法实现下拉列表选中
Oct 13 #Javascript
You might like
提升PHP执行速度全攻略
2006/10/09 PHP
在服务端进行目录建立、删除,文件上传、删除的过程的php代码
2008/09/10 PHP
php中global和$GLOBALS[]的分析之一
2012/02/02 PHP
PHP彩蛋信息介绍和阻止泄漏的方法(隐藏功能)
2014/08/06 PHP
使用php方法curl抓取AJAX异步内容思路分析及代码分享
2014/08/25 PHP
PHP分页初探 一个最简单的PHP分页代码的简单实现
2016/06/21 PHP
thinkphp5.0自定义验证规则使用方法
2017/11/16 PHP
JS 强制设为首页的代码
2009/01/31 Javascript
模仿JQuery.extend函数扩展自己对象的js代码
2009/12/09 Javascript
javascript实用小函数使用介绍
2013/11/11 Javascript
jQuery的each终止或跳过示例代码
2013/12/12 Javascript
jquery datepicker参数介绍和示例
2014/04/15 Javascript
跟我学Nodejs(一)--- Node.js简介及安装开发环境
2014/05/20 NodeJs
Javascript基础教程之while语句
2015/01/18 Javascript
《JavaScript高级编程》学习笔记之object和array引用类型
2015/11/01 Javascript
jQuery实现定位滚动条位置
2016/08/05 Javascript
js实现常用排序算法
2016/08/09 Javascript
js严格模式总结(分享)
2016/08/22 Javascript
AngularJS用户选择器指令实例分析
2016/11/04 Javascript
JQuery 封装 Ajax 常用方法(推荐)
2017/05/21 jQuery
JavaScript数组去重的方法总结【12种方法,号称史上最全】
2019/02/28 Javascript
[04:32]DOTA2著名解说配音敌法师 现场专访海涛怒切假腿
2013/12/20 DOTA
Python写的Socks5协议代理服务器
2014/08/06 Python
python threading模块操作多线程介绍
2015/04/08 Python
python实现mysql的读写分离及负载均衡
2018/02/04 Python
python 实现在shell窗口中编写print不向屏幕输出
2020/02/19 Python
解决Django提交表单报错:CSRF token missing or incorrect的问题
2020/03/13 Python
美国工业用品采购网站:Zoro.com
2020/10/27 全球购物
交通安全教育制度
2014/02/02 职场文书
给公司的建议书范文
2014/05/13 职场文书
电教室标语
2014/06/20 职场文书
设计专业毕业生求职信
2014/06/25 职场文书
公司租房协议书
2014/10/14 职场文书
2014年办公室个人工作总结
2014/11/12 职场文书
幼儿园保教工作总结2015
2015/10/15 职场文书
python和Appium的移动端多设备自动化测试框架
2022/04/26 Python