diff --git a/src/js/06-code.js b/src/js/06-code.js index 7f862fac..926b8e1c 100644 --- a/src/js/06-code.js +++ b/src/js/06-code.js @@ -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 diff --git a/src/js/08-tabs-block.js b/src/js/08-tabs-block.js index 4d16b7fd..731ee727 100644 --- a/src/js/08-tabs-block.js +++ b/src/js/08-tabs-block.js @@ -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) }