【发布时间】:2018-10-12 06:05:49
【问题描述】:
我有这个导航滚动到部分的代码。
var lastId;
var topMenu = $(".community-nav");
var topMenuHeight = topMenu.outerHeight() - 19;
if(window.matchMedia("(max-width: 768px)").matches){
var logoHeight = $(".community-logo").outerHeight();
var topMenuHeight = topMenu.outerHeight() - logoHeight;
};
menuItems = topMenu.find('.community-links li > div'),
scrollItems = $('.nav-section')
menuItems.click(function(e){
var href = $(this).attr("data-target");
var currentItem = $(this);
var sectionClass = $("#community-intranav");
scrollPartialMenuItem(currentItem, sectionClass);
var offsetTop = href === "#" ? 0 : $(href).offset().top-topMenuHeight + 20;
if(window.matchMedia("(max-width: 768px)").matches){
};
$('html, body').stop().animate({
scrollTop: offsetTop
}, 850);
e.preventDefault();
});
function getCurrentSection(scrollPosition) {
return scrollItems.toArray().findIndex(function(item) {
return item.offsetTop < scrollPosition && item.offsetTop+item.clientHeight > scrollPosition;
});
}
// Bind to scroll
$(window).on("load scroll",function(e){
var scrollPosition = $(this).scrollTop()+topMenuHeight;
var currentSection = getCurrentSection(scrollPosition) ;
var id = currentSection > -1 ? scrollItems[currentSection].id : "";
if (id && lastId !== id) {
lastId = id;
menuItems.removeClass("active");
menuItems.filter("[data-target='#"+id+"']").addClass('active');
}
});
在上面的代码中,IE 支持 findIndex 方法。我想要一个替代方法,以便我可以使用 findIndex 方法以外的方法重写我的函数 getCurrentSection。请帮助。
【问题讨论】:
-
你知道 findIndex 是做什么的吗?它是如何工作的?
-
你的代码不是已经在使用
findIndex了吗?你可以详细说明这个问题。标题说你想为 IE 找一个 findIndex 替代品,然后文字说 IE 支持 findIndex,你想要别的东西。然后在评论中你说你想要findIndex。请清理这个烂摊子。 -
是的。但是它不支持IE,这就是为什么我想重写它以支持IE。
-
链接的 MDN 页面显示了如何让 IE 支持 findIndex ...
标签: javascript jquery navigation sticky