Jquery offset returns only one number when scrolling

I have a webpage and I need to get the current top position of my page elements.

I have a div, each of which has a height 100%, and I have my content.

I want to get this position of div (s) from above and work with it. but when I do this, it just returns a single number and doesn't change when scrolling.

This is my code:

HTML:

<header id="header">
   <div id="menu-div" class="container-full">
        <div class="container">
            <a href="#">TEST</a>
            <a href="#">TEST</a>
            <a href="#">Test</a>
            <a href="#">Test</a>
            <a href="#">Test</a>
            <a id="request-demo-a" class="pull-right" href="#">TEST demo</a>
            </div>
        </div>
   </header>


   <div id="first-div" class="center a-div">
   test content here...<br/>test content here...<br/>test content here...<br/>test content here...<br/>test content here...<br/>test content here...<br/>test content here...<br/>test content here...<br/>test content here...<br/>test content here...<br/>test content here...<br/>test content here...<br/>test content here...<br/>test content here...<br/>test content here...<br/>
   </div>


   <div id="full-div" class="container a-div">

        test content here...<br/>test content here...<br/>test content here...<br/>test content here...<br/>test content here...<br/>test content here...<br/>test content here...<br/>test content here...<br/>test content here...<br/>test content here...<br/>test content here...<br/>test content here...<br/>test content here...<br/>test content here...<br/>test content here...<br/>test content here...<br/>test content here...<br/>test content here...<br/>test content here...<br/>
        </div>

  <div id="other-div" class=" a-div">
  <br/>test content here...<br/>test content here...<br/>test content here...<br/><br/>test content here...<br/>test content here...<br/>test content here...<br/><br/>test content here...<br/>test content here...<br/>test content here...<br/><br/>test content here...<br/>test content here...<br/>test content here...<br/><br/>test content here...<br/>test content here...<br/>test content here...<br/><br/>test content here...<br/>test content here...<br/>test content here...<br/><br/>test content here...<br/>test content here...<br/>test content here...<br/>

  </div>



  <div id="other-div" class=" a-div">
  <br/>test content here...<br/>test content here...<br/>test content here...<br/><br/>test content here...<br/>test content here...<br/>test content here...<br/><br/>test content here...<br/>test content here...<br/>test content here...<br/><br/>test content here...<br/>test content here...<br/>test content here...<br/><br/>test content here...<br/>test content here...<br/>test content here...<br/><br/>test content here...<br/>test content here...<br/>test content here...<br/><br/>test content here...<br/>test content here...<br/>test content here...<br/>

  </div>

CSS

.a-div{
    height:100%;
}


#full-div{
    position:absolute;
    top:100%;
    width: 100%;
    height: auto;
    background: white;
    z-index:5;

}


#first-div{
    width:100%;
    height:100%;
    position:fixed;
    left:0;
    right:0;
    top:0;
    bottom: 0;
    margin: auto;
    color:white;
    z-index:-1;


}
#menu-div{
    background: white;
    padding:7px;
    position: fixed;
    z-index:10;
}


#menu-div .container a{
    margin-left: 15px;
    display:inline-block;
    font-size:large;
}

#request-demo-a{
    padding:5px;
    border: 2px solid #FF8000;
    color: white;
    text-shadow: none;
    background: orange;
    text-decoration: navajowhite;
}

#request-demo-a:hover, #request-demo-a:focus{
    background: #FF8000;
}

JavaScript (jQuery):

$(function(){
        /**start:Scroll*/
        $(document).scroll(function(){
         $("#request-demo-a").html($('#full-div').offset().top);
         /// I want this return current value of full-div!
         //And when it become equel 0 do something! (This is just example)

        });
        /**End: Scroll*/      
})

JSFiddle-Demo

I want to get the position #full-divand use it when it becomes 0, do something (which means when it arrived up), and for the other div (s) I want to do something in a certain position.

But why $('#full-div').offset().topjust returns a single number when scrolling?

+4
1

, offset() , , , .

, scrollTop, , , , , .

$(document).on('scroll', function(){

    var scrolled = $(window).scrollTop();
    var offset   = $('#full-div').offset().top;

    if ( offset <= scrolled ) {
       // element is scrolled to top or above
    }

});
+3

Source: https://habr.com/ru/post/1627193/


All Articles