我有一個網頁,我需要獲取我的頁面元素的當前頂部位置.
我有一些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的位置并使用它,當它變?yōu)榈扔?時,做某事(意味著當到達頂部時)和其他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
|