大家下午好,如何突出顯示滾動中的活動菜單項並單擊?
我想添加一個類active
到當前的滾動菜單項並點擊。這是一個包含不同部分的單頁(長)頁面。當點擊一個項目時,活動狀態應該切換到當前狀態。
我想出了下面的代碼,但沒有現在怎麼我可以整合上面:
// Click event
$('#primary-navwrapper li a[href^="#"]').click(function(event) {
// Prevent from default action to intitiate
event.preventDefault();
// The id of the section we want to go to
var anchorId = $(this).attr('href');
// Our scroll target : the top position of the section that has the id referenced by our href
var target = $(anchorId).offset().top - offset;
console.log(target);
$('html, body').stop().animate({ scrollTop: target }, 500, function() {
window.location.hash = anchorId;
});
return false;
});
希望有人有一個例子或一些好的建議如何做到這一點。