优化企业网站功能
This commit is contained in:
@@ -24,7 +24,7 @@
|
||||
}
|
||||
};
|
||||
|
||||
// section menu active
|
||||
// section menu active(仅处理页内锚点)
|
||||
function onScroll(event) {
|
||||
var sections = document.querySelectorAll('.page-scroll');
|
||||
var scrollPos = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
|
||||
@@ -32,10 +32,16 @@
|
||||
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)) {
|
||||
document.querySelector('.page-scroll').classList.remove('active');
|
||||
sections.forEach(function (link) { link.classList.remove('active'); });
|
||||
currLink.classList.add('active');
|
||||
} else {
|
||||
currLink.classList.remove('active');
|
||||
@@ -45,15 +51,23 @@
|
||||
|
||||
window.document.addEventListener('scroll', onScroll);
|
||||
|
||||
// for menu scroll
|
||||
// 仅页内锚点(#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();
|
||||
document.querySelector(elem.getAttribute('href')).scrollIntoView({
|
||||
target.scrollIntoView({
|
||||
behavior: 'smooth',
|
||||
offsetTop: 1 - 60,
|
||||
block: 'start',
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user