toggleClass odd behavior after infinite scroll and masonry load - jquery-masonry

Have a toggleClass on click that works fine for content loaded before infinite scroll and works fine on content appended to masonry after infinite scroll, but won't work any longer of previously loaded content.
For example//
Page Loads
<article></article>
<article></article>
<article></article>
(Toggle Class works fine on all above article elements)
but then add infinite scroll and appended masonry
load more article elements
<article></article>
<article></article>
<article></article>
(toggleClass works fine on the last loaded content, but not the article elements from page load.)
If done a third time,
<article></article>
<article></article>
<article></article>
(toggleClass works on above, and initial page loaded article elements, but not elements in the middle, which were the first elements loaded from infinite scroll)
I'm adding and removing an ID when element is clicked so ScrollTo works, and that seems to work fine all the time. So there is something off with the toggleClass().
Here is the code in the .js file.
jQuery(window).load( function() {
/**
Add ID to Genesis main.content
*/
jQuery('main.content').attr('id', 'grid');
/**
Adding div and class grid-sizer - utility element for Masonry to size correctly
*/
jQuery('main#grid article:first-of-type').before('<div class="grid-sizer"></div>');
/**
Initialize Masonry
*/
var $container = $('#grid');
$container.imagesLoaded(function(){
$container.masonry({
"itemSelector": "article",
"columnWidth": ".grid-sizer",
"animation": "true",
"isResizable": "true",
});
});
activeArticle();
});
function activeArticle() {
jQuery('article .entry-content a').click(function(event) {
event.preventDefault();
// Add id to clicked articles
jQuery(this).closest('article').attr('id','active');
// Add the giant class to resize element
jQuery(this).closest('article').toggleClass('giant');
jQuery('#grid').masonry();
jQuery('#grid').masonry('on','layoutComplete', function( msnryInstance, laidOutItems) {
jQuery('html, body').animate({
scrollTop: jQuery('#active').offset().top
}, 500);
jQuery('article').removeAttr('id');
});
});
}
Here is the code in the call back function for the WordPress Infinite Scroll Plugin.
var $newElems = jQuery(newElements).css({ opacity: 0 });
$newElems.imagesLoaded(function(){
$newElems.animate({ opacity: 1 });
jQuery('#grid').masonry( 'appended', $newElems, true );
});
activeArticle();

Ended up just writing another function and putting that in the call back. Here is the other function. This appears to work second, third, loads w/o issue.
function activeArticleInfinite() {
jQuery('#infscr-loading').nextAll('article').find('img').click(function(event) {
event.preventDefault();
// Add id to clicked articles
jQuery(this).closest('article').attr('id','active');
// Add the giant class to resize element
jQuery(this).closest('article').toggleClass('giant');
jQuery('#grid').masonry();
jQuery('#grid').masonry('on','layoutComplete', function( msnryInstance, laidOutItems) {
jQuery('html, body').animate({
scrollTop: jQuery('#active').offset().top
}, 500);
jQuery('article').removeAttr('id');
});
});
}

Related

Masonry - Scroll to clicked element on layout -infinite

I am trying to do 2 actions on one click on the element using Masonry library. It should work the way that when I click on the element it expands (I am adding class to have box bigger) and in the meantime the page is scrolling to that box. The problem is that when you expand the box it may drop a line down and that means the function called to scroll will scroll to wrong place. It should do the layout and get new position of the element and then move...
I got it almost working. It is expanding the box and doing new layout and when finished it scrolls the page to the box... but it looks a little bit strange and it first moves all the boxes for new layout, then stops and strars moving the page. I would prefer that to be done in one move. It that possible.
Here is my code:
$(document).ready(function() {
var $container = $('#container');
// initialize
var msnry = new Masonry( container, {
columnWidth: 280,
itemSelector: '.item'
} );
$('.openBoks').on('click', function() {
// this is my element
var thisBox = $(this).parent().parent();
// adding the class to expand it
thisBox.addClass('opened');
// calling method to do new layout
msnry.layout();
msnry.on( 'layoutComplete', function( msnryInstance, laidOutItems )
{
$('html, body').animate({
scrollTop: (thisBox.offset().top-10)
}, 700);
return true;
});
});
});
In case you or someone else is still looking for an answer to this, I did a little workaround this problem. The idea was basically the same, to do the scroll and the masonry repositioning simultaneously. It's not the most elegant solution since I added a couple of lines inside the masonry script.
Inside masonry.pkgd.js, there is a function _processLayoutQueue that positions the masonry items stored in the queue array, calling the _positionItem for each of the items.
Outlayer.prototype._processLayoutQueue = function( queue ) {
for ( var i=0, len = queue.length; i < len; i++ ) {
var obj = queue[i];
this._positionItem( obj.item, obj.x, obj.y, obj.isInstant );
}
};
What I did was check through the iteration for the item with the identifying masonry class, in your case ".opened". So when that item is found, I immediately activate the page scroll using a cointainer div ("#container_div_id" for this example) for the offset and adding the items "y" position (obj.y). At the end, the code looks something like this:
Outlayer.prototype._processLayoutQueue = function( queue ) {
for ( var i=0, len = queue.length; i < len; i++ ) {
var obj = queue[i];
this._positionItem( obj.item, obj.x, obj.y, obj.isInstant );
/* ADDED CODE */
if( $(obj.item.element).hasClass('opened')){
$('html, body').animate({
scrollTop: $('#container_div_id').offset().top+obj.y
}, 600);
}
/* END OF ADDED CODE */
}
};
This makes the scrolling start right after the positioning of the item, without having to wait for the layoutComplete event. As I said, its not the most elegant solution, but it is pretty solid, It works fine for my case.
Hope it helps someone!

Video.js, Infinite Scroll, and Masonry: Videos do not load on newly loaded content

I am using infinite-scroll.js, video.js, and masonry.js. The first page loads correctly (i.e., images of videos load within video players within "pintrest-style" boxes. However, as the user scrolls down, only the pintrest-style" boxes load with empty video players (i.e., no images and videos do not play). I think I should be reinitializing video.js somehow, but I can't figure out how to do so properly with multiple videos. My js code is below. How would I get the newly loaded boxes to load video players?
$container.infinitescroll({
debug: false,
//extraScrollPx: 40,
bufferPx: 40,
navSelector : '#navigation', // selector for the paged navigation
nextSelector : '#navigation a', // selector for the NEXT link (to page 2)
itemSelector : '.masonry_object', // selector for all items you'll retrieve
loading: {
finishedMsg: null,
img: "img/loader.gif",
msg: null,
msgText: " ",
speed: 'fast',
}
},
// trigger Masonry as a callback
function( newElements ) {
// hide new items while they are loading
var $newElems = $( newElements ).css({display:"none"},{ opacity: 0 });
// ensure that images load before adding to masonry layout
$newElems.imagesLoaded(function(){
// show elems now they're ready
$container.masonry( 'appended', $newElems, true );
$newElems.show().delay(1000).animate({ opacity: 1 },1000);
$newElems.videojs("example_video_number", { "controls": true, "autoplay": false, "preload": "auto" });
});
}
);

JQuery Masonry animation doesn't load from bottom

On my WordPress site the Masonry script is included in my functions.php and Infinite-Scroll is activated with the plugin. Looks like everything is working (although elements overlap sometimes).
But new appended elements are animated from top left, instead from the bottom.
This is the callback in WP admin:
function( newElements ) {
var $newElems = $( newElements ).css({ opacity: 0 });
$newElems.imagesLoaded(function(){
$newElems.animate({ opacity: 1 });
$container.masonry( 'appended', $newElems, true );
});
The last 'true' means 'isAnimatedFromBottom', not?
Demo site: http://goo.gl/9XVIl
There's a bug in masonry v2 that prevents the isAnimatedFromBottom parameter from working with CSS transitions.
You have a few options.
Switch to JS animation using the isAnimated: true option. But the animation may be slow.
Patch the code using the code in suggested in the issue.

jQuery Masonry and isotope - height

I need to show a hidden div that is attached to a .box div , my masonry code is
$(function(){
var $container = $('.content');
$container.imagesLoaded( function(){
$container.masonry({
itemSelector : '.box',
isAnimated : true,
isResizable : true,
});
});
});
Its loads a boxes with images and text , but when i want to show a specified DIV
Masonry want move in height , so my hidden div is showing behind a .box div
$('.commentopen').live("click",function()
{
var ID = $(this).attr("id");
$("#commentbox"+ID).fadeIn('slow');
return false;
});
Any ideas how to extend a height in the same window with out reloading
i tries masonry .reload or .reloadItems but with no luck , i think because of .imagesLoaded function
Here a visual explanation
All I can understand is that when a "hidden" div is shown, your Isotope layout does not react correctly (repositioning the other elements when the height grows). So, maybe this fiddle here is of some help. If you'd post a link to your sandbox or provide a jsfiddle yourself, it'd be easier to debug. You seem to also load large images via imagesLoaded, so if that's not correctly implemented, it could also play a role.

jQuery Masonry infinite scroll and picture overlap problems with my tumblr theme

I am new in programming(javascript) but I've done quite a research the past few days in order to make my tumblr theme work correctly. I know my question is common but as it seems I don't have enough knowledge to integrate correctly parts of code that were given in many similar examples.
My theme is supposed to override the "15 posts per page" limitation of tumblr and with an "endless scroll" option it should put all my posts (all of them pictures) in one endless page. Well, It doesn't. With a little help from here, I managed to wrap my {block:Posts} with the and with a couple of random changes in the masonry() call I ended up with this
As you can see my pictures are not overlapping (at last!) but after the 15 first posts it looks like a new page is created and the last pictures are not correctly aligned.
my jQuery masonry code is this:
<script type="text/javascript">
$(window).load(function () {
$('.autopagerize_page_element').masonry(),
$('.autopagerize_page_element').infinitescroll({
navSelector : "div.navigation",
// selector for the paged navigation (it will be hidden)
nextSelector : "div.navigation a#nextPage",
// selector for the NEXT link (to page 2)
itemSelector : ".autopagerize_page_element",
// selector for all items you'll retrieve
bufferPx : 10000,
extraScrollPx: 12000,
loadingImg : "http://b.imagehost.org/0548/Untitled-2.png",
loadingText : "<em></em>",
},
// call masonry as a callback.
function() { $('.autopagerize_page_element').masonry({ appendedContent: $(this) }); }
);
});
</script>
I know, its a mess...
Would really appreciate some help.
I'm not used to work with tumblr, but I can what is happening:
Line 110:
This script is creating a wrapper div around the entries each time you call to masonry, because of the script, each load looks like a new page, I think you can simply remove it.
Some tips:
You don't have to wait $(windows).load to execute masonry, change it by $(function()
To avoid image overlapping use appened masonry method and imagesLoad: Refer this
I see you're using masonry 1.0.1, be sure you're using masonry last version (2.1.06)
Example code:
$(function() {
//$('.autopagerize_page_element').masonry();
var $container = $('.autopagerize_page_element');
//wait until images are loaded
$container.imagesLoaded(function(){
$container.masonry({itemSelector: '.entry'});
});
$('.autopagerize_page_element').infinitescroll({
navSelector : "div.navigation",
// selector for the paged navigation (it will be hidden)
nextSelector : "div.navigation a#nextPage",
// selector for the NEXT link (to page 2)
itemSelector : ".entry",
// selector for all items you'll retrieve
bufferPx : 10000,
extraScrollPx: 12000,
loadingImg : "http://b.imagehost.org/0548/Untitled-2.png",
loadingText : "<em></em>",
},
// call masonry as a callback.
//function() { $('.autopagerize_page_element').masonry({ appendedContent: $(this) }); }
function( newElements ) {
// hide new items while they are loading
var $newElems = $( newElements ).css({ opacity: 0 });
// ensure that images load before adding to masonry layout
$newElems.imagesLoaded(function(){
// show elems now they're ready
$newElems.animate({ opacity: 1 });
$container.masonry( 'appended', $newElems, true );
});
}
);
});
and be sure to remove the last script in this header block:
<script type="text/javascript" src="http://static.tumblr.com/imovwvl/dJWl20ley/jqueryformasonry.js"></script>
<script type="text/javascript" src="jquery.masonry.min.js"></script> <!-- last masonry version -->
<script src="http://static.tumblr.com/df28qmy/SHUlh3i7s/jquery.infinitescroll.js"></script>
<!--<script src="http://static.tumblr.com/thpaaos/lLwkowcqm/jquery.masonry.js"></script>-->
Hope it helps

Resources