Skip to content

Instantly share code, notes, and snippets.

@markusand
Last active January 21, 2021 13:31
Show Gist options
  • Save markusand/c818457c629d94bef8ac5240c4d7b80f to your computer and use it in GitHub Desktop.
Save markusand/c818457c629d94bef8ac5240c4d7b80f to your computer and use it in GitHub Desktop.
Vue.js directive to stagger CSS transitions
/**
* 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