【问题标题】:Jquery offset return just one number while scrolling滚动时 Jquery 偏移量只返回一个数字
【发布时间】:2016-05-13 02:37:41
【问题描述】:

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

我有一些 div,每个 div 的高度为 100%,我的内容在那里。

我想从顶部获取这些 div(s) 位置并使用它。但是当我这样做时,它只返回一个数字,并且在滚动时不会改变。

这是我的代码:

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,我想在某个位置做一些事情。

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

【问题讨论】:

    标签: javascript jquery html css twitter-bootstrap-3


    【解决方案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
        }
    
    });
    

    【讨论】:

    • 所以,你的意思是我应该使用on
    • @Askion - 不,我只使用了on,因为它是附加事件处理程序的首选方式,它与答案无关
    • 谢谢。另一个问题,我怎么知道,当 full-div 到达(类似)标题时,做点什么!?
    • 取决于什么是“标题”?
    • 好吧,所有元素都在同一个文档中,我真的不明白你是如何将#full-div 滚动到header,它们永远不会移动?
    猜你喜欢
    • 2020-08-16
    • 1970-01-01
    • 2021-11-28
    • 1970-01-01
    • 2015-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-07
    相关资源
    最近更新 更多