Skip to content

Commit

Permalink
smooth scrolling for anchor links that are not in the menu
Browse files Browse the repository at this point in the history
  • Loading branch information
nrchtct committed Dec 12, 2023
1 parent 943f6f9 commit 0b09869
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
19 changes: 12 additions & 7 deletions js/navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,19 @@ function toggleMenu(){
}
window.onhashchange = () => {hashChange()}
function hashChange(){
if(sessions.length > 0){
if (window.location.hash == ""){
window.scrollTo({top: 0,behavior: 'smooth' })
return;
if(sessions.length > 0) {
let hash = window.location.hash;
if (hash == ""){
window.scrollTo({top: 0})
}
let session = sessions.filter(session => session.hash == window.location.hash.substring(1))[0]
if(typeof session != "undefined"){
window.scrollTo({top: session.index == 0 ? 0 :session.margin + (session.height/2), behavior: 'smooth' })
else {
let session = sessions.filter(session => session.hash == window.location.hash.substring(1))[0]
if (typeof session != "undefined") {
window.scrollTo({top: session.index == 0 ? 0 :session.margin + (session.height/2)})
return
}
let element = document.getElementById(hash);
if (element) element.scrollIntoView();
}
}
}
2 changes: 0 additions & 2 deletions js/scrolling.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@ function updateCursor(currentSession,currentProgress) {
let cursor = d3.select('#cursor')
//update the cursor if the session changes
if (lastSession == null || lastSession.index != currentSession.index) {
console.log(currentSession);
cursor.html(marked.parse(currentSession.text))
lastSession = currentSession
document.getElementById('anchors').childNodes.forEach(childNode => {childNode.classList.remove('active')})
let currentNav = document.querySelector('#anchors .'+currentSession.hash)
if (currentNav!= null) {
currentNav.classList.add('active')
// console.log(currentSession.index);
}
}
//update the cursor dimension and the position according to the scrolling progress of the session
Expand Down
3 changes: 3 additions & 0 deletions style.css
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
font-weight: bold;
}
/* Global styles*/
html {
scroll-behavior: smooth;
}
body{
--background-menu: hsl(0, 0%, 100%);
--background: hsl(0, 0%, 94%);
Expand Down

0 comments on commit 0b09869

Please sign in to comment.