-
JS / nav anchor, active 효과 구현Web dev/JavaScript 2023. 2. 26. 15:46728x90반응형
스크롤에 따른 nav 엑티브효과 anchor 효과
코드펜에서 뺏겨 왔다. 키키
출처
https://codepen.io/eksch/pen/xwdOeK
<nav class="navigation" id="mainNav"> <a class="navigation__link" href="#1">Section 1</a> <a class="navigation__link" href="#2">Section 2</a> <a class="navigation__link" href="#3">Section 3</a> </nav> <div class="page-section hero" id="1"> <h1>Smooth scroll, fixed jump menu with active class</h1> </div> <div class="page-section" id="2"> <h1>Section Two</h1> </div> <div class="page-section" id="3"> <h1>Section Three</h1> </div>
$(document).ready(function() { $('a[href*=#]').bind('click', function(e) { e.preventDefault(); // prevent hard jump, the default behavior var target = $(this).attr("href"); // Set the target as variable // perform animated scrolling by getting top-position of target-element and set it as scroll target $('html, body').stop().animate({ scrollTop: $(target).offset().top }, 600, function() { location.hash = target; //attach the hash (#jumptarget) to the pageurl }); return false; }); }); $(window).scroll(function() { let scrollDistance = $(window).scrollTop(); $('.page-section').each(function(i) { if ($(this).position().top <= scrollDistance) { $('.navigation a.active').removeClass('active'); $('.navigation a').eq(i).addClass('active'); } }); }).scroll();
https://webclub.tistory.com/304
이건 또다른 참고소스
728x90반응형'Web dev > JavaScript' 카테고리의 다른 글
JS / 바닐라로 video play(), pause() 구현하기 (0) 2023.02.26 JS / 정규식으로 콤마찍기 (0) 2023.02.26 JS / input 정규식으로 숫자만 입력하기 (0) 2023.02.26 JS / selectBox custom하기 (0) 2022.10.30 JS / 가격콤마찍기 Price new Intl.NumberFormat() (0) 2022.05.07