简体   繁体   中英

How to position each element with a class identifier differently using jQuery?

I'm pulling in two RSS feeds using simple pie and then adding a .pin for each feed post using a PHP foreach loop. I would like to position them so that each one appears 30px lower than the previous one. Here's my current attempt:

$( document ).ready(function() {
        for (x = 0; x < i; x++) {
            var position = 0;
            $('.pin').eq( x ).css('top', position + 'px');
            position = position + 30;
        };
    });

This isn't working, they all just appear on top of each other at the top of the page.

Any help would be great, thanks in advance!

The position is the same for each iteration of the loop and your assignment order is the wrong way around, finally i is nowhere to be found.

I think you might be looking for this. Considering i is valid.

        var position = 30 * x;
        $('.pin').eq( x ).css('top', position + 'px');

Position value is zero for all iteration. Move that line before the loop starts

Do away with the i and for . Since you are using jQuery, take advantage of .each :

var position = 0;
$(".pin").each(function () {
    $(this).css('top', position + 'px');
    position += 30;
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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