Responsive Facebook Comments CSS Hack Broken

后端 未结 17 1700
渐次进展
渐次进展 2020-12-12 22:48

I was using:

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

To make Facebook Comments responsi

相关标签:
17条回答
  • 2020-12-12 23:33

    Here is a small solution.. try it...

    Add this jQuery:

    $(document).ready(function(){
        $(".fb-comments").attr("data-width", $(".fb-comments").parent().width());
    });
    
    0 讨论(0)
  • 2020-12-12 23:34

    Ok this is what I have so far based one Timothy's comment.

    function resizeFbComment(){
    
      if (typeof FB === 'undefined')
        return;
    
      $(".fb-comments").attr("data-width", $(".fb-comments").width());
    
      FB.XFBML.parse($(".comments")[0]);
    
    }
    
    $(window)
      .load(resizeFbComment)
      .resize(resizeFbComment);
    

    Obviously, this is a temporary hack. The windows resize should have a timeout.

    0 讨论(0)
  • 2020-12-12 23:35

    Below is my solution. This script is just a workaround for this bug

    Solution inspired by:

    • above answers
    • this link

    Code below (just replace .comments-area with your own container class name)

    <script>
        (function($,sr){
           // debouncing function from John Hann
           // http://unscriptable.com/index.php/2009/03/20/debouncing-javascript-methods/
          var debounce = function (func, threshold, execAsap) {
              var timeout;
    
              return function debounced () {
                  var obj = this, args = arguments;
                  function delayed () {
                      if (!execAsap)
                          func.apply(obj, args);
                      timeout = null;
                  };
    
                  if (timeout)
                      clearTimeout(timeout);
                  else if (execAsap)
                      func.apply(obj, args);
    
                  timeout = setTimeout(delayed, threshold || 100);
              };
          }
          // smartresize 
          jQuery.fn[sr] = function(fn){  return fn ? this.bind('resize', debounce(fn)) : this.trigger(sr); };
    
        })(jQuery,'smartresize');
    
    
        $(document).ready(function(){
            if ($(".comments-area").width() != document.getElementsByClassName("fb-comments")[0].getAttribute("data-width")) {
                $(".fb-comments").attr("data-width", $(".comments-area").width());
            }
            $(window).smartresize(function(){
                if ($(".comments-area").width() != document.getElementsByClassName("fb-comments")[0].getAttribute("data-width")) {
                    $(".fb-comments").attr("data-width", $(".comments-area").width());
                    FB.XFBML.parse($(".comments-area")[0]);
                }
            });
        });
    </script>
    
    0 讨论(0)
  • 2020-12-12 23:36

    try using this code This might be a little different

    #fbcomments, .fb_iframe_widget, 
    .fb_iframe_widget[style],
    .fb_iframe_widget iframe[style],
    .fb_iframe_widget span,
    #fbcomments iframe [style]
    {
      width: 100% !important;
    }
    
    0 讨论(0)
  • 2020-12-12 23:37

    Adding to the answers here. You really should have a timeout so you're not refreshing the comments dozens of times per second. Also, it's really not great practice to continue crawling the DOM for the elements every time the function is fired, this should be a bit more efficient:

    $(function() {
      // cache some selectors so we're not looking up divs over and
      // over and over on resize
    
      var facebook_comment_resize, 
        comment_resize_timeout,
        $window = $(window),
        $comments_container = $('#comments'),
        $comments = $('.fb-comments');
    
      var facebook_comment_resize = function() {
        // define a function to get the width of the comment container
        // then set the data-width attribute on the facebook comment div
        $comments.attr("data-width", $comments_container.width());
    
        // Reinitialize the comments so it can grab the new width from
        // the data element on the comment div
        FB.XFBML.parse($comments_container.get(0));
      }
    
      // Set a timeout that can clear itself, keeps the comments
      // from refreshing themselves dozens of times during resize
      $window.on('resize', function() {
        clearTimeout( comment_resize_timeout );
        comment_resize_timeout = setTimeout(facebook_comment_resize, 200);
      });
    
      // Set the initial width on load
      facebook_comment_resize();
    });
    
    0 讨论(0)
提交回复
热议问题