(function () { /*===================================== Sticky ======================================= */ window.onscroll = function () { var header_navbar = document.querySelector(".navbar-area"); var sticky = header_navbar.offsetTop; if (window.pageYOffset > sticky) { header_navbar.classList.add("sticky"); } else { header_navbar.classList.remove("sticky"); } // show or hide the back-top-top button var backToTo = document.querySelector(".scroll-top"); if (document.body.scrollTop > 50 || document.documentElement.scrollTop > 50) { backToTo.style.display = "flex"; } else { backToTo.style.display = "none"; } }; // section menu active(仅处理页内锚点) function onScroll(event) { var sections = document.querySelectorAll('.page-scroll'); var scrollPos = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop; for (var i = 0; i < sections.length; i++) { var currLink = sections[i]; var val = currLink.getAttribute('href'); if (!val || val.charAt(0) !== '#') { continue; } var refElement = document.querySelector(val); if (!refElement) { continue; } var scrollTopMinus = scrollPos + 73; if (refElement.offsetTop <= scrollTopMinus && (refElement.offsetTop + refElement.offsetHeight > scrollTopMinus)) { sections.forEach(function (link) { link.classList.remove('active'); }); currLink.classList.add('active'); } else { currLink.classList.remove('active'); } } }; window.document.addEventListener('scroll', onScroll); // 仅页内锚点(#xxx)平滑滚动;/news、/ 等路由链接正常跳转 var pageLink = document.querySelectorAll('.page-scroll'); pageLink.forEach(elem => { elem.addEventListener('click', e => { var href = elem.getAttribute('href'); if (!href || href.charAt(0) !== '#') { return; } var target = document.querySelector(href); if (!target) { return; } e.preventDefault(); target.scrollIntoView({ behavior: 'smooth', block: 'start', }); }); }); "use strict"; }) ();