I have a page layout that needs to reduce from a 3 column layout to a 2 column layout depending on the browser size. I am also using the jQuery masonry plugin to keep my grid of containers on the page arranged nicely. This all works fine on window resize.
The problem I am having is that if I load the page at the size smaller than 1100px it correctly calls the ‘col2.css’ stylesheet using jQuery, but the masonry plugin thinks it’s still a 3 column layout until you manually resize the browser so the grid is still at the col3 width and goes off the size of the container.
I’m guessing I need to somehow call the masonry function after jQuery has chosen the stylesheet, but I don’t know how to do this.
I have this in my head
<link href="<?=base_url()?>inc/css/col3.css" id="size-stylesheet" rel="stylesheet" type="text/css" />
and the following javascript
$(document).ready(function() {
function adjustStyle(width) {
width = parseInt(width);
if (width < 1100) {
$("#size-stylesheet").attr("href", "inc/css/col2.css");
} else {
$("#size-stylesheet").attr("href", "inc/css/col3.css");
}
}
$(function() {
adjustStyle($(this).width());
$(window).resize(function() {
adjustStyle($(this).width());
});
});
//masonry
$(function(){
$('#masonry').masonry({
itemSelector : '.project',
columnWidth : 365,
isAnimated: true
});
});
});
Any help would be greatly appreciated!
First, based on the description. I think you are better seeking for the responsive design approach, this is a good article: http://www.alistapart.com/articles/responsive-web-design/. this my solve your problem, without switching Css manally.
Second, a possible problem is that the masonry is applied when the col3.css is loaded, before switching to col2.css. Therefore, you have to load the col2.css as early as possible, this may be done by moving the related code out of
document.readyfunction.Third, if this does not solve the problem, you can always reload the masonry at some point to reorder the elements: