假设我们有这个 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 top 等于某个值时发生某些事情
(我正在使用这个数组,因为稍后我会读取.locationhashid 以便使用位置替换将其附加到 url)