Compare top distances of an element with the following next()

Asked

Viewed 540 times

1

I’m getting the following error on the console:

Uncaught Typeerror: Cannot read Property 'top' of Undefined

He is saying that the error occurs on the following line:

alturaproximo = $(this).next().offset().top;

Follows the code:

$('.lego').each(function() {
    var alturadesse = $(this).offset().top,
        alturaproximo = $(this).next().offset().top;
        console.log(alturadesse);

    var zindex = 5;
    if ( alturadesse == alturaproximo ) {
        $(this).css({'z-index': zindex});
    } if ( alturadesse > alturaproximo ) {
        zindex + 1;
        $(this).css({'z-index': zindex});
    }
});

I believe he’s not getting the height offset().top because of the next().
Would anyone know what I need to do to compare the distances from the top of an element and the next element equal to it?

  • It’s not at the end when you get to the last and there’s no next?

  • Man, that was it!! Hahaha. Thank you so much!

1 answer

1

As answered in the commentary, the problem was when it reached the last element and there was no next .next() causing the error.

Please edit this answer and include the corrected code here, for the question to leave the unanswered list...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.