I'm using the jQuery isotope plugin for a grid layout of a news section, each news item showing the first paragraph of each with a read more
button, once clicked showing the rest of the content.
Here's my example and I'll explain the problem I'm having:
jsFiddle: http://jsfiddle.net/neal_fletcher/gXukc/4/
jQuery:
$(document).ready(function () {
var $container = $('#main-grid');
$container.imagesLoaded(function () {
$container.isotope({
itemSelector: '.grid-block, .grid-block-long',
animationEngine: 'best-available',
masonry: {
columnWidth: 5
}
});
});
});
$(document).ready(function () {
$('.grid-block-text p').hide().filter(":first-child").show();
});
$(document).ready(function () {
$('.read-more').click(function () {
$(this).hide();
$(this).nextAll('.grid-block-text')
.children('p').fadeIn('slow');
$(this).siblings('.read-less').fadeIn('slow');
$(this).parent('.grid-block')
.removeClass('grid-block')
.addClass('grid-block-long');
$('#main-grid').isotope('reLayout');
$container.isotope();
});
$('.read-less').click(function () {
$(this).hide();
$(this).nextAll('.grid-block-text')
.children("p:not(:first-child)").fadeOut('fast');
$(this).siblings('.read-more').fadeIn('slow');
$(this).parent('.grid-block-long')
.removeClass('grid-block-long')
.addClass('grid-block');
$('#main-grid').isotope('reLayout');
$container.isotope();
});
});
As you can see in the example shown, clicking on the read more
button loads the rest of the text content and switches the class of the div to allow the text to fit. The reason I've done this is I always want the height of the divs to fit into the grid, i.e. as in the example, the length of the longer div is equal to 2 of the shorter ones, thus always lining up.
But as this is going to be a news section, I can't negate for how much text is going to be there, so I'm trying to figure out if this can be done dynamically.
e.g. grid-block
is 300px in height, if the text content is longer than 300px, it will then switch to 630px and so on and so on.
Any suggestions would be greatly appreciated!