简体   繁体   English

如何在该动画函数的回调中使用由动画函数创建的属性?

[英]How do I use the properties created by an animate function in that animate function's callback?

I'm working on a page that will have a series of images on it. 我正在处理一个包含一系列图像的页面。 Each image will be in a container, and each container will be floated left. 每个图像将在一个容器中,并且每个容器将向左浮动。 Since the images will have varying widths, there's no way of knowing how many images will be on each row. 由于图像的宽度会变化,因此无法知道每行上有多少图像。 When a visitor clicks on an image, it and it's container will expand. 当访客点击图片时,图片及其容器就会展开。

In some situations, this expansion means the clicked image will jump to the following row, and will leave the screen because of it. 在某些情况下,这种扩展意味着单击的图像将跳至下一行,并因此而离开屏幕。 I'm looking for a way of having the screen follow the clicked image wherever it goes. 我正在寻找一种使屏幕无论走到哪里都跟随点击的图像的方法。

Here's my code thus far: 到目前为止,这是我的代码:

<script type="text/javascript">
$(document).ready(function(){
    $(".ExampleImage").toggle(
        function(){
            var Image = $(this);
            var Container = $(this).closest(".ImageContainer");
            Image.switchClass("ExampleImageContracted","ExampleImageExpanded",500);
            Container.css('height','auto');
            Image.queue(scrollToExample(Image));
        },
        function(){
            var Image = $(this);
            var Container = $(this).closest(".ImageContainer");
            Image.switchClass("ExampleImageExpanded","ExampleImageContracted",500);
            Container.animate({'height':'250'},500,scrollToExample(Image));
        }
    );

    function scrollToExample(Image) {
        var NewTop = $(Image).closest(".Example").offset();         
        $('html, body').animate( {
            scrollTop:NewTop.top
        }, 1000);   
    }
});
</script>

Note that I've used both a callback in the .animate and also a .queue in an attempt to sequence this correctly. 请注意,我在.animate.queue中都使用了回调,以尝试正确地对其进行排序。 Unfortunately, the value this pulls in for the image container's .offset is the old top, and not the one that occurs after the animation. 不幸的是,它为图像容器的.offset输入的.offset旧的顶部,而不是在动画之后出现的值。 I'm looking for a way of getting the container's top once the animation has completed. 我正在寻找一种在动画完成后获取容器顶部的方法。 Any advice would be very appreciated. 任何建议将不胜感激。

If you'd like to see the above code in action, here's a link. 如果您想查看上面的代码,请访问以下链接。 Note that when you arrive at the page and click Example 4, the screen does not scroll down to Example 4's new location. 请注意,当您到达页面并单击示例4时,屏幕不会向下滚动到示例4的新位置。

http://notiondigitalarts.com/ImageEnlargeTest/ http://notiondigitalarts.com/ImageEnlargeTest/

Try changing these two lines: 尝试更改这两行:

Image.queue(scrollToExample(Image));
...
Container.animate({'height':'250'},500,scrollToExample(Image));

To: 至:

Image.queue(function() { scrollToExample(Image); });
...
Container.animate({'height':'250'},500,function(){scrollToExample(Image);});

The way you had it you were't passing a callback function to .queue() and .animate() , you were calling your scrollToExample() function immediately and passing its result to .queue() and .animate() . 您没有将回调函数传递给.queue().animate() ,而是立即调用scrollToExample()函数并将其结果传递给.queue().animate()

Having said that, I don't think you need to use .queue() at all here. 话虽如此,我认为您根本不需要在此使用.queue()

Note also that within your scrollToExample() function you can say Image.closest(... rather than $(Image).closest(... because Image is already a jQuery object. (Unless you want your function to be able to handle other types of inputs.) 还请注意,在scrollToExample()函数中,您可以说Image.closest(...而不是$(Image).closest(...因为Image已经是jQuery对象。(除非您希望函数能够处理)其他类型的输入。)

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

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