Discover the position of the Scroll of a certain object

8

Is it possible to know the position of the scroll of a given object through Jquery or pure Javascript? I tried var posobj = $("#meuobjeto_id").scrollTop(); However, the value obtained is always 0. Any idea? I want the user to go through a certain object (not clicking, or passing the mouse, but scrolling the page) the object is animated, for this I am wanting to use scrollTop()

    
asked by anonymous 19.11.2014 / 19:48

2 answers

8

By steps, what you need to do is:

  • have an event dropper that measures the current scroll amount at each moment of the scroll

  • compare the current scroll with the initial position of the object and act on a condition if

  • A example I did for another question :

    var posicaoInicial = $('#meuobjeto_id').position().top;
    $(document).scroll(function () { // oscultador de scroll
        var posicaoScroll = $(document).scrollTop(); // obtem a quantidade de scroll no momento
         if (posicaoInicial < posicaoScroll) $('#meuobjeto_id').animate({'opacity': 1}, 500);
    })
    

    The if (posicaoInicial < posicaoScroll) compares the values and runs the .animate() when the scroll is greater than the initial position of its element.

        
    19.11.2014 / 20:00
    3

    Use offset to find the position of the object

    $("#meuobjeto_id").offset().top
    

    And compare $(window).scrollTop() with the value obtained. You can do something like this

    $(window).scroll(function(){
        if ($("#meuobjeto_id").offset().top > $(window).scrollTop()){
            // animar
        }
    });
    
        
    19.11.2014 / 19:51