0

假设我们有这个 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)

4

2 回答 2

2

演示

$(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");
        }
    });
}
于 2013-08-30T01:37:31.873 回答
0

我遵循了@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));
});
于 2013-08-29T18:08:49.763 回答