|
|
var dContents = document.querySelector('d-contents'); |
|
|
var dArticle = document.querySelector('d-article'); |
|
|
|
|
|
var computedStyle = window.getComputedStyle(dContents); |
|
|
|
|
|
var marginTop = parseInt(computedStyle.marginTop, 10); |
|
|
|
|
|
var originalOffsetTop = dContents.offsetTop; |
|
|
var originalOffsetLeft = dContents.offsetLeft; |
|
|
var originalWidth = dContents.offsetWidth; |
|
|
|
|
|
|
|
|
function onResize() { |
|
|
|
|
|
originalOffsetLeft = dContents.offsetLeft; |
|
|
originalWidth = dContents.offsetWidth; |
|
|
} |
|
|
|
|
|
|
|
|
window.addEventListener('resize', onResize); |
|
|
|
|
|
window.addEventListener('scroll', function() { |
|
|
if (window.innerWidth > 900) { |
|
|
var scrollPosition = window.pageYOffset || document.documentElement.scrollTop; |
|
|
var dArticleBottom = dArticle.offsetTop + dArticle.offsetHeight; |
|
|
var dContentsActualTop = scrollPosition > originalOffsetTop ? scrollPosition : originalOffsetTop; |
|
|
var dContentsBottom = dContentsActualTop + dContents.offsetHeight; |
|
|
if (dContentsBottom >= dArticleBottom) { |
|
|
|
|
|
dContents.style.visibility = 'hidden'; |
|
|
} else { |
|
|
|
|
|
dContents.style.visibility = 'visible'; |
|
|
} |
|
|
|
|
|
|
|
|
if (scrollPosition + marginTop >= originalOffsetTop) { |
|
|
dContents.style.position = 'fixed'; |
|
|
dContents.style.top = '0px'; |
|
|
dContents.style.left = originalOffsetLeft + 'px'; |
|
|
dContents.style.width = originalWidth + 'px'; |
|
|
} else { |
|
|
dContents.style.position = ''; |
|
|
dContents.style.top = ''; |
|
|
dContents.style.left = ''; |
|
|
dContents.style.width = ''; |
|
|
} |
|
|
} else { |
|
|
|
|
|
dContents.style.position = ''; |
|
|
dContents.style.top = ''; |
|
|
dContents.style.left = ''; |
|
|
dContents.style.width = ''; |
|
|
dContents.style.visibility = 'visible'; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
function getActiveSection() { |
|
|
var sections = document.querySelectorAll('section'); |
|
|
var scrollPosition = window.scrollY || document.documentElement.scrollTop; |
|
|
|
|
|
for (var i = 0; i < sections.length; i++) { |
|
|
|
|
|
if (sections[i].offsetTop + sections[i].offsetHeight > scrollPosition + 1) { |
|
|
return sections[i].id; |
|
|
} |
|
|
} |
|
|
return null; |
|
|
} |
|
|
|
|
|
|
|
|
function updateNavigation() { |
|
|
var activeSection = getActiveSection(); |
|
|
var navLinks = document.querySelectorAll('d-contents nav a'); |
|
|
|
|
|
navLinks.forEach(function(navLink) { |
|
|
if (navLink.getAttribute('href') === '#' + activeSection) { |
|
|
navLink.classList.add('active-nav-item'); |
|
|
} else { |
|
|
navLink.classList.remove('active-nav-item'); |
|
|
} |
|
|
}); |
|
|
} |
|
|
|
|
|
|
|
|
window.addEventListener('scroll', updateNavigation); |
|
|
|
|
|
|
|
|
onResize(); |
|
|
|
|
|
updateNavigation(); |
|
|
|