Last active
January 21, 2021 13:31
-
-
Save markusand/c818457c629d94bef8ac5240c4d7b80f to your computer and use it in GitHub Desktop.
Vue.js directive to stagger CSS transitions
This file contains 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 StaggerDirective from '@/utils/directive.stagger'; | |
* Vue.directive('stagger', StaggerDirective); | |
* | |
* Optional directive value selects elements through CSS selectors v-stagger="'.stagger-item'" | |
* Optional argument defines generic stagger interval v-stagger:250 (default: 100ms) | |
* Optional data-stagger for single item delay | |
*/ | |
export default { | |
bind(el, binding) { | |
const { value: selector, arg: ms } = binding | |
const items = selector ? [...el.querySelectorAll(selector)] : [...el.children]; | |
items.forEach((item, index) => { | |
const interval = index * (parseInt(ms, 10) || 100); | |
const { stagger } = item.dataset; | |
item.classList.add('stagger-ini'); | |
setTimeout(() => { | |
item.classList.remove('stagger-ini'); | |
item.classList.add('stagger-end'); | |
}, stagger || interval); | |
}); | |
}, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment