响应地在一个iframe中嵌入Bootstrap网页

时间:2014-05-14 10:37:03

标签: css twitter-bootstrap iframe responsive-design embed

我正在开发一个SaaS应用程序,它实质上为客户端提供了一个完整的网页。客户可以访问他们的页面:http://client.myapp.com。但是,我希望允许客户在其网站上轻松嵌入此页面。目前我只提供带有样式表的iframe嵌入代码来重置正文标记的边距。

<head>
<style type="text/css">
html { overflow: auto; }
html, body, div, iframe { margin: 0px; padding: 0px; height: 100%; border: none; }
iframe { display: block; width: 100%; border: none; overflow-y: auto; overflow-x: hidden; }
</style>
</head>

<body>
<iframe id="myapp" name="myapp" src="https://client.myapp.com" frameborder="0" marginheight="0" marginwidth="0" width="100%" height="100%" scrolling="auto"></iframe>
</body>

但是当它们包含iframe时,页面不再响应。如何在使用iframe(或您建议的任何其他方式)时复制原始网页的响应能力?

3 个答案:

答案 0 :(得分:7)

所以我找到了两个问题的解决方案:

解决方案1:

<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>body { margin: 0px; } .embed-container { position: relative; padding-bottom: 100%; height: 0; overflow: hidden; max-width: 100%; min-height: 100%; } .embed-container iframe, .embed-container object, .embed-container embed { position: absolute; top: 0; left: 0; width: 100%; height: 100%; }</style>
</head>
<body>
<div class='embed-container'><iframe src='https://client.myapp.com' style='border:0;'></iframe></div>
</body>

解决方案2:

<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
/* Using http://npr.github.io/responsiveiframe/ */
<script src="jquery.responsiveiframe.js"></script>

<script type='text/javascript'>
$(function() {
  $('iframe').responsiveIframe({xdomain: '*'});
});
</script>

<style>
body {
  margin: 0;
}
</style>
</head>

<body>

<iframe src='https://client.myapp.com' style='border:0; width: 100%; height: 100%;'></iframe>

</body>

到目前为止,它似乎在桌面浏览器(Chrome,Firefox)和我的手机(Android 4)上运行良好

答案 1 :(得分:2)

添加类似这样的内容

@media screen and (min-width: 768px) {
  iframe{    
    min-width:768px;
    }
}

@media screen and (min-width: 992px) {
  iframe{ 
    min-width:992px;
    }
}

@media screen and (min-width: 1200px) {
  iframe{    
    min-width:1200px;
    }
}

到您的css文件。

这些媒体查询会导致iframe宽度与包含的屏幕宽度相匹配。

答案 2 :(得分:0)

确保您在iframes标头中使用:<meta name="viewport" content="width=device-width, initial-scale=1.0">

相关问题