兼容主流瀏覽器的jQuery+CSS 實(shí)現(xiàn)遮罩層的簡單代碼
在頁面點(diǎn)擊"注冊",出現(xiàn)一層有不透明度的黑色遮罩;遮罩層的上方是注冊框;此時(shí)無法點(diǎn)擊頁面上除注冊框外的其他元素;點(diǎn)擊注冊框上的"隨便逛逛",遮罩層消失。
預(yù)覽地址:
http://jsfiddle.net/p2x3c7df/embedded/result/
要點(diǎn):
1.注冊框始終水平、垂直居中,包括鼠標(biāo)滾輪上下滾動(dòng)頁面、縮放頁面和調(diào)整瀏覽器窗口大小時(shí)
主要由CSS控制,注冊框的寬度和高度都已經(jīng)確定( 620*420px ),首先使用position:fixed來使它相對于瀏覽器窗口絕對定位;然后使它垂直居中:top:50%; left:50%; margin:-210px 0 0 -310px;
2.當(dāng)縮放頁面和調(diào)整瀏覽器窗口大小時(shí),遮罩層需始終覆蓋整個(gè)文檔和充滿整個(gè)瀏覽器可視窗口以及需要滾動(dòng)才能瀏覽到的部分,需要兼容Chrome和IE等不同內(nèi)核瀏覽器;
兩個(gè)重要的屬性:js的window.screen.availHeight和jQuery的$(document.body).outerHeight(true)。window.screen.availHeight指屏幕可用工作區(qū)域的高,$(document.body).outerHeight(true)指瀏覽器當(dāng)前窗口文檔body的總寬度 包括border padding margin。window.screen.availHeight主要用于IE(11)縮放頁面后,遮罩層仍能充滿瀏覽器窗口。
3.遮罩層出現(xiàn)時(shí),頁面仍可以上下滾動(dòng),但是無法操作頁面中除登陸框外的其他元素
代碼:
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(){
//點(diǎn)擊注冊
$("#register").click(function(){
if(window.screen.availHeight > $(document.body).outerHeight(true)){
//當(dāng)屏幕可用工作區(qū)域的高度 > 瀏覽器當(dāng)前窗口文檔body的總高度 包括border padding margin時(shí)( 縮放時(shí) )
$("#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();
});
//根據(jù)瀏覽器可視窗口的變化調(diào)整遮罩的寬度和高度,使遮罩充滿瀏覽器
$(window).resize(function(){
//根據(jù)瀏覽器窗口變化改變遮罩寬度和高度,使遮罩充滿整個(gè)瀏覽器
if($("#mask").css("width")!=0){
$("#mask").css("width","100%"); //必要時(shí)可對寬度也作出判斷
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();
});
});
至此功能完成。
- jQuery實(shí)現(xiàn)簡單網(wǎng)頁遮罩層/彈出層效果兼容IE6、IE7
- JS遮罩層效果 兼容ie firefox jQuery遮罩層
- jQuery+css+html實(shí)現(xiàn)頁面遮罩彈出框
- JQuery 遮罩層實(shí)現(xiàn)(mask)實(shí)現(xiàn)代碼
- jQuery+html5實(shí)現(xiàn)div彈出層并遮罩背景
- jquery彈出關(guān)閉遮罩層實(shí)例
- jQuery操作dom實(shí)現(xiàn)彈出頁面遮罩層(web端和移動(dòng)端阻止遮罩層的滑動(dòng))
- jquery 圓角遮罩圖片實(shí)現(xiàn)圖片圓角
- jQuery+AJAX實(shí)現(xiàn)遮罩層登錄驗(yàn)證界面(附源碼)
- jquery下實(shí)現(xiàn)overlay遮罩層代碼
- jQuery實(shí)現(xiàn)可兼容IE6的遮罩功能詳解
相關(guān)文章
jQuery ajax中使用confirm,確認(rèn)是否刪除的簡單實(shí)例
下面小編就為大家?guī)硪黄猨Query ajax 中使用confirm ,確認(rèn)是否刪除的簡單實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-06-06jQuery UI實(shí)現(xiàn)動(dòng)畫效果代碼分享
這篇文章給大家總結(jié)了jQuery UI實(shí)現(xiàn)動(dòng)畫效果的實(shí)例代碼,有需要的朋友們可以參考測試下。2018-08-08用jquery與css打造個(gè)性化的單選框和復(fù)選框
利用jquery和css打造個(gè)性化的單選框和復(fù)選框,喜歡的朋友可以參考下。2010-10-10基于jQuery實(shí)現(xiàn)列表循環(huán)滾動(dòng)小技巧(超簡單)
只要能夠不停地把第一個(gè)item移動(dòng)到末尾,其余的自會(huì)往上填補(bǔ)空缺,填補(bǔ)的過程用動(dòng)畫放慢些,就能有不斷滾動(dòng)的視覺效果了,這種效果基于jquery怎么實(shí)現(xiàn)呢?下面小編給大家?guī)砹薺Query列表循環(huán)滾動(dòng)效果的實(shí)現(xiàn)思路代碼,一起看看吧2021-08-08從零開始學(xué)習(xí)jQuery (一) 開天辟地入門篇
本篇文章是入門第一篇, 主要是簡單介紹jQuery, 通過簡單示例指導(dǎo)大家如何編寫jQuery代碼以及搭建開發(fā)環(huán)境. 詳細(xì)講解了如何在Visual Studio中配合使用jQuery.2010-10-10jquery選擇器-根據(jù)多個(gè)屬性選擇示例代碼
根據(jù)多個(gè)屬性選擇在某些對選擇比較精確的情況下,還是比較實(shí)用的,其實(shí)很簡單,不會(huì)的朋友看下本文2013-10-10js jquery獲取當(dāng)前元素的兄弟級 上一個(gè) 下一個(gè)元素
js獲取方法要比jq的方法麻煩的多,主要是因?yàn)镕F瀏覽器,因?yàn)镕F瀏覽器也會(huì)把換行當(dāng)作dom元素,下面跟著小編一起來學(xué)習(xí)js jquery獲取當(dāng)前元素的兄弟級 上一個(gè) 下一個(gè)元素的,需要的朋友一起來學(xué)習(xí)吧2015-09-09