如何使谷歌加徽章具有响应性?

4

目前,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个回答

4

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

<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>

2

你可以使用“静态”徽章选项来代替小部件,它基本上只是一个链接和一个图像。小部件代码在呈现后不会监听其属性的更新。此外,在徽章小部件上设置的高度和宽度存在限制,这可能会导致徽章无法呈现,具体取决于响应式布局中如何计算宽度/高度。

使用静态徽章,您将失去动态功能,例如人们能够立即将您添加到圆圈而无需导航,但它确实为您提供了更多的布局控制权。


0
这是我所做的...
使用Twitter Bootstrap脚手架类,我创建了多个徽章,根据浏览器所处的“模式”(例如桌面、平板电脑或手机)进行渲染。 Twitter Bootstrap Scaffolding 那么就做到了!

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