Last active
June 19, 2018 15:02
-
-
Save LucasCalazans/b1e05e15a26bea7ab12951eb78f8c963 to your computer and use it in GitHub Desktop.
Add "sticky-header" class to body
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> | |
<head> | |
<link src="js/sticky-header.js" type="application/javascript" /> | |
</head> | |
</page> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
document.addEventListener('DOMContentLoaded', function() { | |
if(document.body.className.indexOf('checkout-index-index') >= 0) return; | |
var headerContainer = document.getElementsByClassName('page-header')[0]; | |
var checkStickyHeader = function() { | |
if(!headerContainer || window.innerWidth < 768) return; | |
var headerRect = headerContainer.getBoundingClientRect(); | |
var headerContainerTop = headerContainer.offsetTop + headerRect.height; | |
if(window.scrollY <= headerContainerTop) { | |
document.body.style.paddingTop = 'initial'; | |
document.body.classList.remove("sticky-header"); | |
return; | |
} | |
document.body.classList.add("sticky-header"); | |
document.body.style.paddingTop = headerContainerTop + 'px'; | |
}; | |
window.addEventListener('scroll', checkStickyHeader); | |
window.addEventListener('resize', function() { | |
if(window.innerWidth >= 768) return; | |
document.body.style.paddingTop = 'initial'; | |
document.body.classList.remove("sticky-header"); | |
}); | |
checkStickyHeader(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment