在IE8及以下版本中使用Respond.js和Twitter Bootstrap。

4

我正在使用Twitter Bootstrap实现一个响应式网站,但在IE8及以下版本中无法使用媒体查询。

我尝试使用respond.js创建了一个简单的示例,但媒体查询仍然无法在IE7-8上工作,以下是HTML代码:

<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8">
<title>Bootstrap 101 Template</title>
<meta name="Description" content="Web description here" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet">
<link href="./css/styles.css" rel="stylesheet">
<!--[if IE]>
  <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link rel="shortcut icon" href="./img/favicon.png">
</head>
<body>
<div class="container">
  <h1>Hello, world!</h1>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script src="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>
<script src="./js/respond.min.js"></script>
<script src="./js/scripts.js"></script>
</body>
</html>

这里是style.css的内容:

@media only screen and (max-width: 38.75em) {
body {
    background: green;
}
}

致敬

3个回答

2

https://github.com/scottjehl/Respond#cdnx-domain-setup

CDN/X-Domain设置

Respond.js通过AJAX请求CSS的原始副本来工作,因此如果您将样式表托管在CDN(或子域)上,则需要上传代理页面以启用跨域通信。

请参阅cross-domain/example.html进行演示:

Upload cross-domain/respond-proxy.html to your external domain
Upload cross-domain/respond.proxy.gif to your origin domain
Reference the file(s) via <link /> element(s):

<!-- Respond.js proxy on external server -->
<link href="http://externalcdn.com/respond-proxy.html" id="respond-proxy" rel="respond-proxy" />

<!-- Respond.js redirect location on local server -->
<link href="/path/to/respond.proxy.gif" id="respond-redirect" rel="respond-redirect" />

<!-- Respond.js proxy script on local server -->
<script src="/path/to/respond.proxy.js"></script>

如果您在跨域设置方面遇到问题,请确保respond-proxy.html文件没有附加查询字符串。

注意:非常感谢@doctyper在跨域代理方面的贡献!


谢谢Craig。这是直接从文档中摘录的,但它并没有真正解决问题。你不能将HTML文件上传到Bootstrap的CDN,所以你需要放弃CDN吗? - Simon East
Simon,说实话,我还没有花时间解决那个问题。我知道这是可能的。在此期间,我只是使用本地CSS来使用Bootstrap和CDN来使用其他不需要通过respond.js进行调整的内容。如果你找到答案,请发布!=) - Craig London

0

如果您不想添加任何与respond.js相关的代码,请通过CDN加载bootstrap.min.css,除了IE 8之外。对于IE 8,请使用本地静态文件。

    <!--[if lte IE 8]>                                                      
        <link rel="stylesheet" href="/path/to/bootstrap.min.css' %}" />
    <![endif]-->                                                            
    <!--[if gt IE 8]>                                         
        <link rel="stylesheet" href="{CDN Address}" />                                                      
    <![endif]-->                                              
    <!--[if !IE]><!-->                                        
        <link rel="stylesheet" href="{CDN Address}" />
    <!--<![endif]-->

已在IE 8 ~ 11和其他Webkit浏览器上进行了测试。


0

您需要在</head>标签之前包含<script src="./js/respond.min.js"></script>


1
在 </head> 标签上方,紧接着样式表的位置。 - T. Junghans
2
respond.js应该可以在HTML文档底部加载时正常工作,但可能会更容易出现FOUC问题。我认为真正的问题是respond.js无法从其他域(例如Bootstrap CDN)中拉取样式表。 - Michael Martin-Smucker

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接