首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >响应式TrustPilot小部件

响应式TrustPilot小部件
EN

Stack Overflow用户
提问于 2013-12-17 03:20:19
回答 3查看 9.2K关注 0票数 0

我有一个来自truspilot的小部件,它可以生成一个iframe,我正在尝试使其响应。我已经找了几个小时,想知道怎么做,但似乎没有一个解决方案能有效地调整我的身高和宽度。

JSFIDDLE

这是小部件

代码语言:javascript
复制
<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宽度在以下情况下工作正常:

代码语言:javascript
复制
iframe {width:100%}

但是iframe中有一个元素需要设置为100%才能使布局工作。

代码语言:javascript
复制
.tp-box {width:100%;}

但是我不能样式化来自父元素的iframe中的内容。所以我试着添加:

代码语言:javascript
复制
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));
    }
}

但没有成功。

那么我能做些什么来使宽度和高度流畅呢?

EN

回答 3

Stack Overflow用户

发布于 2014-04-16 20:41:57

我最近遇到了一个类似的问题,特别是让宽度与父div匹配,并且也做出了响应。我不明白为什么Trust Pilot认为设置一些东西来使框架的高度动态而不是宽度动态是合适的,但不管怎样。

首先,我为父div设置了一些CSS,比如:

代码语言:javascript
复制
<style type="text/css">
div.trust-pilot{
    width: 100% !important;
    height: auto;
}
</style>

这可能实际上不是必需的,但我还是把它放在了那里。

以下是我使用的“信任箱”设置(参见http://trustpilot.github.io/developers/#trustbox):

代码语言:javascript
复制
<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匹配,我使用:

代码语言:javascript
复制
<script type="text/javascript">
jQuery( window ).load(function() {
    _width = jQuery('#trust-pilot').innerWidth();
    jQuery('#tpiframe-box0').attr('width',_width);
});
</script>

然后为了让框做出响应,我使用:

代码语言:javascript
复制
<script type="text/javascript">
jQuery( window ).resize(function() {
    _width = jQuery('#trust-pilot').innerWidth();
    jQuery('#tpiframe-box0').attr('width',_width);
});
</script>

我希望这能有所帮助。肖恩。

票数 2
EN

Stack Overflow用户

发布于 2013-12-17 03:23:48

请参阅Making an iframe responsive ::这条评论:

iframe本身(‘

box')可以响应。但是iframe中的所有内容都是一个单独的页面,因此不在CSS或JS的域中。- DA。7月25日20:12

票数 0
EN

Stack Overflow用户

发布于 2014-07-24 21:45:08

最简单的解决方法是确定有多少个响应状态,例如:如果在响应主题中有四个不同的宽度,那么只需创建四个不同的css规则并包含显示标签(如果是主css,则示例如下):

代码语言:javascript
复制
.trustpilot_a {display:block;}
.trustpilot_b {display:none;}
.trustpilot_c {display:none;}
.trustpilot_d {display:none;}

然后将'trustbox‘代码包装在名为'trustpilot_a’的div中,使其具有所需宽度的正确大小(下面是所需宽度为210px的示例):

代码语言:javascript
复制
<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‘。

快乐的日子

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20619078

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档