masonry icon indicating copy to clipboard operation
masonry copied to clipboard

How to call masonry reload onClick?

Open mmehrle opened this issue 12 years ago • 6 comments

Pretty simple question - I am using a masonry style layout and need to call upon masonry to refit everything after a 'read more' event which changes the height of one of the divs in my sidebar. This is what happens:

  1. I click on a 'read more' link which unhides some text in a div.
  2. The containing masonry-brick resizes to accommodate the new text.
  3. Unfortunately the bricks below do not push further down as their position and height has been set by masonry.
  4. When I slightly resize the window everything moves into the right place.

Therefore the easiest way to trigger this event would be by calling a masonry reload with that click. This is what I have in my page's header right now:

<script type='text/javascript'>
$(document).ready(function(){
  $("a.read_more").click(function(){
    alert("Clicked");
    $rootScope.$broadcast('masonry.reload');
    $('#content').masonry('reload');
  });
});
</script>

The clicked alert pops up, so I'm sure the function has been bound properly. However no reload of masonry is being triggered. Any help/pointers/suggestions would be very welcome.

Thanks in advance!! :-)

mmehrle avatar Dec 01 '14 18:12 mmehrle

You should be able to just trigger .masonry(), which triggers layout again.

var $content = $('#content');
$("a.read_more").click(function(){
  $rootScope.$broadcast('masonry.reload');
  $content.masonry();
});

I see you do not have Masonry initialization code. Are you initializing Masonry with HTML? A reduced test case would help. See Submitting Issues in the contributing guidelines.

desandro avatar Dec 01 '14 19:12 desandro

Hello Desando, thanks for the quick response - very much appreciated! :-)

I replaced the method with your suggestion but it's not firing, so probably I have not initialized Masonry in my header as I assumed my theme was already doing that. Forgive me but my jQuery and Masonry skills are still rather limited and I definitely don't want to waste your time. If you could perhaps point me at an example of how to add the proper init calls?

Thanks!

mmehrle avatar Dec 01 '14 20:12 mmehrle

See Initializaiton with JavaScript http://masonry.desandro.com/#javascript

desandro avatar Dec 02 '14 02:12 desandro

Follow up - I sorted it out. The first problem was that the component which was clicked is dynamically loaded and isn't part of the original DOM. The second problem was that all the masonry stuff was initialized and configured inside a jquery.custom.js file as part of the WP theme I'm using.

So I was able to intercept the click only by hooking into the dynamic JS routines. But those had no view of the masonry libs and configuration. In the end this is the solution that I wound up with:

$(document).click(function(event){
    $(window).trigger('resize');
}

This simple call was tacked on to the jQuery(document).ready(function($) block in my jquery.custom.js. I couldn't intercept the particular click but it's permissible to intercept all clicks within the document as it's a rare event. So yes, a bit of a workaround/hack but the best solution I could think of.

I just wanted to tack on my solution as it may help someone else one day.

mmehrle avatar Dec 02 '14 17:12 mmehrle

same problem happen with me

var $content = $('#block-views-ideas-block > .view-ideas > .view-content'); $("#wg-shw-mr").click(function(){ alert ("hello"); $rootScope.$broadcast('masonry.reload'); $content.masonry(); });

but this code is not work for me can us suggest. How i can solve this problem

thanks

sumitkumarSrijan avatar May 13 '15 06:05 sumitkumarSrijan

I have tried "element.masonry()" Its works fine on first call but after append new items with array push it does not working event after I trigger above code again. Please suggest me what should I do? Thanks

northoutIshan avatar Jul 31 '16 11:07 northoutIshan