如何使 Facebook 评论小部件具有流体宽度?

发布于 2024-12-05 09:05:56 字数 249 浏览 2 评论 0原文

是否可以使 Facebook 的评论小部件具有流动宽度?他们的文档显示了fb:comments xfbml或iframe的宽度字段指定为:

  • width - 插件的宽度(以像素为单位)。建议的最小宽度:400px。

所以也许这是不可能的...

Is it possible to make Facebook's comments widget a fluid width? Their documentation shows a width field for the fb:comments xfbml or iframe which is specified as:

  • width - the width of the plugin in pixels. Minimum recommended width: 400px.

So maybe it's not possible...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(19

幼儿园老大 2024-12-12 09:05:56

艾伦,你的解决方案正在工作,但看起来 Facebook 更新了他们的插件并打破了风格。我使用通用选择器让它再次工作:

.fb-comments, .fb-comments * {
    width:100% !important;
}

Alan your solution was working however it looks like facebook updated their plugin and broke the style. I got it working again using the universal selector:

.fb-comments, .fb-comments * {
    width:100% !important;
}
甩你一脸翔 2024-12-12 09:05:56

我找到了一个使用CSS的解决方案。灵感来自于这篇文章
http://css-tricks.com/2708-override-inline-样式与 css/

.fb-comments, .fb-comments iframe[style] {width: 100% !important;}

I found a solution using css. Inspiration came from this article
http://css-tricks.com/2708-override-inline-styles-with-css/

.fb-comments, .fb-comments iframe[style] {width: 100% !important;}
離人涙 2024-12-12 09:05:56

可能是 FB 的下一次更改,这次效果更好:


.fb_iframe_widget,
.fb_iframe_widget span,
.fb_iframe_widget iframe[style]  {width: 100% !important;}

Probably next change from FB and this time this works better:


.fb_iframe_widget,
.fb_iframe_widget span,
.fb_iframe_widget iframe[style]  {width: 100% !important;}
花落人断肠 2024-12-12 09:05:56

截至 2014 年 3 月,没有一个 CSS 解决方案对我有用。Facebook 似乎已更改插件,现在在 iFrame 内的容器上设置宽度,您无法使用 CSS 覆盖该宽度。

经过一番挖掘,我注意到注释的宽度实际上是由 iframe src width=XXX 的最后一个参数控制的。考虑到这一点,我是这样解决的:

// ON PAGE LOAD
setTimeout(function(){
  resizeFacebookComments();
}, 1000);

// ON PAGE RESIZE
$(window).on('resize', function(){
  resizeFacebookComments();
});

function resizeFacebookComments(){
  var src   = $('.fb-comments iframe').attr('src').split('width='),
      width = $('#container').width();

  $('.fb-comments iframe').attr('src', src[0] + 'width=' + width);
}

#container 是您希望评论插件拉伸以适应的容器的宽度。将其更改为您需要的任何内容,并且此代码应该适合您。

我使用了超时,因为加载 iframe 后我无法让它触发。对此的任何帮助将不胜感激,但超时可以完成这项工作。

编辑:此解决方案会导致后退按钮损坏。我现在正在尝试这个解决方案,它似乎更好: https://stackoverflow.com/a/22257586/394788

None of the CSS solutions worked for me as of March 2014. It seems that Facebook has changed the plugin to now set a width on a container within the iFrame which you are unable to override with CSS.

After a little digging, I noticed that the width of the comments are actually controlled by the last param of the iframe src width=XXX. With that in mind, here's how I solved it:

// ON PAGE LOAD
setTimeout(function(){
  resizeFacebookComments();
}, 1000);

// ON PAGE RESIZE
$(window).on('resize', function(){
  resizeFacebookComments();
});

function resizeFacebookComments(){
  var src   = $('.fb-comments iframe').attr('src').split('width='),
      width = $('#container').width();

  $('.fb-comments iframe').attr('src', src[0] + 'width=' + width);
}

#container is the width of your container that you want the comments plugin to stretch to fit within. Change this to whatever you need it to be and this code should work for you.

I'm using a timeout because I wasn't able to get it to fire once the iframe was loaded. Any help on that would be appreciated but the timeout does the job.

EDIT: This solution causes the back button to break. I'm trying out this solution now and it seems to be better: https://stackoverflow.com/a/22257586/394788

静待花开 2024-12-12 09:05:56

我想我有一个解决方案,可以对 Ryan 3 月 5 日的回答进行一些改进。

您可以执行以下操作,而不是在延迟后重新加载 Facebook iframe。

插入一个常规的 Facebook 评论标签,但在类中附加一个额外的位,这样 Facebook 就不会检测到它,但您可以。

<div class="fb-comments-unloaded" data-href="..." data-numposts="10" data-colorscheme="light"></div>

然后添加一些 JS 来选取这个 div,将其修改为所需的宽度,然后触发 Facebook 的解析器。

var foundFBComs = false;

$('.fb-comments-unloaded').each(function(){

    var $fbCom = $(this),
        contWidth = $fbCom.parent().width();

    $fbCom.attr('data-width', contWidth)
          .removeClass('fb-comments-unloaded')
          .addClass('fb-comments');

    foundFBComs = true;

});

if (foundFBComs && typeof FB !== 'undefined') {
    FB.XFBML.parse();
}

I think I have a solution which improves a little on Ryan's answer from March 5th.

Rather than re-loading the Facebook iframe after a delay, you could do the following.

Insert a regular Facebook comments tag, but append an extra bit to the class, so that Facebook doesn't detect it, but you can.

<div class="fb-comments-unloaded" data-href="..." data-numposts="10" data-colorscheme="light"></div>

Then add some JS which picks this div up, modifies it with the desired width, then triggers Facebook's parser.

var foundFBComs = false;

$('.fb-comments-unloaded').each(function(){

    var $fbCom = $(this),
        contWidth = $fbCom.parent().width();

    $fbCom.attr('data-width', contWidth)
          .removeClass('fb-comments-unloaded')
          .addClass('fb-comments');

    foundFBComs = true;

});

if (foundFBComs && typeof FB !== 'undefined') {
    FB.XFBML.parse();
}
葬花如无物 2024-12-12 09:05:56

Facebook 已经解决了这个问题。您现在可以添加 data-width="100%" 或将宽度选项设置为 100% 并根据需要删除任何疯狂的 js hack!

This problem has been addressed by Facebook. You can now add data-width="100%" or set the width option to100% and delete any crazy js hacks as appropriate!

烟沫凡尘 2024-12-12 09:05:56

我通常希望避免使用通用选择器以获得更好的性能。 如果您检查 facebook 选择器,您应该能够得到相同的结果,

.fb-comments, .fb-comments span, .fb-comments iframe {width: 100% !important;}

可能会得到更多改进...

I generally want to avoid using the universal selector for better performance. You should be able to get the same result with

.fb-comments, .fb-comments span, .fb-comments iframe {width: 100% !important;}

Could probably be improved upon more if you check the facebook selectors...

迟到的我 2024-12-12 09:05:56

在初始化 Facebook 插件之前插入此代码,您将获得流畅的 Facebook 评论:):):)

 $(".fb-comments").attr("data-width", $(".fb-comments").parent().width());
$(window).on('resize', function() {
resizeFacebookComments();
});

function resizeFacebookComments() {
var src = $('.fb-comments iframe').attr('src').split('width='),
    width = $(".fb-comments").parent().width();

$('.fb-comments iframe').attr('src', src[0] + 'width=' + width);
}

insert this code before initialize facebook plugin and you will have fluid fb comments :):):)

 $(".fb-comments").attr("data-width", $(".fb-comments").parent().width());
$(window).on('resize', function() {
resizeFacebookComments();
});

function resizeFacebookComments() {
var src = $('.fb-comments iframe').attr('src').split('width='),
    width = $(".fb-comments").parent().width();

$('.fb-comments iframe').attr('src', src[0] + 'width=' + width);
}
浅唱ヾ落雨殇 2024-12-12 09:05:56

看起来 facebook 更新了他们的文档..你现在可以使用 data-width="100%" 或 width="100%"

https://developers.facebook.com/docs/plugins/comments/

looks like facebook updated their documentation..you can now use data-width="100%" or width="100%"

https://developers.facebook.com/docs/plugins/comments/

你的背包 2024-12-12 09:05:56

我还不能发表评论,因为我的声誉还不够高,但是截至 2014 年 12 月 21 日,有一个解决方案

可以在 CSS 中工作:

.fb_iframe_widget,
.fb_iframe_widget span,
.fb_iframe_widget iframe[style]  {width: 100% !important;}

您必须将 data-width="" 部分设置为 100% FB 插件代码即

<div class="fb-comments" data-href="your site here" data-width="100%" data-numposts="5" data-colorscheme="light"></div>

流体运动的工作示例:http://www.unitedbiker.org

希望这对大家有所帮助,其想法是将 iframe 样式覆盖为 100% 的父元素,并设置实际的FB插件到100%的iframe。这就是我的工作。

I can't comment yet because my reputations not yet high enough, however there is a solution to this as of Dec 21, 2014

for this to work in CSS:

.fb_iframe_widget,
.fb_iframe_widget span,
.fb_iframe_widget iframe[style]  {width: 100% !important;}

you MUST have the data-width="" section set to 100% within the FB plugin code i.e

<div class="fb-comments" data-href="your site here" data-width="100%" data-numposts="5" data-colorscheme="light"></div>

working example with fluid motion: http:www.unitedbiker.org

Hope this helps everyone out, the idea is to override the iframe style to be 100% of the parent element, and to set the actual FB plugin to 100% of the iframe. This was my work around.

活雷疯 2024-12-12 09:05:56

这对我有用,但我需要添加 span 标签才能正常工作:

.fb-comments, .fb-comments iframe[style], .fb-comments span { width: 100% !important; }

This worked for me, but I need to add span tag to work correctly:

.fb-comments, .fb-comments iframe[style], .fb-comments span { width: 100% !important; }
往事随风而去 2024-12-12 09:05:56

我认为这对每个人都有效。 2013 年 12 月 26 日在 http://eddie11.com/dj-eddie-talks/

#fbSEOComments, 
#fbSEOComments span,
#fbSEOComments div,
#fbSEOComments iframe,
#fbSEOComments iframe[style],
#fbSEOComments .fb_iframe_widget,
#fbSEOComments .fb_iframe_widget span,
#fbSEOComments .fb_iframe_widget iframe
{
    width: 100% !important;
}

以下内容都不适合我,但我还是把它留在那里。

.fb-comments,
.fb-comments *,
.fb-comments iframe[style],
.fb_iframe_widget,
.fb_iframe_widget span,
.fb_iframe_widget iframe,
.fb_iframe_widget iframe[style],
.fb-comments span,
.fb-comments iframe,

I think this will work for everyone. Works for me Dec 26, 2013 at http://eddie11.com/dj-eddie-talks/

#fbSEOComments, 
#fbSEOComments span,
#fbSEOComments div,
#fbSEOComments iframe,
#fbSEOComments iframe[style],
#fbSEOComments .fb_iframe_widget,
#fbSEOComments .fb_iframe_widget span,
#fbSEOComments .fb_iframe_widget iframe
{
    width: 100% !important;
}

None of the below worked for me but I left it there anyway.

.fb-comments,
.fb-comments *,
.fb-comments iframe[style],
.fb_iframe_widget,
.fb_iframe_widget span,
.fb_iframe_widget iframe,
.fb_iframe_widget iframe[style],
.fb-comments span,
.fb-comments iframe,
甜中书 2024-12-12 09:05:56

我知道这是一个老问题,但这也许对某人有帮助。

你可以使用下面的代码让你的注释变得流畅

.fb-comments, .fb-comments iframe[style], .fb-like-box, .fb-like-box iframe[style] {width: 100% !important;}
.fb-comments span, .fb-comments iframe span[style], .fb-like-box span, .fb-like-box iframe span[style] {width: 100% !important;}

可以检查这里的代码,因为这里的pre函数删除了一些东西。我是新来的。问候

Facebook 评论流畅

I know this is old question, but this maybe can help to someone.

You can use the following code to make your comments fluid


.fb-comments, .fb-comments iframe[style], .fb-like-box, .fb-like-box iframe[style] {width: 100% !important;}
.fb-comments span, .fb-comments iframe span[style], .fb-like-box span, .fb-like-box iframe span[style] {width: 100% !important;}

Can check the code here, because the pre function here delete some things. I'm new here. Regards

Facebook Comments Fluid

微暖i 2024-12-12 09:05:56

花了一些时间调查这个问题。尽管 FB 建议以像素为单位指定绝对宽度,但看起来只要将其设置为“100%”就可以了。请注意下面的数据宽度。

<div class="fb-comments" data-width="100%" data-href="http://www.sample.com/" data-numposts="5" data-colorscheme="light"></div>

是的,这很简单,没有 onresize 回调,没有 iframe src 修改。

spent some time investigating this issue. Though FB proposes to specify absolute width in pixels, looks like it works if you just set it to '100%'. Note data-width below.

<div class="fb-comments" data-width="100%" data-href="http://www.sample.com/" data-numposts="5" data-colorscheme="light"></div>

Yeah yeah, that's easy, no onresize callbacks no iframe src modifications.

吻安 2024-12-12 09:05:56

在解决这个问题相当长一段时间后,我发现了一个花絮,它应该可以帮助您弄清楚此页面上的哪些 CSS 技巧将有助于您的 Facebook 评论插件的特定站点/版本/年份。在浏览器中查看您的网站并检查 Facebook 评论插件周围的元素。你应该找到你添加的一个片段,现在由 facebook 的 javascript 小部件修饰,看起来像这样:

<fb:comments href="http://mywebpage.com" 
    fb-xfbml-state="rendered" class="fb_iframe_widget">

记下这样的部分:

class="<whatever class your version is using>"

这是你想要使用的类 - 所以在我上面的示例中,你需要添加以下款式:

<style>
    .fb_iframe_widget,
    .fb_iframe_widget iframe[style],
    .fb_iframe_widget span[style],
    .fb_iframe_widget *  {
        width: 100% !important;
    }
</style>

After grappling with this for quite some time I found a tidbit that should help you to figure out which of the CSS tricks on this page will help for your particular site/version/year of facebook's comment plugin. Look at your site in a browser and inspect the elements around the facebook comment plugin. You should find a snippet that you added and is now embellished by facebook's javascript widget that looks something like this:

<fb:comments href="http://mywebpage.com" 
    fb-xfbml-state="rendered" class="fb_iframe_widget">

take note of the part that says:

class="<whatever class your version is using>"

this is the class you want to use - so in my example above, you would want to add the following styles:

<style>
    .fb_iframe_widget,
    .fb_iframe_widget iframe[style],
    .fb_iframe_widget span[style],
    .fb_iframe_widget *  {
        width: 100% !important;
    }
</style>
他不在意 2024-12-12 09:05:56
.fb-comments, .fb-comments iframe[style],  .fb-comments > span {width: 100% !important;}
.fb-comments, .fb-comments iframe[style],  .fb-comments > span {width: 100% !important;}
时光礼记 2024-12-12 09:05:56

如果您的 Facebook 评论插件的代码类似于 (XFBML):

<fb:comments href="{URL_HERE}" numposts="5" colorscheme="light"></fb:comments>

那么以下 CSS 应该可以完成工作:

.fb_iframe_widget, .fb_iframe_widget span, .fb_iframe_widget iframe {
     width: 100% !important;
}

If your Facebook Comments Plugin's code looks like (XFBML):

<fb:comments href="{URL_HERE}" numposts="5" colorscheme="light"></fb:comments>

Then the following CSS should get the job done:

.fb_iframe_widget, .fb_iframe_widget span, .fb_iframe_widget iframe {
     width: 100% !important;
}
倦话 2024-12-12 09:05:56

这是唯一对我来说正确的事情!

$(".fb-comments").attr("data-width", $(".fb-comments").parent().width());
$(window).on('resize', function() {
resizeFacebookComments();
});

function resizeFacebookComments() {
var src = $('.fb-comments iframe').attr('src').split('width='),
width = $(".fb-comments").`enter code here`parent().width();

$('.fb-comments iframe').attr('src', src[0] + 'width=' + width);
}

This is the only thing that worked correctly for me!

$(".fb-comments").attr("data-width", $(".fb-comments").parent().width());
$(window).on('resize', function() {
resizeFacebookComments();
});

function resizeFacebookComments() {
var src = $('.fb-comments iframe').attr('src').split('width='),
width = $(".fb-comments").`enter code here`parent().width();

$('.fb-comments iframe').attr('src', src[0] + 'width=' + width);
}
罪#恶を代价 2024-12-12 09:05:56

无需覆盖CSS,使用data-width="100%"

<div class="fb-comments" data-width="100%" data-href="yourURL"></div>

No need to override the css, Use data-width="100%"

<div class="fb-comments" data-width="100%" data-href="yourURL"></div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文