百分百源码网-让建站变得如此简单! 登录 注册 签到领金币!

主页 | 如何升级VIP | TAG标签

当前位置: 主页>网站教程>html5教程> HTML实现遮罩层的办法 HTML中怎样运用遮罩层
分享文章到:

HTML实现遮罩层的办法 HTML中怎样运用遮罩层

发布时间:09/01 来源:未知 浏览: 关键词:
这篇文章主要为大家具体介绍了HTML实现遮罩层的办法,Web页面中使用遮罩层,可防止反复操纵,那么怎样在HTML中使用遮罩层?感乐趣的小伙伴们可以参照 一下

Web页面中使用遮罩层,可防止反复操纵,提醒loading;也可以模拟弹出模态窗口。

实现思绪:一个p作为遮罩层,一个p显示loading动态GIF图片。鄙人面的示例代码中,同时展现了怎样在iframe子页面中调取显示和潜藏遮罩层。

示例代码:

index.html

XML/HTML Code复制内容到剪贴板

<!DOCTYPE html>  
<html lang="zh-CN">  
<head>  
<meta charset="utf-8">  
<meta http-equiv="X-UA-Commpatible" content="IE=edge">  
<title>HTML遮罩层</title>  
<link rel="stylesheet" href="css/index.css">  
</head>  
<body>  
    <p class="header" id="header">  
        <p class="title-outer">  
            <span class="title">  
                HTML遮罩层使用   
            </span>  
        </p>  
    </p>  
    <p class="body" id="body">  
        <iframe id="iframeRight" name="iframeRight" width="100%" height="100%"  
            scrolling="no" frameborder="0"  
            style="border: 0px;margin: 0px; padding: 0px; width: 100%; height: 100%;overflow: hidden;"  
            onload="rightIFrameLoad(this)" src="body.html"></iframe>  
    </p>  
       
    <!-- 遮罩层p -->  
    <p id="overlay" class="overlay"></p>  
    <!-- Loading提醒 p -->  
    <p id="loadingTip" class="loading-tip">  
        <img src="images/loading.gif" />  
    </p>  
       
    <!-- 模拟模态窗口p -->  
    <p class="modal" id="modalp"></p>  
       
    <script type='text/javascript' src="js/jquery-1.10.2.js"></script>  
    <script type="text/javascript" src="js/index.js"></script>  
</body>  
</html>

index.css

CSS Code复制内容到剪贴板

* {   
    margin: 0;   
    padding: 0;   
}   
  
html, body {   
    width: 100%;   
    height: 100%;   
    font-size: 14px;   
}   
  
p.header {   
    width: 100%;   
    height: 100px;   
    border-bottom: 1px dashed blue;   
}   
  
p.title-outer {   
    position: relative;   
    top: 50%;   
    height: 30px;   
}   
span.title {   
    text-align: left;   
    position: relative;   
    left: 3%;   
    top: -50%;   
    font-size: 22px;   
}   
  
p.body {   
    width: 100%;   
}   
.overlay {   
    position: absolute;   
    top: 0px;   
    left: 0px;   
    z-index: 10001;   
    display:none;   
    filter:alpha(opacity=60);   
    background-color: #777;   
    opacity: 0.5;   
    -moz-opacity: 0.5;   
}   
.loading-tip {   
    z-index: 10002;   
    position: fixed;   
    display:none;   
}   
.loading-tip img {   
    width:100px;   
    height:100px;   
}   
  
.modal {   
    position:absolute;   
    width: 600px;   
    height: 360px;   
    border: 1px solid rgba(0, 0, 0, 0.2);   
    box-shadow: 0px 3px 9px rgba(0, 0, 0, 0.5);   
    display: none;   
    z-index: 10003;   
    border-radius: 6px;   
}

index.js

JavaScript Code复制内容到剪贴板

function rightIFrameLoad(iframe) {   
    var pHeight = getWindowInnerHeight() - $('#header').height() - 5;   
       
    $('p.body').height(pHeight);   
    console.log(pHeight);   
       
}   
  
// 阅读器兼容 取得阅读器可视区高度   
function getWindowInnerHeight() {   
    var winHeight = window.innerHeight   
            || (document.documentElement && document.documentElement.clientHeight)   
            || (document.body && document.body.clientHeight);   
    return winHeight;   
       
}   
  
// 阅读器兼容 取得阅读器可视区宽度   
function getWindowInnerWidth() {   
    var winWidth = window.innerWidth   
            || (document.documentElement && document.documentElement.clientWidth)   
            || (document.body && document.body.clientWidth);   
    return winWidth;   
       
}   
  
/** 
 * 显示遮罩层  
 */  
function showOverlay() {   
    // 遮罩层宽高离别为页面内容的宽高   
    $('.overlay').css({'height':$(document).height(),'width':$(document).width()});   
    $('.overlay').show();   
}   
  
/** 
 * 显示Loading提醒  
 */  
function showLoading() {   
    // 先显示遮罩层   
    showOverlay();   
    // Loading提醒窗口居中   
    $("#loadingTip").css('top',   
            (getWindowInnerHeight() - $("#loadingTip").height()) / 2 + 'px');   
    $("#loadingTip").css('left',   
            (getWindowInnerWidth() - $("#loadingTip").width()) / 2 + 'px');   
               
    $("#loadingTip").show();   
    $(document).scroll(function() {   
        return false;   
    });   
}   
  
/** 
 * 潜藏Loading提醒  
 */  
function hideLoading() {   
    $('.overlay').hide();   
    $("#loadingTip").hide();   
    $(document).scroll(function() {   
        return true;   
    });   
}   
  
/** 
 * 模拟弹出模态窗口p  
 * @param innerHtml 模态窗口HTML内容  
 */  
function showModal(innerHtml) {   
    // 取得显示模拟模态窗口用p   
    var dialog = $('#modalp');   
       
    // 设定内容   
    dialog.html(innerHtml);   
       
    // 模态窗口p窗口居中   
    dialog.css({   
        'top' : (getWindowInnerHeight() - dialog.height()) / 2 + 'px',   
        'left' : (getWindowInnerWidth() - dialog.width()) / 2 + 'px'  
    });   
       
    // 窗口p圆角   
    dialog.find('.modal-container').css('border-radius','6px');   
       
    // 模态窗口关闭按钮事件   
    dialog.find('.btn-close').click(function(){   
        closeModal();   
    });   
       
    // 显示遮罩层   
    showOverlay();   
       
    // 显示遮罩层   
    dialog.show();   
}   
  
/** 
 * 模拟关闭模态窗口p  
 */  
function closeModal() {   
    $('.overlay').hide();   
    $('#modalp').hide();   
    $('#modalp').html('');   
}

body.html

XML/HTML Code复制内容到剪贴板

<!DOCTYPE html>  
<html lang="zh-CN">  
<head>  
<meta charset="utf-8">  
<meta http-equiv="X-UA-Commpatible" content="IE=edge">  
<title>body 页面</title>  
<style type="text/css">  
* {   
    margin: 0;   
    padding: 0;   
}   
  
html, body {   
    width: 100%;   
    height: 100%;   
}   
  
.outer {   
    width: 200px;   
    height: 120px;   
    position: relative;   
    top: 50%;   
    left: 50%;   
}   
  
.inner {   
    width: 200px;   
    height: 120px;   
    position: relative;   
    top: -50%;   
    left: -50%;   
}   
  
.button {   
    width: 200px;   
    height: 40px;   
    position: relative;   
}   
    
.button#btnShowLoading {   
    top: 0;   
}   
  
.button#btnShowModal {   
    top: 30%;   
}   
  
</style>  
<script type="text/javascript">  
       
    function showOverlay() {   
        // 调取父窗口显示遮罩层和Loading提醒   
        window.top.window.showLoading();   
  
        // 使用按时器模拟关闭Loading提醒   
        setTimeout(function() {   
            window.top.window.hideLoading();   
        }, 3000);   
  
    }   
  
    function showModal() {   
        // 调取父窗口办法模拟弹出模态窗口   
        window.top.showModal($('#modalContent').html());   
    }   
       
</script>  
</head>  
<body>  
    <p class='outer'>  
        <p class='inner'>  
            <button id='btnShowLoading' class='button' onclick='showOverlay();'>点击弹出遮罩层</button>  
            <button id='btnShowModal' class='button' onclick='showModal();'>点击弹出模态窗口</button>  
        </p>  
    </p>  
       
    <!-- 模态窗口内容p,将本页面p内容设定到父窗口p上并模态显示 -->  
    <p id='modalContent' style='display: none;'>  
        <p class='modal-container' style='width: 100%;height: 100%;background-color: white;'>  
            <p style='width: 100%;height: 49px;position: relative;left: 50%;top: 50%;'>  
                <span style='font-size: 36px; width: 100%; text-align:center; display: inline-block; position:inherit; left: -50%;top: -50%;'>模态窗口1</span>  
            </p>  
            <button class='btn-close' style='width: 100px; height: 30px; position: absolute; right: 30px; bottom: 20px;'>关闭</button>  
        </p>  
    </p>  
    <script type='text/javascript' src="js/jquery-1.10.2.js"></script>  
</body>  
</html>

运转结果:

初始化

显示遮罩层和Loading提醒

显示遮罩层和模拟弹出模态窗口

以上就是本文的全部内容,但愿对大家的学习有所帮忙。

相关引荐:

HTML中使背景图片自顺应阅读器大小

以上就是HTML实现遮罩层的办法 HTML中怎样使用遮罩层的具体内容,更多请关注百分百源码网其它相关文章!

打赏

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

百分百源码网 建议打赏1~10元,土豪随意,感谢您的阅读!

共有150人阅读,期待你的评论!发表评论
昵称: 网址: 验证码: 点击我更换图片
最新评论

本文标签

广告赞助

能出一分力是一分吧!

订阅获得更多模板

本文标签

广告赞助

订阅获得更多模板