使用javascript在菜单上显示活动状态
我这里有一个持久的黄色菜单: 如你所见,它仍保留在页面上。我想要实现的是在用户滚动到该部分后,将活动阶段放在菜单项上(即黄色菜单位于当前部分的顶部) 似乎有很多不同的尝试方法,但我真的不知道如何开始。Bootstrap在这里(当您向下滚动时)使用一种称为scrollspy的东西来实现它,但是对于如此简单的东西来说,它的代码似乎太大了:使用javascript在菜单上显示活动状态,javascript,jquery,Javascript,Jquery,我这里有一个持久的黄色菜单: 如你所见,它仍保留在页面上。我想要实现的是在用户滚动到该部分后,将活动阶段放在菜单项上(即黄色菜单位于当前部分的顶部) 似乎有很多不同的尝试方法,但我真的不知道如何开始。Bootstrap在这里(当您向下滚动时)使用一种称为scrollspy的东西来实现它,但是对于如此简单的东西来说,它的代码似乎太大了: // Cache selectors var lastId, topMenu = $("#top-menu"), topMenuHeight =
// Cache selectors
var lastId,
topMenu = $("#top-menu"),
topMenuHeight = topMenu.outerHeight()+15,
// All list items
menuItems = topMenu.find("a"),
// Anchors corresponding to menu items
scrollItems = menuItems.map(function(){
var item = $($(this).attr("href"));
if (item.length) { return item; }
});
// Bind click handler to menu items
// so we can get a fancy scroll animation
menuItems.click(function(e){
var href = $(this).attr("href"),
offsetTop = href === "#" ? 0 : $(href).offset().top-topMenuHeight+1;
$('html, body').stop().animate({
scrollTop: offsetTop
}, 300);
e.preventDefault();
});
// Bind to scroll
$(window).scroll(function(){
// Get container scroll position
var fromTop = $(this).scrollTop()+topMenuHeight;
// Get id of current scroll item
var cur = scrollItems.map(function(){
if ($(this).offset().top < fromTop)
return this;
});
// Get the id of the current element
cur = cur[cur.length-1];
var id = cur && cur.length ? cur[0].id : "";
if (lastId !== id) {
lastId = id;
// Set/remove active class
menuItems
.parent().removeClass("active")
.end().filter("[href=#"+id+"]").parent().addClass("active");
}
});
//缓存选择器
var lastId,
topMenu=$(“#top menu”),
topMenuHeight=topMenu.outerHeight()+15,
//所有列表项
menuItems=topMenu.find(“a”),
//与菜单项相对应的锚定
scrollItems=menuItems.map(函数(){
var item=$($(this.attr(“href”));
if(item.length){return item;}
});
//将单击处理程序绑定到菜单项
//所以我们可以得到一个奇特的卷轴动画
菜单项。单击(函数(e){
var href=$(this.attr(“href”),
offsetTop=href==“#”?0:$(href).offset()。顶部菜单高度+1;
$('html,body').stop().animate({
滚动顶:偏置
}, 300);
e、 预防默认值();
});
//绑定到滚动
$(窗口)。滚动(函数(){
//获取容器滚动位置
var fromTop=$(this.scrollTop()+topMenuHeight;
//获取当前滚动项目的id
var cur=scrollItems.map(函数(){
if($(this).offset().top
好了
$(function () {
var $select = $('#select');
var $window = $(window);
var isFixed = false;
var init = $select.length ? $select.offset().top : 0;
$window.scroll(function () {
var currentScrollTop = $window.scrollTop();
if (currentScrollTop > init && isFixed === false) {
isFixed = true;
$select.css({
top: 0,
position: 'fixed'
});
$('body').css('padding-top', $select.height());
} else if (currentScrollTop <= init && isFixed === true) {
isFixed = false;
$select.css('position', 'relative');
$('body').css('padding-top', 0);
}
//active state in menu
$('.section').each(function(){
var eleDistance = $(this).offset().top;
if (currentScrollTop >= eleDistance) {
var makeActive = $(this).attr('id');
$('#select a').removeClass('active');
$('#select a.' + makeActive).addClass('active');
}
});
});
$(".nav").click(function (e) {
e.preventDefault();
var divId = $(this).attr('href');
$('body').animate({
scrollTop: $(divId).offset().top - $select.height()
}, 500);
});
});
$(函数(){
变量$select=$('#select');
变量$window=$(window);
var isFixed=假;
var init=$select.length?$select.offset()。顶部:0;
$window.scroll(函数(){
var currentScrollTop=$window.scrollTop();
if(currentScrollTop>init&&isFixed==false){
isFixed=true;
$select.css({
排名:0,
位置:'固定'
});
$('body').css('padding-top',$select.height());
}else if(currentScrollTop=eleDistance){
var makeActive=$(this.attr('id');
$(“#选择一个”).removeClass('active');
$('#选择一个.+makeActive.addClass('active');
}
});
});
$(“.nav”)。单击(函数(e){
e、 预防默认值();
var divId=$(this.attr('href');
$('body')。设置动画({
scrollTop:$(divId).offset().top-$select.height()
}, 500);
});
});
这并不是一项简单的任务,不仅需要根据滚动位置设置菜单突出显示,还需要与锚定相关。另一方面,你也可以点击菜单跳转到你想要的位置。这个jQuery插件可以满足您的需求:如果是我在您的位置上。我会从这个开始:,并删除任何我不喜欢的部分。例如数据api。