IE9 iframe内的媒体查询失败

时间:2012-04-25 13:02:39

标签: html css css3 media-queries responsive-design

我的CSS中有以下媒体查询:

@media screen and (min-width: 0px) and (max-width: 319px) {
    body {background-color:red;}
}

@media screen and (min-width: 320px) and (max-width: 480px) {
    body {background-color:orange;}
}

@media screen and (min-width: 481px) and (max-width: 980px) {
    body {background-color:yellow;}
}

@media screen and (min-width: 981px) and (max-width: 1200px) {
    body {background-color:green;}
}

@media screen and (min-width: 1201px) {
    body {background-color:blue;}
}

以及相应尺寸的五个iframe:

<iframe frameBorder="0" src="index.html" height="320" width="255" ></iframe>

查询在独立的html文件中启动,但在iframe上下文中查看时,它们在IE9中失败。所有其他浏览器显示OK。

任何人都知道为什么?感谢

[edit]对于记录,父和子html具有相同的docType,CSS作为text / css提供。

5 个答案:

答案 0 :(得分:22)

不是真正的答案,但可以帮助其他人在IE中找到解决此错误的方法。

包含iframe的网页

<link href="style.css" rel="stylesheet">

iframe网页

<link href="style.css?frameX" rel="stylesheet">

param frameX阻止IE缓存css页面,因此iframe具有独立于其他页面的响应式布局。这只是一个黑客(可怕的),并帮助其他人找到这个问题的答案。

示例文件

<强>的index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Title</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="style.css" rel="stylesheet">
</head>
<body>
  <p></p>

  <hr>
  250px frame
  <iframe frameBorder="0" src="frame1.html" height="100" width="250" id='frame_1'></iframe>  

  <hr>
  350px frame
  <iframe frameBorder="0" src="frame2.html" height="100" width="350" id='frame_2'></iframe>  

  <hr>
  390px frame
  <iframe frameBorder="0" src="frame3.html" height="100" width="390" id='frame_3'></iframe>  

</div>
</body>

<强> frame1.html

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Title</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="style.css?page=frame1" rel="stylesheet">
</head>
<body>
  <p></p>
</body>
</html>

<强> frame2.html

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Title</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="style.css?page=frame2" rel="stylesheet">
</head>
<body>
  <p></p>
</body>
</html>

<强> frame3.html

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Title</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="style.css?page=frame3" rel="stylesheet">
</head>
<body>
  <p></p>
</body>
</html>

<强>的style.css

iframe{display:block;}

@media (max-width: 8000px)
{
  body p:before {content: "bigger than 550px";}
}

@media (max-width: 550px)
{
  body p:before {content: "max550";}
}

@media (max-width: 450px)
{
  body p:before {content: "max450";}
}

@media (max-width: 350px)
{
  body p:before {content: "max350";}
}


@media (max-width: 250px)
{
  body p:before {content: "max250";}
}

答案 1 :(得分:3)

这就是我的所作所为:

  • 使用jQuery ...
  • 获取页面上的所有iframe
  • 每个iframe的onload找到所有样式表链接
  • 为每个样式表抓取url并附加一个随机的num'nocache'查询字符串
  • 创建一个新的样式表链接标记并将其附加到头部
  • 进行。

以下是代码:

// This forces the media queries to run in IE iframes, by waiting for the iframes to load and then,
// re-getting/applying the stylesheet
(function($){
    // Get each of the iframes on this page
    $('iframe').each(function(index){
        // On load of each iframe:
        $(this).on('load', function(){
            var iframeDoc = $(this).contents();

            // For each stylesheet reference found:
            iframeDoc.find('link[rel="stylesheet"]').each(function(){
                // Get the current stylesheet's url and add a 'nocache' random num query string to it
                var cssURL = $(this).attr('href');
                cssURL += (cssURL.indexOf('?') != -1)? '&':'?';
                cssURL += 'nocache=' + (Math.random() + index);

                // Create a new stylesheet link tag and append it to the head
                $("<link/>", {
                    rel: "stylesheet",
                    type: "text/css",
                    href: cssURL
                }).appendTo(iframeDoc.find('head'));
            });

        });
    });
})(jQuery);

答案 2 :(得分:1)

你很可能无法解决这个问题。媒体查询基于视口大小,我想IE在此上下文中并未将iFrame视为完全成熟的视口。

您最好的选择很可能是添加一些检测大小的JavaScript,并将类添加到您用于响应式设计的相同阈值。这也可以使您向后兼容不支持媒体查询的浏览器。

答案 3 :(得分:1)

有同样的问题。但是我找到了一个简单的方法。使用JS创建iframe我只是将类似?nocache = Math.random()的内容添加到iframe src中。这对我来说是固定的:)

答案 4 :(得分:0)

我在使用IE9时遇到了这个问题。父页面没有声明doctype 在父页面中添加html5 doctype(<!DOCTYPE html>)解决了这个问题。