Skip to content

Instantly share code, notes, and snippets.

@treetrum
Last active August 19, 2021 09:33
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save treetrum/8bfebce21b3714ba7533cc2c0450faa3 to your computer and use it in GitHub Desktop.
Save treetrum/8bfebce21b3714ba7533cc2c0450faa3 to your computer and use it in GitHub Desktop.
jQuery Slideup & Slidedown replacement with GSAP - TweenLite
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