티스토리 script.js GNB 오류 수정

카테고리 IT/티스토리

무찌마, 댓글

초고 2020. 11. 18.

※ 2020.11.27: 블로그 반영

※ 2024.03.26: 아래 링크한 알릭2님의 블로그에 원본 소스를 수정하는 자세한 이유와 설명이 기재되어 있습니다.

 

북클럽 스킨 - GNB 메뉴 현위치 버그 (tistory.com)

 

원본 (오류)

$(window).load(function(){
	$gnb.find("li").each(function(){
		gnbWidth  = gnbWidth + $(this).outerWidth() + 1;
		if ( window.location.pathname.indexOf($(this).find("a").attr("href")) != -1 ){
			$(this).addClass("current");
		}
	});
	$gnb.find("ul").width(gnbWidth);

	if ( $gnb.width() < $gnb.find("ul").width() && $gnb.find(".current").length ){
		var scrollPos = $gnb.find(".current").prev().length ? $gnb.find(".current").prev().position().left : $gnb.find(".current").position().left;
		$gnb.scrollLeft( scrollPos );
	}
});

 

수정

$gnb.find("li").each(function(){
	var href = $(this).find('a').attr('href');
	if (href == window.location.pathname) {
		$(this).addClass("current"); return false;
	}
	if (href.indexOf(window.location.pathname) > -1 ){
		$(this).addClass("current"); return false;
	}
});
	
$(window).on("load", function(){
	$gnb.find("li").each(function(){
		gnbWidth  = gnbWidth + $(this).outerWidth() + 1;
	});
	$gnb.find("ul").width(gnbWidth);
	if ( $gnb.width() < $gnb.find("ul").width() && $gnb.find(".current").length ){
		var scrollPos = $gnb.find(".current").prev().length ? 
        $gnb.find(".current").prev().position().left : 
        $gnb.find(".current").position().left;
		$gnb.scrollLeft( scrollPos );
	}
});

댓글