用JavaScript做一个遮盖(Iframe和div),但是当页面本身有Scrollbar时,弹出的遮盖层却始终无法遮盖住Scrollbar。本人推测原因可能是Iframe和div的添加方式导致它们不可能覆盖掉原有页面的Scrollbar,具体的代码如下:
document.body.appendChild(iframe);
document.body.appendChild(div);
这样一来Iframe和div都属于body中的元素,它们就不可能遮盖body之外的元素(页面本身的Scrollbar属于整个HTML这个根节点)
但是某天在浏览gmail时却发现google完美地实现了此功能(gmail-->设置-->选择图片)。
任何想法或解决方案都是受欢迎的。

解决方案 »

  1.   

    是不是GOOGLE的页面本身就处于一个frame中呢.
    就像这样
    <frame>
    GAMIL
    </frame>然后在frame里面弹出层?
      

  2.   

    function showDiv()
    {
    var arrySize = getPageSize();
    var overlayDiv =document.createElement("div");
    overlayDiv.id = "overlay";
    document.body.appendChild(oDiv);
    overlayDiv.style.height = arrySize[1];
    overlayDiv.style.width = arrySize[0];
    overlayDiv.style.display = "block";
    }
    function getPageSize(){

    var xScroll, yScroll;

    if (window.innerHeight && window.scrollMaxY) {
    xScroll = document.body.scrollWidth;
    //alert(xScroll);
    yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
    xScroll = document.body.scrollWidth;
    yScroll = document.body.scrollHeight;
    } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
    xScroll = document.body.offsetWidth;
    //alert(xScroll);
    yScroll = document.body.offsetHeight;
    }

    var windowWidth, windowHeight;
    if (self.innerHeight) { // all except Explorer
    //alert('all except Explorer');
    windowWidth = self.innerWidth;
    windowHeight = self.innerHeight;
    //alert(windowWidth +'all except Explorer'+windowHeight);
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
    windowWidth = document.documentElement.clientWidth;
    windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
    //alert('other Explorers');
    windowWidth = document.body.clientWidth;
    windowHeight = document.body.clientHeight;
    }

    // for small pages with total height less then height of the viewport
    if(yScroll < windowHeight){
    pageHeight = windowHeight;
    } else { 
    pageHeight = yScroll;
    } // for small pages with total width less then width of the viewport
    if(xScroll < windowWidth){
    pageWidth = windowWidth;
    } else {
    pageWidth = xScroll;
    } //alert('pageWidth ' + pageWidth + " pageHeight " +pageHeight + " windowWidth "+ windowWidth + " windowHeight "+windowHeight);
    arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
    return arrayPageSize;
    }
      

  3.   

    又看了一下,整个显示应该是就是一个充满页面的iframe,遮盖层盖住的也只是iframe罢了 (在页面中遮盖iFrame)
    iFrame的滚动条充当页面的滚动条,要确保的是iFrame永远不会超出页面高度,页面的滚动条此时无用且永远不该出现
      

  4.   

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title></title>
    <STYLE>
    #login{
        position: relative;
        display: none;
        top: 20px;
        left: 30px;
        background-color: #ffffff;
        text-align: center;
        border: solid 1px;
        padding: 10px;
        z-index: 1;
    }
      
    </STYLE>
    <script   type="text/javascript">
    var W = screen.width;//取得屏幕分辨率宽度
    var H = screen.height;//取得屏幕分辨率高度function M(id){
    return document.getElementById(id);//用M()方法代替document.getElementById(id)
    }
    function MC(t){
    return document.createElement(t);//用MC()方法代替document.createElement(t)
    };
    //判断浏览器是否为IE
    function isIE(){
       return (document.all && window.ActiveXObject && !window.opera) ? true : false;
    }
    //取得页面的高宽
    function getBodySize(){
    var bodySize = [];
    with(document.documentElement) {
    bodySize[0] = (scrollWidth>clientWidth)?scrollWidth:clientWidth;//如果滚动条的宽度大于页面的宽度,取得滚动条的宽度,否则取页面宽度
    bodySize[1] = (scrollHeight>clientHeight)?scrollHeight:clientHeight;//如果滚动条的高度大于页面的高度,取得滚动条的高度,否则取高度
    }
    return bodySize;
    }
    //创建遮盖层
    function popCoverDiv(){
    if (M("cover_div")) {
    //如果存在遮盖层,则让其显示
    M("cover_div").style.display = 'block';
    } else {
    //否则创建遮盖层
    var coverDiv = MC('div');
    document.body.appendChild(coverDiv);
    coverDiv.id = 'cover_div';
    with(coverDiv.style) {
        position = 'absolute';
        background = '#CCCCCC';
        left = '0px';
        top = '0px';
        var bodySize = getBodySize();
        width = bodySize[0] + 'px'
        height = bodySize[1] + 'px';
        zIndex = 0;
        if (isIE()) {
       filter = "Alpha(Opacity=60)";//IE逆境
        } else {
       opacity = 0.6;
        }
    }
    }
    }
    //让登陆层显示为块
    function showLogin()
    {
    var login=M("login");
           login.style.display = "block";
    }//设置DIV层的样式
    function change(){
       var login = M("login");
       login.style.position = "absolute";
       login.style.border = "1px solid #CCCCCC";
       login.style.background ="#F6F6F6";
       var i=0;
       var bodySize = getBodySize();
       login.style.left = (bodySize[0]-i*i*4)/2+"px";
       login.style.top = (bodySize[1]/2-100-i*i)+"px";
       login.style.width =    i*i*4 + "px";
       login.style.height = i*i*1.5 + "px";
       
       popChange(i);
    }
    //让DIV层大小循环增大
    function popChange(i){
       var login = M("login");
       var bodySize = getBodySize();
       login.style.left = (bodySize[0]-i*i*4)/2+"px";
       login.style.top = (bodySize[1]/2-100-i*i)+"px";
       login.style.width =    i*i*4 + "px";
       login.style.height = i*i*1.5+ "px";
       if(i<=10){
              i++;
              setTimeout("popChange("+i+")",10);//设置超时10毫秒
       }
    }
    //打开DIV层
    function open()
    {
    change();
    showLogin();
    popCoverDiv()
    void(0);//不进行任何操作,如:<a href="#">aaa</a>
    }
    //关闭DIV层
    function close(){
        M('login').style.display = 'none';
        M("cover_div").style.display = 'none';
    void(0);
    }</script>
    </head><body><a href="javascript:open();">登陆</a><div id="login">
       <span>用户登陆</span>
    <div id="panel">
    <lable>用户名: </lable><input type="text" size="20" />
    <lable>密码: </lable><input type="password" size="20">
    <input type="checkbox" /><lable>登陆</lable>
    </div>
    <input type="button" value="提交" />
    <a href="javascript:close();">关闭</a>  
    </div>
    </body>
    </html> 
      

  5.   

    div一定要带上样式:position: absolute;  和 z-index: 3;    数字3可以变的很大,是显示遮盖优先级的控制。absolute;是浮动的意思。
      

  6.   

    测试:IE6, FF3.5
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>danica</title>
    <style type="text/css">
    *{padding:0;margin:0;}
    html,body{width:100%;height:100%;background:#0f0;overflow:hidden;}
    .content{
    width:100%;height:100%;
    background:#00f;
    overflow:auto;
    }
    .shadow{
    width:100%;height:100%;
    overflow:hidden;
    background:#f00;
    opacity:0.3;
    filter:alpha(opacity=30);
    position:absolute;left:0;top:0;
    }
    </style>
    </head>
    <body>
    <div class="shadow"></div>
    <div class="content">
    <p>sdfdsfsdf1111111</p><p>sdfdsfsdf</p><p>sdfdsfsdf</p><p>sdfdsfsdf</p><p>sdfdsfsdf</p><p>sdfdsfsdf</p><p>sdfdsfsdf</p><p>sdfdsfsdf</p><p>sdfdsfsdf</p><p>sdfdsfsdf</p><p>sdfdsfsdf</p><p>sdfdsfsdf</p><p>sdfdsfsdf</p><p>sdfdsfsdf</p><p>sdfdsfsdf</p><p>sdfdsfsdf</p><p>sdfdsfsdf</p><p>sdfdsfsdf</p><p>sdfdsfsdf</p><p>sdfdsfsdf</p><p>sdfdsfsdf</p><p>sdfdsfsdf</p><p>sdfdsfsdf</p><p>sdfdsfsdf</p><p>sdfdsfsdf</p><p>sdfdsfsdf</p><p>sdfdsfsdf</p><p>sdfdsfsdf</p><p>sdfdsfsdf</p><p>sdfdsfsdf</p><p>sdfdsfsdf</p><p>sdfdsfsdf</p><p>sdfdsfsdf</p><p>sdfdsfsdf</p><p>sdfdsfsdf</p><p>sdfdsfsdf</p><p>sdfdsfsdf</p><p>sdfdsfsdf</p><p>sdfdsfsdf</p><p>sdfdsfsdf</p><p>sdfdsfsdf</p><p>sdfdsfsdf</p><p>sdfdsfsdf</p><p>sdfdsfsdf</p><p>sdfdsfsdf</p><p>sdfdsfsdf</p><p>sdfdsfsdf</p><p>sdfdsfsdf</p><p>sdfdsfsdf</p><p>sdfdsfsdf</p><p>sdfdsfsdf</p><p>sdfdsfsdf</p><p>sdfdsfsdf</p><p>sdfdsfsdf</p><p>sdfdsfsdf</p><p>sdfdsfsdf</p><p>sdfdsfsdf</p><p>sdfdsfsdf</p><p>sdfdsfsdf</p><p>sdfdsfsdf</p><p>sdfdsfsdf</p><p>sdfdsfsdf</p><p>sdfdsfsdf</p><p>sdfdsfsdf</p><p>sdfdsfsdf</p><p>sdfdsfsdf</p><p>sdfdsfsdf222222</p>
    </div>
    </body>
    </html>