Get position of an element

Asked

Viewed 836 times

0

I have for example the following structure

<div style="height: 1500px;">
    <div id="dv1" style="height: 600px">
       conteudo 1
    </div>

    <div id="dv2" style="height: 300px">
       conteudo 2
    </div>

    <div id="dv3" style="height: 300px">
       conteudo 3
    </div>

    <div id="dv4" style="height: 300px">
       conteudo 4
    </div>    
</div>

And always while scrolling, I would like to get the current position of the div "content 3"

I tried to get by clicking on some other div and nothing, by scrolling the same scroll I would like to do and nothing tbm.

var a = $('#dv2').scrollTop();
alert(a);
  • @Marconi no friend, I wanted to get his position in relation to the scroll. For example with this code: var div = $('#general-menu'); $(window). scroll(Function() { if ($(this).scrollTop() > 35) { div.removeClass("general-menu-normal"); div.addClass("general-menu-Fixed"); } Else { div.removeClass("general-menu-Fixed"); div.addClass("general-menu-normal"); } }); I can check if the scroll is larger q 35 to set the fixed menu, in this same footprint, I wanted to pick up the position of the div

2 answers

2


To take the position of an element relative to the top of the window, you need to make the following relation:

distance from the element to the top of the document - scrolled distance from the page

$(window).on("scroll", function(){
    console.log($("#dv3").offset().top - $(window).scrollTop());
});
  • Wow, that’s what I wanted :D Thank you

0

You can find the element coordinates in relation to window current... the function below returns the "coordinates" in pixels where x represents the horizontal line (based on left measures) and y the vertical line (based on top).

Note that the element must be {DOM DocumentElement Node}

        /**
         * Get element position on window
         * @param el {DOM DocumentElement Node}
         * @return {number|float} cardinal x,y relative element position
         */
         function getPosition(el){
            // default positions
            var xPos = 0,
                yPos = 0;
            // loop
            while (el) {
                if ( el.tagName === "BODY" ){
                    // deal with browser quirks with body/window/document and page scroll
                    var xScroll = el.scrollLeft || document.documentElement.scrollLeft,
                        yScroll = el.scrollTop  || document.documentElement.scrollTop;
                    xPos += ( el.offsetLeft - xScroll + el.clientLeft );
                    yPos += ( el.offsetTop  - yScroll + el.clientTop );
                } else {
                   // for all other non-BODY elements
                   xPos += ( el.offsetLeft - el.scrollLeft + el.clientLeft );
                   yPos += ( el.offsetTop  - el.scrollTop  + el.clientTop );
                }
                el = el.offsetParent;
            }
            return {
               x: xPos,
               y: yPos
            };
        };
        
        window.addEventListener('scroll', function(){
            var el = document.getElementById('dv3');
            console.log(getPosition(el));
        }, false);
<div style="height: 1500px;">
    <div id="dv1" style="height: 600px">
       conteudo 1
    </div>

    <div id="dv2" style="height: 300px">
       conteudo 2
    </div>

    <div id="dv3" style="height: 300px">
       conteudo 3
    </div>

    <div id="dv4" style="height: 300px">
       conteudo 4
    </div>    
</div>







<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

  • That’s right man, thank you. What would {Domdocumentelementnode}?

  • Fault mine, this definition does not exist. The correct one would be to add the spaces: {DOM DocumentElement Node} or simply, a "node" of the "document" handled by the DOM API. References: GIFT, Documentelement, Node

Browser other questions tagged

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