开发者

Using jQuery to make a group of images appear in order

I have a page that shows a bunch of thumbnails (around 30), and 开发者_开发知识库the client is wanting them to appear one by one, in order, going from left to right on the page. This is what I've tried:

var start_opacity = 500;
$j('.grid-photo').each(function(i) {
    start_opacity = start_opacity + 500;            

    setTimeout(function() {
        $j(i).animate({opacity: 1}, 4000);
    }, start_opacity);  
});

It doesn't seem to know what i is referencing. Any thoughts?


The .each() function passes the index and the element as the arguments of the function. It also calls the function within the context of the element (this points to your element)

You can save a quick variable var $photo = $j(this); inside your .each() and also, you can calculate your setTimeout delay by just taking 500*(i+1). Leaving us with:

$j('.grid-photo').each(function(i) {
    var $photo = $j(this);
    setTimeout(function() {
        $photo.animate({opacity: 1}, 4000);
    }, 500*(i+1));  
});


Here is a jquery plugin that you can use to achieve your purpose:


Looks like a scope issue.

Try:

$j('.grid-photo').each(function(i) {
    start_opacity = start_opacity + 500;            
    var thisImg = $(this);
    setTimeout(function() {
        thisImg.animate({opacity: 1}, 4000);
    }, start_opacity);
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜