Last active
August 19, 2021 09:33
-
-
Save treetrum/8bfebce21b3714ba7533cc2c0450faa3 to your computer and use it in GitHub Desktop.
jQuery Slideup & Slidedown replacement with GSAP - TweenLite
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
import { TweenLite } from 'gsap'; | |
const DEFAULT_DURATION = 0.3; | |
export const hideElement = (el, speed = DEFAULT_DURATION) => { | |
const oldSpacing = { height: el.clientHeight }; | |
const newSpacing = { height: 0 }; | |
const additionalSpacingProperties = ['paddingTop', 'paddingBottom', 'marginTop', 'marginBottom']; | |
additionalSpacingProperties.forEach(property => { | |
const value = window.getComputedStyle(el)[property]; | |
if (value !== '0px') { | |
oldSpacing[property] = value; | |
newSpacing[property] = 0; | |
} | |
}); | |
el.showhideprops = oldSpacing; | |
TweenLite.to(el, speed, { | |
overflow: 'hidden', | |
...newSpacing | |
}); | |
}; | |
export const showElement = (el, speed = DEFAULT_DURATION) => { | |
const showhideprops = el.showhideprops || {}; | |
TweenLite.to(el, speed, { | |
clearProps: 'all', | |
...showhideprops | |
}); | |
}; | |
export default { showElement, hideElement }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment