I've been working on a responsive redesign and I'm trying to reduce the layout shift and flash of unstyled content that occurs when my AddThis
follow buttons are rendered on the page after being inserted by a Modernizr
query. Depending on the media query, the AddThis
code is either inserted into a div with the ID ss1
or ss2
. (See previous solved question.)
AddThis
support suggested I hide the div until the buttons are rendered. I'm a jQuery noob, but I dug around and think I have a partially working solution using .hide
and .show
.
Here's a relevant snippet from my main.js:
$('#ss1').hide();
$('#ss2').hide();
function initAddThis() {
addthis.init();
}
function checkMq() {
if (Modernizr.mq('only screen and (min-width: 1140px)')) {
$('div#ss1').html('<div class="addthis_toolbox">[snip]</div>');
$('div#ss1').append('<div style="clear:left">[google search box]</div>');
$('div#ss2').html(' ');
addthis.toolbox("#ss1");
$('#ss1').show();
} else {
$('div#ss2').html('<div class="addthis_toolbox">[snip]</div>');
$('div#ss2').append('<div style="clear:left">[google search box]</div>');
$('div#ss1').html(' ');
addthis.toolbox("#ss2");
$('#ss2').show();
}
}
$(function () {
initAddThis();
checkMq();
$(window).resize(function () {
checkMq();
});
});
Despite the fact that I first hide the ss1
and ss2
divs, I still get that
initial FOUC. When I resize the window back and forth it works as planned.
It's just the initial load that has the issue. I'm assuming that perhaps the time between the render call initAddThis();
and the jQuery .show
in checkMq();
is too short?