如何使Google Plus徽章响应?

时间:2013-01-09 22:27:14

标签: twitter-bootstrap responsive-design google-plus

目前,Google Plus徽章会自动响应宽度变化:https://developers.google.com/+/plugins/badge/

我们的新网站设计使用Bootstrap进行响应式布局,我们如何才能做出响应?

以下是我们当前的徽章代码:

<!-- Place this tag where you want the badge to render. -->
<div class="g-plus" data-width="230" data-href="//plus.google.com/102018846923934084444" data-rel="publisher"></div>

<!-- Place this tag after the last badge tag. -->
<script type="text/javascript">
  (function() {
   var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
   po.src = 'https://apis.google.com/js/plusone.js';
   var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
})();
</script>

如您所见,默认宽度为230像素。有点困惑如何干净利落地做这件事?

谢谢!

3 个答案:

答案 0 :(得分:4)

这对我有用。如果我们只有一个g-person-class类徽章。

<div id="google-badge" style="width: 100%">
    <!-- Place this tag where you want the widget to render. -->
    <div class="g-person" data-href="//plus.google.com/104771303799616655833" data-rel="author" data-width="180"></div>
    <!-- Place this tag after the last widget tag. --><script type="text/javascript">
    window.___gcfg = {lang: 'de'};

    // just put this in here
    document.getElementsByClassName('g-person')[0].setAttribute('data-width', document.getElementById('google-badge').clientWidth);

    (function () {
        var po = document.createElement('script');
        po.type = 'text/javascript';
        po.async = true;
        po.src = 'https://apis.google.com/js/platform.js';
        var s = document.getElementsByTagName('script')[0];
        s.parentNode.insertBefore(po, s);
    })();
    </script>
</div>

答案 1 :(得分:2)

您可以使用“静态”徽章选项,而不是使用窗口小部件,它基本上只是一个链接和一个图像。窗口小部件代码在呈现后不会侦听其属性的更新。此外,徽章窗口小部件的高度和宽度具有可能最终导致徽章无法渲染的约束,具体取决于在响应布局中如何计算宽度/高度。

使用静态徽章会丢失动态功能,例如人们可以在不导航的情况下立即将您添加到圆圈中,但它确实可以让您更好地控制布局。

答案 2 :(得分:0)

这就是我做的......

使用twitter的bootstrap脚手架类,我根据浏览器的“模式”(例如桌面,平板电脑或手机)创建了多个徽章。

Twitter Bootstrap Scaffolding

那很有用!

相关问题