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

1

I'm getting the following error in the console:

  

Uncaught TypeError: Can not read property 'top' of undefined

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

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

Follow 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 think he can not get the height offset().top because of next() . Would anyone know what I need to do to compare the top distances of an element and the next element equal to it?

    
asked by anonymous 18.08.2014 / 18:36

1 answer

1

As stated in the comment, the problem was when it arrived in the last element and there was the next .next() causing the error.

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

    
18.08.2014 / 21:29