數組比方說,我們已經有了這個HTML:如何檢查當前窗口的位置對價值
<div class="locationhash" data-position="0"></div>
<div class="locationhash" data-position="1000"></div>
<div class="locationhash" data-position="3000"></div>
<div class="locationhash" data-position="5000"></div>
<div class="locationhash" data-position="8000"></div>
其中data-positions
值,在正常條件下將等於元素的高度從頁面頂部的px
。
林檢索元件與.locationhash
,讀取其數據位置,並且使用該碼推動兩個值作爲對象到一個數組。 (這裏按預期工作,第三個代碼示例中的輸出)
var getSections = function() {
var pageSectionsArr = [];
var pageSections = (document).querySelectorAll('.locationhash');
Array.prototype.forEach.call(pageSections, function(section) {
var getSectionPos = section.getAttribute('data-position');
var pageSectionObj = {
pageSection : section,
pageSectionPos : getSectionPos
};
pageSectionsArr.push(pageSectionObj);
});
console.log(pageSectionsArr)
};
(function recalcSectionPos() {
$(window).resize(function() {
getSections();
});
})();
輸出看起來像這樣:
[ {div.locationhash, value of its data-position},
{div.locationhash, value of its data-position}, ... ]
現在我需要檢查滾動閹當前window.scrollTop()等於任何的數據位置值,因此:
$(window).scroll(function() {
var hFromTop = $(window).scrollTop()
//cant figure out what should i do next (jquery allowed)
});
問:我怎麼能檢查當前window.scrolltop( )對多個值,而用戶在滾動,所以我可以做的時候window.scroll頂部等於值
(我使用這個數組,因爲以後我也會爲了將其追加閱讀.locationhash
的ID事情發生URL使用位置替換)
這將幫助你http://jsfiddle.net/cse_tushar/Dxtyu/141/ –
太感謝你了!你能添加此作爲一個答案,所以你可以得到當之無愧的+點? – mymlyn