如何根据值数组检查当前窗口位置
How to check current window location against an array of values
假设我们有这个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));
});
相关文章:
- JavaScript窗口.位置不;我不在内容编辑器Web部件中工作
- JS窗口.位置不起作用
- 是否可以模拟 qUnit 测试的窗口位置对象
- 窗口位置href更改不会重新加载
- 如何在提交事件的底部设置窗口位置
- 如何在模式打开时更改窗口.位置
- 谷歌地图偏移标记/信息窗口位置(panTo/setCenter)
- 书签窗口位置javascript
- 单元测试窗口位置分配的角度问题
- JavaScript窗口位置延迟
- 如果类名和窗口位置匹配,请执行一些操作
- 会话丢失窗口.位置.href.
- JavaScript:window.conf,有两个窗口位置
- 如何从顶部获取窗口位置(以像素为单位)
- 如何在窗口位置之前显示消息 2 秒
- 休息标记位置 + 信息窗口位置
- 将标头添加到窗口.位置.路径名
- 窗口位置向上一级
- 将窗口位置替换为锚点
- 在谷歌地图中设置信息窗口位置