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 tab position in viewport #284

Merged
merged 2 commits into from
Dec 3, 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
28 changes: 14 additions & 14 deletions src/js/06-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,16 @@ document.addEventListener('DOMContentLoaded', function () {
var switchTab = function (e) {
var tab = e.target
var title = tab.innerHTML

var toolbarOffset = 0
var toolbar = document.querySelector('.toolbar')
if (toolbar.offsetHeight) {
toolbarOffset = toolbar.offsetHeight
}
var offset = document.querySelector('.navbar').offsetHeight + toolbarOffset + 20
var topOfWindowPosition = window.scrollY + offset
var topOfTabPosition = tab.getBoundingClientRect().top + window.scrollY

// Switch Tabs
var targetTabs = document.querySelectorAll('.tabbed-target[data-title="' + title + '"]')
targetTabs.forEach(function (target) {
Expand All @@ -277,22 +287,12 @@ 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
var newTopOfTabPosition = tab.getBoundingClientRect().top + window.scrollY
var shift = topOfTabPosition - newTopOfTabPosition

window.scrollTo({
top: offsetPosition, // center clicked tab to a fifth of viewport height
behavior: 'smooth',
top: topOfWindowPosition - shift - offset, // scroll back to the same position before the click
behavior: 'instant', // instantly so nothing is visible to the user
})
}

Expand Down
28 changes: 14 additions & 14 deletions src/js/08-tabs-block.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ document.addEventListener('DOMContentLoaded', function () {
var tab = e.target
var lang = tab.dataset.lang
var title = tab.dataset.title

var toolbarOffset = 0
var toolbar = document.querySelector('.toolbar')
if (toolbar.offsetHeight) {
toolbarOffset = toolbar.offsetHeight
}
var offset = document.querySelector('.navbar').offsetHeight + toolbarOffset + 20
var topOfWindowPosition = window.scrollY + offset
var topOfTabPosition = tab.getBoundingClientRect().top + window.scrollY

// Switch Tabs
var targetTabs = document.querySelectorAll('.tabbed-target[data-title="' + title + '"]')
targetTabs.forEach(function (target) {
Expand All @@ -63,22 +73,12 @@ 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
var newTopOfTabPosition = tab.getBoundingClientRect().top + window.scrollY
var shift = topOfTabPosition - newTopOfTabPosition

window.scrollTo({
top: offsetPosition, // center clicked tab to a fifth of viewport height
behavior: 'smooth',
top: topOfWindowPosition - shift - offset, // scroll back to the same position before the click
behavior: 'instant', // instantly so nothing is visible to the user
})

if (sessionStorageAvailable) {
Expand Down