如何根据值数组检查当前窗口位置

How to check current window location against an array of values

本文关键字:窗口 位置 检查 数组 何根      更新时间:2023-09-26

假设我们有这个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中距页面顶部的元素高度。

Im使用.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}, ... ]

现在我需要检查滚动当前窗口。scrollTop()是否等于任何数据位置值,所以:

$(window).scroll(function() {
    var hFromTop = $(window).scrollTop()
    //cant figure out what should i do next (jquery allowed)
});

问题:在用户滚动时,我如何对照多个值检查当前window.scrolltop(),以便在window.scroll top等于值时发生一些事情

(我使用这个数组是因为稍后我将读取.locationhash的id,以便使用位置替换将其附加到url)

DEMO

$(document).ready(function () {
    $(document).on("scroll", onScroll);
    //smoothscroll
    $('a[href^="#"]').on('click', function (e) {
        e.preventDefault();
        $(document).off("scroll");
        $('a').each(function () {
            $(this).removeClass('active');
        })
        $(this).addClass('active');
        var target = this.hash,
            menu = target;
        $target = $(target);
        $('html, body').stop().animate({
            'scrollTop': $target.offset().top+2
        }, 500, 'swing', function () {
            window.location.hash = target;
            $(document).on("scroll", onScroll);
        });
    });
});
function onScroll(event){
    var scrollPos = $(document).scrollTop();
    $('#menu-center a').each(function () {
        var currLink = $(this);
        var refElement = $(currLink.attr("href"));
        if (refElement.position().top <= scrollPos && refElement.position().top + refElement.height() > scrollPos) {
            $('#menu-center ul li a').removeClass("active");
            currLink.addClass("active");
        }
        else{
            currLink.removeClass("active");
        }
    });
}

我遵循@tushar gupta的建议,还将其封装在一个函数中,检查滚动是否结束

$(window).scroll(function() {
clearTimeout($.data(this, 'scrollTimer'));
$.data(this, 'scrollTimer', setTimeout(function() {
  var hFromTop = $(window).scrollTop();
    $.each(pageSectionsArr, function(obj) {
      console.log(this + 'is sparta')
      if (~~this.pageSectionPos <= hFromTop && ~~this.pageSectionPos + this.pageSection.offsetHeight > hFromTop) {
        //console.log(this.pageSectionPos)
        var sectionsId = this.pageSection.id
        //console.log(sectionsId)
        window.location.replace('#/'+sectionsId)
      };
    });
}, 250));
});