我有一个来自truspilot的小部件,它可以生成一个iframe,我正在尝试使其响应。我已经找了几个小时,想知道怎么做,但似乎没有一个解决方案能有效地调整我的身高和宽度。
JSFIDDLE
这是小部件
<div class="tp_-_box" data-tp-settings="domainId:3660907">
<a href="http://www.trustpilot.com/review/www.longtiestore.com" rel="nofollow" hidden>Long Tie Store Reviews</a>
</div>
<script type="text/javascript">
(function () { var a = "https:" == document.location.protocol ? "https://ssl.trustpilot.com" : "http://s.trustpilot.com", b = document.createElement("script"); b.type = "text/javascript"; b.async = true; b.src = a + "/tpelements/tp_elements_all.js"; var c = document.getElementsByTagName("script")[0]; c.parentNode.insertBefore(b, c) })();
</script>我的iframe宽度在以下情况下工作正常:
iframe {width:100%}但是iframe中有一个元素需要设置为100%才能使布局工作。
.tp-box {width:100%;}但是我不能样式化来自父元素的iframe中的内容。所以我试着添加:
window.onload = function () {
if (parent) {
var oHead = document.getElementsByTagName("head")[0];
var arrStyleSheets = parent.document.getElementsByTagName("style");
for (var i = 0; i < arrStyleSheets.length; i++)
oHead.appendChild(arrStyleSheets[i].cloneNode(true));
}
}但没有成功。
那么我能做些什么来使宽度和高度流畅呢?
发布于 2014-04-16 20:41:57
我最近遇到了一个类似的问题,特别是让宽度与父div匹配,并且也做出了响应。我不明白为什么Trust Pilot认为设置一些东西来使框架的高度动态而不是宽度动态是合适的,但不管怎样。
首先,我为父div设置了一些CSS,比如:
<style type="text/css">
div.trust-pilot{
width: 100% !important;
height: auto;
}
</style>这可能实际上不是必需的,但我还是把它放在了那里。
以下是我使用的“信任箱”设置(参见http://trustpilot.github.io/developers/#trustbox):
<div id="trust-pilot" class="trust-pilot">
<div class="tp_-_box" data-tp-settings="domainId: xxxxxxx,
bgColor: F5F5F5,
showRatingText: False,
showComplementaryText: False,
showUserImage: False,
showDate: False,
width: -1,
numOfReviews: 6,
useDynamicHeight: False,
height: 348 ">
</div>
<script type="text/javascript">
(function () {
var a = "https:" == document.location.protocol ? "https://ssl.trustpilot.com" : "http://s.trustpilot.com", b = document.createElement("script");
b.type = "text/javascript";
b.async = true;
b.src = a + "/tpelements/tp_elements_all.js";
var c = document.getElementsByTagName("script")[0];
c.parentNode.insertBefore(b, c) })();
</script>
</div>将domainId替换为有效的值(在这个问题中,它是3660907)。请注意,width:-1参数可能已经对您起作用了,除非您希望页面具有响应性(如下所示)。
首先,为了使宽度与父div匹配,我使用:
<script type="text/javascript">
jQuery( window ).load(function() {
_width = jQuery('#trust-pilot').innerWidth();
jQuery('#tpiframe-box0').attr('width',_width);
});
</script>然后为了让框做出响应,我使用:
<script type="text/javascript">
jQuery( window ).resize(function() {
_width = jQuery('#trust-pilot').innerWidth();
jQuery('#tpiframe-box0').attr('width',_width);
});
</script>我希望这能有所帮助。肖恩。
发布于 2013-12-17 03:23:48
请参阅Making an iframe responsive ::这条评论:
iframe本身(‘
box')可以响应。但是iframe中的所有内容都是一个单独的页面,因此不在CSS或JS的域中。- DA。7月25日20:12
发布于 2014-07-24 21:45:08
最简单的解决方法是确定有多少个响应状态,例如:如果在响应主题中有四个不同的宽度,那么只需创建四个不同的css规则并包含显示标签(如果是主css,则示例如下):
.trustpilot_a {display:block;}
.trustpilot_b {display:none;}
.trustpilot_c {display:none;}
.trustpilot_d {display:none;}然后将'trustbox‘代码包装在名为'trustpilot_a’的div中,使其具有所需宽度的正确大小(下面是所需宽度为210px的示例):
<div class="block trustpilot_a">
<div class="tp_-_box" data-tp-settings="domainId:[your number],
linkColor:59962B,
fontColor:4077BD,
bgColor:ECF1F3,
bgBarColor:4077BD,
borderColor:D3EAF8,
width:210,
fontBarColor:4077BD,
useDarkLogo:False,height:100">
<a href="[your trust pilot domain]" rel="nofollow">[your title]</a></div>
<script type="text/javascript">// <![CDATA[
(function () { var a = "https:" == document.location.protocol ? "https://s.trustpilot.com" : "http://s.trustpilot.com", b = document.createElement("script"); b.type = "text/javascript"; b.async = true; b.src = a + "/tpelements/tp_elements_all.js"; var c = document.getElementsByTagName("script")[0]; c.parentNode.insertBefore(b, c) })();
// ]]></script>
</div>然后重复四次,将类更改为'a (宽度:210)‘,b(宽度:190)等。将它们全部粘贴到您的代码中,只需在您的响应式主题中更改'display’设置为您需要的'width‘。
快乐的日子
https://stackoverflow.com/questions/20619078
复制相似问题