Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tabbed examples scrolling behavior #247

Merged
merged 3 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/js/06-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,24 @@ document.addEventListener('DOMContentLoaded', function () {
el.classList.add(tabActive)
})
})

var toolbarOffset = 0
var toolbar = document.querySelector('.toolbar')
if (toolbar.offsetHeight) {
toolbarOffset = toolbar.offsetHeight
}
var offset = document.querySelector('.navbar').offsetHeight + toolbarOffset + 20

var vh = Math.max(document.documentElement.clientHeight || 0, window.innerHeight || 0)
var bodyRect = document.body.getBoundingClientRect().top
var elementRect = tab.getBoundingClientRect().top
var elementPosition = elementRect - bodyRect
var offsetPosition = elementPosition - offset - vh / 5

window.scrollTo({
top: offsetPosition, // center clicked tab to a fifth of viewport height
behavior: 'smooth',
})
}

// Tabbed code
Expand Down
18 changes: 18 additions & 0 deletions src/js/08-tabs-block.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,24 @@ document.addEventListener('DOMContentLoaded', function () {
})
})

var toolbarOffset = 0
var toolbar = document.querySelector('.toolbar')
if (toolbar.offsetHeight) {
toolbarOffset = toolbar.offsetHeight
}
var offset = document.querySelector('.navbar').offsetHeight + toolbarOffset + 20

var vh = Math.max(document.documentElement.clientHeight || 0, window.innerHeight || 0)
var bodyRect = document.body.getBoundingClientRect().top
var elementRect = tab.getBoundingClientRect().top
var elementPosition = elementRect - bodyRect
var offsetPosition = elementPosition - offset - vh / 5

window.scrollTo({
top: offsetPosition, // center clicked tab to a fifth of viewport height
behavior: 'smooth',
})

if (sessionStorageAvailable) {
window.sessionStorage.setItem('code_example_language', lang)
}
Expand Down