jQuery+CSS 简单代码实现遮罩层( 兼容主流浏览器 )
/*
** jQuery版本:jQuery-1.8.3.min.js
** 浏览器:Chrome( v31.0.1650.63 m ),IE11,Firefox( v32.0.1 ),IETester下IE8-IE10
** Author:博客园小dee
*/
比起使用注册页和登陆页,网站在当前页使用遮罩层注册和登陆的用户体验要好不少。这里使用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." "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:;
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:;
left:;
display:none;
z-index:;
}
#register{ z-index:; color:blue;}
.content{ width:800px; height:auto; margin:0 auto;}
CSS
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();
});
});
至此功能完成。
如需转载,请在文章页面保留此说明并且给出原文链接。谢谢!
jQuery+CSS 简单代码实现遮罩层( 兼容主流浏览器 )的更多相关文章
- JS+CSS简单实现DIV遮罩层显示隐藏【转藏】
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- JS+CSS简单实现DIV遮罩层显示隐藏
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 在jQuery环境下制作轻巧遮罩层
遮罩层的好处就是可以屏蔽用户对遮罩层下方元素的操作. 制作原理很简单:1设置遮罩层触发按钮 2设置遮罩层内容 3设置遮罩层背景(重点是捕获内容div的大小位置)4设置点击触发按钮遮罩层背景内容同时显示 ...
- 兼容主流浏览器的CSS透明代码
透明往往能产生不错的网页视觉效果下面是兼容主流浏览器的CSS透明代码.transparent_class { filter:alpha(opacity=50); -moz-opacity:0.5; ...
- (转)兼容主流浏览器的CSS透明代码
透明往往能产生不错的网页视觉效果下面是兼容主流浏览器的CSS透明代码.transparent_class { filter:alpha(opacity=50); -moz-opacity:0.5; - ...
- 简单的ajax遮罩层(加载进度圈)cvi_busy_lib.js
cvi_busy_lib.js cvi_busy_lib.js 是一个基于ajax的遮罩js,遮罩区域为body区域.使用比较简单. 效果: 在下面的Js代码,标注为红色标记为需要设置的参数. 1.g ...
- javascript创建css、js,onload触发callback兼容主流浏览器的实现
http://www.fantxi.com/blog/archives/load-css-js-callback/ 由于需要写个函数,既可以加载css,又可以加载js,所以对各主流浏览器对加载js.c ...
- 纯css制作带三角border篇(兼容所有浏览器)
今天帅哥给你们介绍下,如何用 border 来制作三角. html 代码如下: 复制代码 代码如下: <div class="arrow-up"></div> ...
- 兼容主流浏览器的css渐变色
网页中的渐变色区域,渐变色背景,一般都是通过ps图片方法来实现,但是图片放得多了会影响网页的打开速度,本文介绍的就是用纯 CSS 实现 IE .Firefox.Chrome 和 和Safari都支持的 ...
随机推荐
- WW_TRANS_I18N_LOCALE”与“WW_TRANS_I18N_LOCALE”属性
Struts2 i18n国际化(允许用户自行选择语言)转最近在学习struts2,学习资料是李刚著的<struts2权威指南>,这本书写得非常好,非常有学习价值.我在学习过程中,自己跟着做 ...
- gitlab安装
[root@localhost ~]# wget https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rp ...
- Kafka学习笔记(一):概念介绍
Kafka是一个开源的,分布式的,高吞吐量的消息系统.随着Kafka的版本迭代,日趋成熟.大家对它的使用也逐步从日志系统衍生到其他关键业务领域.特别是其超高吞吐量的特性,在互联网领域,使用越来越广泛, ...
- HTML5中的DOMContentLoaded 和 touchmove
Html5的出现确实解决了一部分页面交互的问题,同时它的一些特性还是没能被我们掌握,今天主要聊聊Html5中的DomcontenLoaded和touchmove事件的属性和使用: DomcontenL ...
- javascript普通链表及双向链表
写代码的真是心细啊,每一步操作的先后顺序都在卡准. 我其实只是理解了思想和大概的操作. 真正要用时,可能还是要复制,粘贴...:) function LinkedList(){ var Node = ...
- Struts2拦截器原理以及实例
一.Struts2拦截器定义 1. Struts2拦截器是在访问某个Action或Action的某个方法,字段之前或之后实施拦截,并且Struts2拦截器是可插拔的,拦截器是AOP的一种实现. 2. ...
- web应用配置
tomcat 的 server.html 配置文件 加在</Host>之上 <Context path=”/itcast” docBase=”c:\news” /> path虚 ...
- 源码安装Memcached服务器及其2种PHP客户端
本文所用源码包皆为当时最新stable稳定版. 安装memcached服务器 先安装libevent, 最新版为2.0.21 tar -zxvf libevent-2.0.21-stable.tar. ...
- asp.net控件开发基础(1)(转)原文更多内容
asp.net本身提供了很多控件,提供给我们这些比较懒惰的人使用,我认为控件的作用就在此,因为我们不想重复工作,所以要创建它,这个本身便是一个需求的关系,所以学习控件开发很有意思. wrox网站上有本 ...
- 10688 XYM-AC之路
Description 在华农的众ACMers中,有着一位家喻户晓.人称一鸣惊人的DP神牛—XYM.由于XYM太出名了,他的仰慕者决定给XYM写一部个人传奇以传承他光辉的AC之路. 为了使故事更加真实 ...