layout / masonry - delayed layout adjustment - one click behind

为君一笑 提交于 2020-01-03 01:41:09

问题


Ok, so here is the problem https://jsfiddle.net/yq7f1a63/1/

Here is my code:

$(function () {
 $('.content').hide();
 $('a.read').click(function () {

     $(this).parent('.excerpt').hide();
     $(this).closest('li').find('.content').slideDown('fast');
     $('ul').masonry('layout');
     return false;
 });
 $('a.read-less').click(function () {

     $(this).parent('.content').slideUp('fast');
     $(this).closest('li').find('.excerpt').show();
     $('ul').masonry('layout');
     return false;       
 });
 });

 $('ul').masonry({
  itemSelector: 'li',
});

I have several masonry items with text inside. When clicking 'read more' it reveals more text (swaps .excerpt for .content).

I am using the layout method to adjust the grid when each item is clicked to reveal more, but for some reason it's one click behind. So for example I click read more on the first item and it goes wrong, but then I click read more for the next item and it adjusts to what the first item layout should have been, and so on!

Any ideas people?! Thanks in advance :-)


回答1:


You need to adjust the grid after slideDown() or slideUp() finishes. So, add that code to their complete callback functions.

$(function () {
     $('.content').hide();
     $('a.read').click(function () {

         $(this).parent('.excerpt').hide();
         $(this).closest('li').find('.content').slideDown('fast',function(){
            $('ul').masonry('layout');
         });         
         return false;
     });
     $('a.read-less').click(function () {

         $(this).parent('.content').slideUp('fast',function(){
            $(this).closest('li').find('.excerpt').show();
            $('ul').masonry('layout');
         });         
         return false;       
     });
 });

 $('ul').masonry({
  itemSelector: 'li',
});

JSFiddle: https://jsfiddle.net/yq7f1a63/2/



来源:https://stackoverflow.com/questions/32236261/layout-masonry-delayed-layout-adjustment-one-click-behind

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!