Is it possible to set a fluid width for Facebook's social plugins?

后端 未结 29 1506
粉色の甜心
粉色の甜心 2020-12-22 18:52

I\'m developing a site around the \"responsive design\" concept, but the facebook social plugins are static width and \"break\" the layout when it\'s re-sized.

Using

29条回答
  •  臣服心动
    2020-12-22 18:59

    Here is what I ended up with:

    (function() {
        window.addEventListener('load', updateWidth);
        window.addEventListener('resize', debounce(function () {
            updateWidth();
            if (window.FB && FB.XFBML && FB.XFBML.parse) {
                FB.XFBML.parse();
            }
        }, 1000));
    
        function updateWidth() {
            $('.fb-like, .fb-comments').each(function () {
                var el = $(this);
                var width = el.parent().width();
                el.attr('data-width', width);
            })
        }
    
        function debounce(func, wait, immediate) {
            var timeout;
            return function() {
                var context = this, args = arguments;
                var later = function() {
                    timeout = null;
                    if (!immediate) func.apply(context, args);
                };
                var callNow = immediate && !timeout;
                clearTimeout(timeout);
                timeout = setTimeout(later, wait);
                if (callNow) func.apply(context, args);
            };
        }
    })();
    

提交回复
热议问题