Responsive width Facebook Page Plugin

前端 未结 17 1879
温柔的废话
温柔的废话 2020-12-02 16:28

Facebook introduced a new Page Plugin to replace the Like box plugin.

Documentation: https://developers.facebook.com/docs/plugins/page-plugin/

I\'m replacing

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

    I'm putting this here for those that had the same problem as me and couldn't find their answer here between the comments or on any other stackoverflow page.

    I added the Facebook Page Plugin with settings that would adjust it to the container width.

    data-adapt-container-width="true"

    However, one or more elements within the iframe or Javascript SDK element were given the width of 340px making the Page Plugin not adapt to the container width. While it should have a minimum of 180px and a maximum of 500px.

    The code provided by Facebook was missing something however.

    <div class="fb-page" data-href="https://www.facebook.com/Paras-Design-393209377418188" data-tabs="timeline" data-small-header="false" data-adapt-container-width="true" data-hide-cover="false" data-show-facepile="false"></div>
    

    By manually adding data-width="500" the Page Plugin responded as expected and adapted to the container width to a maximum width of 500px.

    I hope this helps anyone coming across the same problem.

    0 讨论(0)
  • 2020-12-02 17:24

    for those of you looking for a JS solution to sizes smaller than 280

    here is my code snippit:

    facebookScale = function () {
        var adjustWidth;
        adjustWidth = $('.facebook-likebox').width() / 280;
        return $('.fb-page').css({
          '-webkit-transform': 'scale(' + adjustWidth + ')',
          '-moz-transform': 'scale(' + adjustWidth + ')',
          'transform': 'scale(' + adjustWidth + ')'
        });
      }
    
    $(function(){
        $('.fb-page').on('resize DOMSubtreeModified', facebookScale);
        $(window).on('resize', facebookScale);
      })
    

    edit. make sure the following is in the css:

    .fb-page{
      transform-origin: 0 0;
      -webkit-transform-origin: 0px 0px;
      -moz-transform-origin: 0px 0px;
    }
    
    0 讨论(0)
  • 2020-12-02 17:24

    The accepted answer works for me only the first time that page is loaded, but after the browser is resized or I change from landscape to portrait in mobile devices the Facebook Plugin (version 3.2) doesn't adapt to my container. The solution for me was just check the Adapt to plugin container width, and add a listener to know when the page is resized then I remove the facebook iframe and load it again.

    window.addEventListener('resize', this.resize);
    resize = () => {
        document.querySelector('.fb-page').classList.remove('fb_iframe_widget');
        document.querySelector('.fb-page').classList.remove('fb_iframe_widget_fluid');
        FB.XFBML.parse();
    };
    

    By the way, I added a timeout to avoid multiple refreshing while the user is resizing the page, but it's optional

    var FB_UPDATE_TIMEOUT = null;
    
    window.addEventListener('resize', this.resize);
    resize = () => {
        if(FB_UPDATE_TIMEOUT === null) {
            FB_UPDATE_TIMEOUT = window.setTimeout(function() {
                FB_UPDATE_TIMEOUT = null;
                document.querySelector('.fb-page').classList.remove('fb_iframe_widget');
                document.querySelector('.fb-page').classList.remove('fb_iframe_widget_fluid');
                FB.XFBML.parse();
            }, 100);
        }
    };
    

    Demo

    0 讨论(0)
  • 2020-12-02 17:24

    Here's a dynamic resize of iframe include way, with some delayed rerender on resize event:

    function _facebook() {
    
        var parent = document.getElementById('facebook');
        var width = (window.innerWidth > 540) ? 500 : window.innerWidth - 40;
        if (parent.firstChild && parent.firstChild.width == width) { return; }
        var url = 'https://www.facebook.com/plugins/page.php?href=YOUR_FACEBOOK_PAGE_URL&tabs=messages&width='+width+'&height=500&small_header=false&adapt_container_width=true&hide_cover=false&show_facepile=true&appId=YOUR_APP_ID';
        var i = document.createElement('iframe');
        i.src = url;
        i.width = width;
        i.height = 500;
        i.style = 'border:none;overflow:hidden';
        i.scrolling = "no";
        i.frameborder = "0";
        i.allowTransparency = "true";
        while (parent.firstChild) { parent.removeChild(parent.firstChild); }
        parent.appendChild(i);
    
    }
    
    _facebook();
    
    $(window).resize(function() {
        clearTimeout(window.resizedFinished);
        window.resizedFinished = setTimeout(function(){
            _facebook();
        }, 250);
    });
    
    0 讨论(0)
  • 2020-12-02 17:25

    I'm using the solution proposed by Robert Smith with max-width instead of width:

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

    Also, i'm using the Yugal Jindle's proposal and so i've

    data-width="555"
    

    and

    data-adapt-container-width="true"
    

    Now my page is ok! Thank you alot!

    0 讨论(0)
  • 2020-12-02 17:25

    I refined Twentyfortysix answer a bit, to only trigger the event after the resize is done.

    In addition I added check for the width of the window, so it won't trigger the reinitialisation on Android.

    (function(d, s, id) {
          var js, fjs = d.getElementsByTagName(s)[0];
          if (d.getElementById(id)) return;
          js = d.createElement(s); js.id = id;
          js.src = "//connect.facebook.net/en_EN/sdk.js#xfbml=1&version=v2.3";
          fjs.parentNode.insertBefore(js, fjs);
        }(document, 'script', 'facebook-jssdk'));
    
        jQuery(document).ready(function($) {
            var oldwidth = $(window).width();
            var timeout;
    
            var recalc = function() {
              clearTimeout(timeout);
              timeout = setTimeout(function() {
              var container_width = $('#fbcontainer').width();    
                $('#fbcontainer').html('<div class="fb-page" ' + 
                'data-href="YOUR FACEBOOK PAGE URL"' +
                ' data-width="' + container_width + '" data-height="750" data-adapt-container-width="true" data-hide-cover="false" data-show-facepile="true" data-show-posts="true"><div class="fb-xfbml-parse-ignore"><blockquote cite="YOUR FACEBOOK PAGE URL"><a href="YOUR FACEBOOK PAGE URL">YOUR FACEBOOK PAGE TITLE</a></blockquote></div></div>');
                if(typeof FB !== 'undefined') {
                    FB.XFBML.parse( );  
                }  
              }, 100);  
            };
    
            recalc();
    
          $(window).bind("resize", function(){  
            if(oldwidth !== $(window).width()) {
                recalc();
                oldwidth = $(window).width();
            }
            }); 
        });
    
    0 讨论(0)
提交回复
热议问题