Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function createObserver(handleIntersect, el, $options = {}) {
  2.     const options = {
  3.         root: null,
  4.         threshold: 0.2,
  5.         ...$options,
  6.     };
  7.     const observer = new IntersectionObserver(handleIntersect, options);
  8.  
  9.     observer.observe(el); // target element to watch
  10. }
  11.  
  12. export default {
  13.     inserted(el, binding) {
  14.         let ran = 0;
  15.         let wasVisible = 0;
  16.         const [callback, options] = binding.value;
  17.         if ((window.IntersectionObserver || typeof IntersectionObserver === 'function') && el) {
  18.             createObserver(handleIntersect, el, options);
  19.         } else handleIntersect([{ isIntersecting: true }], null);
  20.         function handleIntersect([element], observer) {
  21.             if (binding.modifiers.once && (ran > 1 || wasVisible > 0)) {
  22.                 if (observer) observer.unobserve(el);
  23.                 return;
  24.             }
  25.             callback(element.isIntersecting);
  26.             wasVisible++;
  27.             ran++;
  28.         }
  29.     },
  30. };
  31. // usage
  32. <div v-visible.once="[value => isVisible = value]"></div>
  33.  
  34. //once will only run the directive one time
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement