遮罩层的实现:
1、实现思路:
利用div实现遮罩层效果:利用一个全屏、半透明的div遮住页面上其它元素
js xml">js-doctype"><!DOCTYPE html>
js-tag"><js-title">html>
js-tag"><js-title">head>
js-tag"><js-title">meta js-attribute">charset=js-value">"utf-8">
js-tag"><js-title">title>遮罩层js-tag"></js-title">title>
js-tag"><js-title">style>css">
js-tag">html,js-tag">body js-rules">{
js-rule">js-attribute">margin:js-value">js-number">0;
js-rule">js-attribute">height:js-value">js-number">100%;
js-rule">}
js-id">#shadejs-rules">{
js-rule">js-attribute">position:js-value">absolute;
js-rule">js-attribute">top:js-value">js-number">0;
js-rule">js-attribute">left:js-value">js-number">0;
js-rule">js-attribute">z-index:js-value">js-number">2;
js-rule">js-attribute">width:js-value">js-number">100%;
js-rule">js-attribute">height:js-value">js-number">100%;
js-rule">js-attribute">background-color:js-value">js-hexcolor">#000;
js-rule">js-attribute">opacity:js-value">js-number">0.3;
js-comment">/*兼容IE8及以下版本浏览器*/
js-rule">js-attribute">filter:js-value"> js-function">alpha(opacity=js-number">30);
js-rule">js-attribute">display:js-value">none;
js-rule">}
js-id">#modal js-rules">{
js-rule">js-attribute">position:js-value"> absolute;
js-rule">js-attribute">z-index:js-value">js-number">3;
js-rule">js-attribute">top:js-value"> js-number">0;
js-rule">js-attribute">bottom:js-value"> js-number">0;
js-rule">js-attribute">left:js-value"> js-number">0;
js-rule">js-attribute">right:js-value"> js-number">0;
js-rule">js-attribute">width:js-value">js-number">200px;
js-rule">js-attribute">height:js-value">js-number">200px;
js-rule">js-attribute">margin:js-value"> auto;
js-rule">js-attribute">display:js-value">none;
js-rule">js-attribute">background-color:js-value">js-hexcolor">#FF0;
js-rule">}
js-tag"></js-title">style>
js-tag"><js-title">script>
window.onload=js-function">js-keyword">functionjs-params">(){
js-keyword">var a1=document.getElementById(js-string">'a1');
js-keyword">var a2=document.getElementById(js-string">'a2');
a1.onclick=shield;
a2.onclick=cancel_shield;
js-function">js-keyword">function js-title">shieldjs-params">(e){
e.preventDefault();
js-keyword">var shade = document.getElementById(js-string">"shade");
shade.style.display = js-string">"block";
js-keyword">var modal = document.getElementById(js-string">"modal");
modal.style.display = js-string">"block";
}
js-function">js-keyword">function js-title">cancel_shieldjs-params">(e){
e.preventDefault();
js-keyword">var shade = document.getElementById(js-string">"shade");
shade.style.display = js-string">"none";
js-keyword">var modal = document.getElementById(js-string">"modal");
modal.style.display = js-string">"none";
}
};
js-tag"></js-title">script>
js-tag"></js-title">head>
js-tag"><js-title">body>
js-tag"><js-title">a js-attribute">id=js-value">"a1" js-attribute">href=js-value">"#">打开遮罩js-tag"></js-title">a>
js-tag"><js-title">div js-attribute">id=js-value">"shade">js-tag"></js-title">div>
js-tag"><js-title">div js-attribute">id=js-value">"modal">
js-tag"><js-title">a js-attribute">id=js-value">"a2" js-attribute">href=js-value">"#">关闭js-tag"></js-title">a>
js-tag"></js-title">div>
js-tag"></js-title">body>
js-tag"></js-title">html>
效果图:
点击“打开遮罩”:
点击关闭:
遮罩层的应用:图片模态框
js xml">js-doctype"><!DOCTYPE html>
js-tag"><js-title">html js-attribute">lang=js-value">"en">
js-tag"><js-title">head>
js-tag"><js-title">meta js-attribute">charset=js-value">"UTF-8">
js-tag"><js-title">title>图片模态框js-tag"></js-title">title>
js-tag"><js-title">style>css">
js-id">#myImgjs-rules">{
js-rule">js-attribute">-webkit-border-radius:js-value"> js-number">5px;
js-rule">js-attribute">-moz-border-radius:js-value"> js-number">5px;
js-rule">js-attribute">border-radius:js-value"> js-number">5px;
js-rule">js-attribute">cursor:js-value"> pointer;
js-rule">js-attribute">-webkit-transition:js-value"> all js-number">0.3s;
js-rule">js-attribute">-moz-transition:js-value"> all js-number">0.3s;
js-rule">js-attribute">-o-transition:js-value"> all js-number">0.3s;
js-rule">js-attribute">transition:js-value"> all js-number">0.3s;
js-rule">}
js-id">#myImgjs-pseudo">:hoverjs-rules">{
js-rule">js-attribute">opacity:js-value"> js-number">0.7;
js-rule">}
js-class">.modaljs-rules">{
js-rule">js-attribute">display:js-value"> none;
js-rule">js-attribute">position:js-value"> fixed;
js-rule">js-attribute">z-index:js-value">js-number">1;
js-rule">js-attribute">left:js-value">js-number">0;
js-rule">js-attribute">top:js-value">js-number">0;
js-rule">js-attribute">width:js-value">js-number">100%;
js-rule">js-attribute">height:js-value">js-number">100%;
js-rule">js-attribute">padding-top:js-value">js-number">50px;
js-rule">js-attribute">overflow:js-value"> auto;
js-rule">js-attribute">background-color:js-value">js-function">rgb(js-number">0,js-number">0,js-number">0);
js-rule">js-attribute">background-color:js-value">js-function">rgba(js-number">0,js-number">0,js-number">0,js-number">0.9);
js-rule">}
js-class">.modal-contentjs-rules">{
js-rule">js-attribute">margin:js-value">auto;
js-rule">js-attribute">display:js-value"> block;
js-rule">js-attribute">width:js-value">js-number">100%;
js-rule">js-attribute">max-width:js-value"> js-number">700px;
js-rule">}
js-id">#captionjs-rules">{
js-rule">js-attribute">margin:js-value">auto;
js-rule">js-attribute">display:js-value"> block;
js-rule">js-attribute">width:js-value">js-number">80%;
js-rule">js-attribute">max-width:js-value">js-number">700px;
js-rule">js-attribute">text-align:js-value"> center;
js-rule">js-attribute">color:js-value"> js-hexcolor">#ccc;
js-rule">js-attribute">padding:js-value">js-number">10px js-number">0;
js-rule">js-attribute">height:js-value">js-number">150px;
js-rule">}
js-class">.modal-content,js-id">#captionjs-rules">{
js-rule">js-attribute">-webkit-animation:js-value">zoom js-number">0.6s;
js-rule">js-attribute">-o-animation:js-value">zoom js-number">0.6s;
js-rule">js-attribute">animation:js-value">zoom js-number">0.6s;
js-rule">}
js-at_rule">@js-keyword">-webkit-keyframes zoom {
js-tag">fromjs-rules">{
js-rule">js-attribute">-webkit-transform:js-value"> js-function">scale(js-number">0.1);
js-rule">}
js-tag">tojs-rules">{
js-rule">js-attribute">-webkit-transform:js-value"> js-function">scale(js-number">1);
js-rule">}
}
js-at_rule">@js-keyword">keyframes zoom {
js-tag">fromjs-rules">{
js-rule">js-attribute">transform:js-value"> js-function">scale(js-number">0.1);
js-rule">}
js-tag">tojs-rules">{
js-rule">js-attribute">transform:js-value"> js-function">scale(js-number">1);
js-rule">}
}
js-class">.closejs-rules">{
js-rule">js-attribute">position:js-value"> absolute;
js-rule">js-attribute">top:js-value">js-number">15px;
js-rule">js-attribute">right:js-value">js-number">35px;
js-rule">js-attribute">color:js-value"> js-hexcolor">#f1f1f1;
js-rule">js-attribute">font-size:js-value"> js-number">40px;
js-rule">js-attribute">font-weight:js-value">bold;
js-rule">js-attribute">transition:js-value"> js-number">0.3s;
js-rule">}
js-class">.closejs-pseudo">:hover,js-class">.closejs-pseudo">:focusjs-rules">{
js-rule">js-attribute">color:js-value"> js-hexcolor">#bbb;
js-rule">js-attribute">text-decoration:js-value"> none;
js-rule">js-attribute">cursor:js-value"> pointer;
js-rule">}
js-comment">/* 100% Image Width on Smaller Screens */
js-at_rule">@js-keyword">media only screen and (max-width: js-number">700px){
js-class">.modal-content js-rules">{
js-rule">js-attribute">width:js-value"> js-number">100%;
js-rule">}
}
js-tag"></js-title">style>
js-tag"></js-title">head>
js-tag"><js-title">body>
js-tag"><js-title">h2>图片模态框js-tag"></js-title">h2>
js-tag"><js-title">p>本实例演示了如何结合 CSS 和 JavaScript 来一起渲染图片。js-tag"><js-title">br/>
首先,我们使用 CSS 来创建 modal 窗口 (对话框), 默认是隐藏的。js-tag"><js-title">br/>
然后,我们使用 JavaScript 来显示模态窗口,当我们点击图片时,图片会在弹出的窗口中显示:
js-tag"></js-title">p>
js-tag"><js-title">img js-attribute">id=js-value">"myImg" js-attribute">src=js-value">"http://www.runoob.com/wp-content/uploads/2016/04/img_lights.jpg" js-attribute">alt=js-value">"Northern Lights, Norway" js-attribute">width=js-value">"300" js-attribute">height=js-value">"200">
js-comment"><!--The modal-->
js-tag"><js-title">div js-attribute">id=js-value">"myModal" js-attribute">class=js-value">"modal">
js-tag"><js-title">span js-attribute">class=js-value">"close">×js-tag"></js-title">span>
js-tag"><js-title">img js-attribute">class=js-value">"modal-content" js-attribute">id=js-value">"img01"/>
js-tag"><js-title">div js-attribute">id=js-value">"caption">js-tag"></js-title">div>
js-tag"></js-title">div>
js-tag"><js-title">script>
js-keyword">var modal=document.getElementById(js-string">'myModal');
js-keyword">var img=document.getElementById(js-string">'myImg');
js-keyword">var modalImg=document.getElementById(js-string">'img01');
js-keyword">var captionText=document.getElementById(js-string">'caption');
img.onclick=js-function">js-keyword">functionjs-params">(){
modal.style.display=js-string">'block';
modalImg.src=js-keyword">this.src;
modalImg.alt=js-keyword">this.alt;
captionText.innerHTML=js-keyword">this.alt;
};
js-keyword">var span=document.getElementsByClassName(js-string">'close')[js-number">0];
span.onclick=js-function">js-keyword">functionjs-params">(){
modal.style.display=js-string">'none';
}
js-tag"></js-title">script>
js-tag"></js-title">body>
js-tag"></js-title">html>
效果图:
点击图片:
点击“X”: