简体   繁体   English

包含动画选项时,jQuery回调不起作用(砌体插件)

[英]Jquery callback not working when animation options are included (masonry plugin)

I am implementing David DeSandro's JQuery Masonry plugin for a site i'm trying to build. 我正在为我要建立的网站实施David DeSandro的JQuery Masonry插件。 I am trying to run a callback on the masonry function so that i can scroll to the relevant part in the page after it runs but for some reason can't get it to work when I have the animation turned on. 我正在尝试在砌筑函数上运行回调,以便在它运行后可以滚动到页面中的相关部分,但由于某种原因,当我打开动画时无法使其正常工作。 The docs can be seen at http://desandro.com/demo/masonry/docs/#options . 可以在http://desandro.com/demo/masonry/docs/#options中查看该文档。 When I run the following code it works fine and the alert only happens once the masonry function has run: 当我运行以下代码时,它可以正常工作,并且仅在砌石功能运行后才发出警报:

$wall.masonry(
{
columnWidth: 216,
itemSelector: '.box:not(.invis)',
animate: false
},
function()
{
alert("Finished?");
}
);

However when i run the following code with the animation options included the alert runs before the animation has finished: 但是,当我运行以下包含动画选项的代码时,警报将在动画完成之前运行:

$wall.masonry(
{
columnWidth: 216,
itemSelector: '.box:not(.invis)',
animate: true,
animationOptions: {
  duration: speed,
  queue: false
}
},
function()
{
alert("Finished?");
}
);

I would really appreciate any pointers anyone can give me as to how to prevent the alert happening until the animation has completed as i'm stumped! 我真的很感激任何人都可以给我的关于如何防止警报发生的指针,直到我陷入困境时动画才完成! Thanks so much for your help, 非常感谢你的帮助,

Dave 戴夫

There's something you can do, but it to work in your sense requires a small hack: 您可以做一些事情,但要按照您的意愿进行工作,需要进行一些改动:

The object to animationOptions passed can take a complete property, which defines a function which will be fired after the animation is complete. 传递给animationOptions的对象可以采用complete属性,该属性定义一个函数,该函数将在动画完成后触发。

The problem is , this will be the case for each and every of your blocks, since every block is animated separately. 问题是 ,每个块都是这种情况,因为每个块都是单独设置动画的。 But you could get around this like so: 但是您可以这样解决:

var boxCount = $wall.find('box').size(),
    counter = 0,
    onComplete = function() {
        if (counter < boxCount) counter++;
        else {
            alert("Finished?"); // <-- Here goes your actual callback!
            counter = 0;
        }
    }

$wall.masonry({
    columnWidth: 216,
    itemSelector: '.box:not(.invis)',
    animate: true,
    animationOptions: {
        duration: speed,
        queue: false,
        complete: onComplete
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM