Javascript-форум (https://javascript.ru/forum/)
-   Библиотеки/Тулкиты/Фреймворки (https://javascript.ru/forum/library-toolkit-framework/)
-   -   Якоря в NavBar (https://javascript.ru/forum/library-toolkit-framework/72463-yakorya-v-navbar.html)

Frost56ru 30.01.2018 19:43

Якоря в NavBar
 
Всем привет, делаю NavBar на сайте, воткнул такой код:

var menu_selector = "#navbar-list-wrap"; // Переменная должна содержать название класса или идентификатора, обертки нашего меню. 
function onScroll(){
	var scroll_top = $(document).scrollTop();
	$(menu_selector + " a").each(function(){
		var hash = $(this).attr("href");
		var target = $(hash);
		if (target.position().top <= scroll_top && target.position().top + target.outerHeight() > scroll_top) {
			$(menu_selector + " a.active").removeClass("active");
			$(this).addClass("active");
		} else {
			$(this).removeClass("active");
		}
	});
}
$(document).ready(function () {
	$(document).on("scroll", onScroll);
	$('a[href*=\\#]:not([href=\\#])').click(function(e){
		e.preventDefault();
		$(document).off("scroll");
		$(menu_selector + " a.active").removeClass("active");
		$(this).addClass("active");
		var hash = $(this).attr("href");
		var target = $(hash);
		$("html, body").animate({
		    scrollTop: target.offset().top
		}, 800, function(){
			window.location.hash = hash;
			$(document).on("scroll", onScroll);
		});
	});
});



Ссылки с конкретными href типа #home ведет на блок id="home".

Проблема в том что все ссылки с решеткой стали мотать по страницы в рандомное место и возвращать обратно, как поправить?

Frost56ru 30.01.2018 19:50

Решил, выставил $('a[href*=\\.]:not([href=\\.])').click(function(e){

Cделал перемещение не по id а по классу

Frost56ru 30.01.2018 23:27

Сказали по классу нельзя, как быть?


Часовой пояс GMT +3, время: 06:18.