Responsive Facebook Comments CSS Hack Broken

后端 未结 17 1742
渐次进展
渐次进展 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:14

    The debounce solution from Ka. is good but this could be more straightforward and should memoize the nodes. Make sure you wrap your fb-comments in some container:

    Then (if using jQuery) setup a resize that debounces the number of requests. Also, make sure you cache the nodes you are checking to speed things up:

    var $comments = null;
    var $fbComments = null;
    var resizeTimeout = null;
    
    function resizeComments() {
      if (resizeTimeout) clearTimeout(resizeTimeout);
      resizeTimeout = setTimeout(function() {
        resizeTimeout = null;
        if (typeof FB === 'undefined') return;
        // The class of the wrapper element above is 'comments'
        $comments = $comments || $('.comments');
        $fbComments = $fbComments || $(".fb-comments");
        if ($comments.width() !== parseInt($fbComments.attr("data-width"), 10)) {
          $fbComments.attr("data-width", $comments.width());
          FB.XFBML.parse($comments[0]);
        }
      }, 100);
    }
    

    Then call this function on document ready and when the window resizes:

    $(document).ready(function() {
      resizeComments();
    
      $(window).resize(function() {
        resizeComments();
      });  
    });
    

提交回复
热议问题