Skip to content

Instantly share code, notes, and snippets.

@ethanmay
Created March 9, 2022 16:26
Show Gist options
  • Save ethanmay/7691a47dff21f502d323d9b9bc513451 to your computer and use it in GitHub Desktop.
Save ethanmay/7691a47dff21f502d323d9b9bc513451 to your computer and use it in GitHub Desktop.
Expand/collapse menu on scroll
var currentMenuState = 'open'
var lastScrollPosition = window.scrollY
const minimizeMenuY = 60
const collapseMenu = () => {
$('.top-bar-container').css('overflow', 'hidden')
gsap.to('.top-bar-container', {
height: 0,
duration: 0.5,
ease: 'power3.inOut'
})
currentMenuState = 'closed'
}
const expandMenu = () => {
gsap.to('.top-bar-container', {
height: 'auto',
duration: 0.5,
ease: 'power3.inOut',
onComplete: function() {
if( currentMenuState === 'open' ) {
$('.top-bar-container').css('overflow', 'visible')
}
}
})
currentMenuState = 'open'
}
const handleMenuState = () => {
if( window.outerWidth > 768 ) {
if( window.scrollY >= minimizeMenuY && window.scrollY > lastScrollPosition && currentMenuState === 'open' ) {
collapseMenu()
} else if( window.scrollY >= minimizeMenuY && window.scrollY < lastScrollPosition && currentMenuState === 'closed' ) {
expandMenu()
} else if( window.scrollY < minimizeMenuY && currentMenuState === 'closed' ) {
expandMenu()
}
lastScrollPosition = window.scrollY
} else {
expandMenu()
}
}
$(window).on( 'scroll', handleMenuState )
@ethanmay
Copy link
Author

ethanmay commented Mar 9, 2022

Be sure to you include gsap or switch to jQuery animations where gsap is used

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment