透明背景在IE9中无法正常工作

时间:2012-08-04 20:56:39

标签: jquery html css internet-explorer lightbox

基本上它是一个灯箱演示。代码来自here.我想点击链接“显示面板”然后弹出一个表单。 它在Firefox中运行良好但在IE9中出错。

html代码非常简单:

<body>
<a id="show-panel" href="#">Show Panel</a>

 <div id="lightbox-panel">
    <h2>Lightbox Panel</h2>
    <p>You can add any valid content here.</p>
    <p align="center">
    <a id="close-panel" href="#">Close this window</a>
    </p>
    </div><!-- /lightbox-panel -->

    <div id="lightbox"> </div><!-- /lightbox -->
    </body>

CSS:

<style type="text/css">

#lightbox {
display:none;
opacity:0.9;
filter:alpha(opacity=90);
position:fixed;
top:0px;
left:0px;
min-width:100%;
min-height:100%;
z-index:1000;
background-color: #FFCC66;
    }

#lightbox-panel {
display:none;
position:fixed;
top:180px;
left:50%;
margin-left:-200px;
width:450px;
border:2px solid #CCCCCC;
z-index:1001;
background-color: #999900;
padding-top: 10px;
padding-right: 15px;
padding-bottom: 10px;
padding-left: 15px;
    }

.show-panel {
font-family: Arial, Helvetica, sans-serif;
font-size: xx-large;
    }
    </style>

JQuery代码:

$("a#show-panel").click(function(){
$("#lightbox, #lightbox-panel").fadeIn(300);
})
 $("a#close-panel").click(function(){
 $("#lightbox, #lightbox-panel").fadeOut(300);
 })

firefox中的效果: firebox

IE9中的效果: IE9

您不仅可以看到透明背景,还可以看到灯箱的位置不正确。

感谢您的帮助。

整个代码:

https://skydrive.live.com/?cid=03a8bb9eaeeff1db#cid=03A8BB9EAEEFF1DB&id=3A8BB9EAEEFF1DB!234

3 个答案:

答案 0 :(得分:1)

问题

正如Jashwant所说,这可能是一个doctype问题。这就是问题所在。在我将{5} doctype添加到<html>标记前面时,它已得到修复。

<!DOCTYPE html>

请记住,IE并不像其他浏览器那么聪明。因此,您应该使用正确的网页设置:

<!DOCTYPE html>

<html>
  <head>
  </head>

  <body>
  </body>
</html>



它似乎正在处理这个JsFiddle

我确实看到你说它在你身边的JsFiddle也能正常工作。那么,您可以将整个HTML发布到pasteTool pastebin上吗?这样可以更容易地解决这个问题。

有一件事,我想指出的是,灯箱垂直居中的方式并不均匀。

灯箱的宽度为450px,因此对于margin-left,您希望将其减半。 450的一半是225,所以你的新margin-left是-225px。

这将使它完美地位于中心。我也喜欢使用JavaScript来获取对象的宽度并在那里进行数学运算。这将使页面加载更长一点,因为它必须做更多的工作,但我认为它工作得很好。

这是一个有点偏离主题,考虑到你,灯箱似乎没有在那个HTML页面上工作,但我认为这也可能有用。确保再次检查HTML。可能存在IE无法解决的问题,而谷歌浏览器等其他浏览器可以解决这个问题。

哦,如果你想水平居中灯箱,你可以使用相同的方法,你只需要一半的高度,并使用margin-top

中的负面形式

答案 1 :(得分:1)

可能是doctype问题。

将它放在<html>

之前
<!DOCTYPE html>

此外,

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<{1>}部分中的

答案 2 :(得分:0)

这似乎对我有用。你是积极的还有其他原因导致这个问题吗?

Live DEMO

我建议您同时设置html和body标签的高度和宽度。我不确定这是否是原因,但我认为这会有所帮助。

html, body {
    height:100%;
    width:100%;
}

我已经创建了一个示例,其中包含您在html文件中提供的内容,它似乎正常工作。我想我们需要更多信息。这些是我放在html文件中的内容。

<!DOCTYPE html>

<html>
    <head>
        <style type="text/css">
            #lightbox {
                display:none;
                opacity:0.9;
                filter:alpha(opacity=90);
                position:fixed;
                top:0px;
                left:0px;
                min-width:100%;
                min-height:100%;
                z-index:1000;
                background-color: #FFCC66;
            }

            #lightbox-panel {
                display:none;
                position:fixed;
                top:180px;
                left:50%;
                margin-left:-200px;
                width:450px;
                border:2px solid #CCCCCC;
                z-index:1001;
                background-color: #999900;
                padding-top: 10px;
                padding-right: 15px;
                padding-bottom: 10px;
                padding-left: 15px;
            }

            .show-panel {
                font-family: Arial, Helvetica, sans-serif;
                font-size: xx-large;
            }
        </style>
    </head>
    <body>
        <a id="show-panel" href="#">Show Panel</a>

        <div id="lightbox-panel">
            <h2>Lightbox Panel</h2>

            <p>You can add any valid content here.</p>
            <p align="center">
                <a id="close-panel" href="#">Close this window</a>
            </p>
        </div><!-- /lightbox-panel -->

        <div id="lightbox"> </div><!-- /lightbox -->

        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
        <script type="text/javascript">
            $("a#show-panel").click(function(){
                $("#lightbox, #lightbox-panel").fadeIn(300);
            });

            $("a#close-panel").click(function(){
                $("#lightbox, #lightbox-panel").fadeOut(300);
            })
        </script>
    </body>
</html>

当我将我的测试与你给我们的测试进行比较时,你会错过doctype。一旦我添加了doctype,一切正常。

相关问题