分享

javascript – Jquery偏移在滚动时只返回一个数字

 印度阿三17 2019-06-28

我有一个网页,我需要获取我的页面元素的当前顶部位置.

我有一些div,他们每个都有100%的高度,我有我的内容.

我希望从顶部获得这些div的位置并使用它.但是当我这样做时,它只返回一个数字,并且在滚动时不会改变.

这是我的代码:

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

我想获得#full-div的位置并使用它,当它变为等于0时,做某事(意味着当到达顶部时)和其他div(s)我想在某个位置做某事.

但为什么$(‘#full-div’).offset().top只是在滚动时返回一个数字?

解决方法:

偏移量在滚动时不会改变,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
    }

});
来源:https://www./content-1-275551.html

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多