兼容主流浏览器的jQuery+CSS 实现遮罩层的简单代码
在页面点击"注册",出现一层有不透明度的黑色遮罩;遮罩层的上方是注册框;此时无法点击页面上除注册框外的其他元素;点击注册框上的"随便逛逛",遮罩层消失。
预览地址:
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."