Advertisement
artemsemkin

Rhye theme: custom functions in AJAX

Dec 20th, 2022 (edited)
1,292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // I would recommend to avoid generic functions names like "hide" or "animateFrom"
  2. // Those reside in global scope and may cause collisions with previous declarations.
  3. // The best way to avoid this is to prefix all custom functions e.g. "jochen_function".
  4.  
  5. function jochen_animate_from(elem, direction) {
  6.   direction = direction || 1;
  7.  
  8.   var x = 0,
  9.     y = direction * 100;
  10.   if (elem.classList.contains('gs_reveal_fromLeft')) {
  11.     x = -500;
  12.     y = 500;
  13.   } else if (elem.classList.contains('gs_reveal_fromRight')) {
  14.     x = 500;
  15.     y = -500;
  16.   }
  17.   elem.style.transform = 'translate(' + x + 'px, ' + y + 'px)';
  18.   elem.style.opacity = '0';
  19.  
  20.   gsap.fromTo(elem, { x: x, y: y, autoAlpha: 0 }, {
  21.     duration: 1.25,
  22.     x: 0,
  23.     y: 0,
  24.     autoAlpha: 1,
  25.     ease: 'expo',
  26.     overwrite: 'auto'
  27.   });
  28. }
  29.  
  30. function jochen_hide(elem) {
  31.   gsap.set(elem, { autoAlpha: 0 });
  32. }
  33.  
  34. function jochen_move_left() {
  35.   if (document.querySelector('.jhd-gsap-move.left')) {
  36.     gsap.to('.jhd-gsap-move.left', {
  37.       x: 200,
  38.       //x: -100,
  39.       //rotation: 360,
  40.       scrollTrigger: {
  41.         trigger: '.jhd-gsap-move.left',
  42.         start: 'top 120%',
  43.         //end: 'top 10%',
  44.         end: '+=200%', // end after scrolling x % beyond the start
  45.         scrub: true,
  46.         //markers: true,
  47.         id: 'scrub'
  48.       }
  49.     });
  50.   }
  51. }
  52.  
  53. function jochen_move_right() {
  54.   if (document.querySelector('.jhd-gsap-move.right')) {
  55.     gsap.to('.jhd-gsap-move.right', {
  56.       x: -200,
  57.       //x: -100,
  58.       //rotation: 360,
  59.       scrollTrigger: {
  60.         trigger: '.jhd-gsap-move.right',
  61.         start: 'top 120%',
  62.         //end: 'top 10%',
  63.         end: '+=200%', // end after scrolling x % beyond the start
  64.         scrub: true,
  65.         //markers: true,
  66.         id: 'scrub'
  67.       }
  68.     });
  69.   }
  70. }
  71.  
  72. function jochen_left_prlx() {
  73.   if (document.querySelector('.jhd-gsap-move.left.prlx')) {
  74.     gsap.to('.jhd-gsap-move.left.prlx', {
  75.       x: 300,
  76.       //x: -100,
  77.       //rotation: 360,
  78.       scrollTrigger: {
  79.         trigger: '.jhd-gsap-move.left.prlx',
  80.         start: 'top 120%',
  81.         //end: 'top 10%',
  82.         end: '+=200%', // end after scrolling x % beyond the start
  83.         scrub: true,
  84.         //markers: true,
  85.         id: 'scrub'
  86.       }
  87.     });
  88.   }
  89. }
  90.  
  91. function jochen_right_prlx() {
  92.   if (document.querySelector('.jhd-gsap-move.right.prlx')) {
  93.     gsap.to('.jhd-gsap-move.right.prlx', {
  94.       x: -300,
  95.       //x: -100,
  96.       //rotation: 360,
  97.       scrollTrigger: {
  98.         trigger: '.jhd-gsap-move.right.prlx',
  99.         start: 'top 120%',
  100.         //end: 'top 10%',
  101.         end: '+=200%', // end after scrolling x % beyond the start
  102.         scrub: true,
  103.         //markers: true,
  104.         id: 'scrub'
  105.       }
  106.     });
  107.   }
  108. }
  109.  
  110. function jochen_move_up() {
  111.   if (document.querySelector('.jhd-gsap-move-up')) {
  112.     gsap.to('.jhd-gsap-move-up', {
  113.       y: -100,
  114.       //x: -100,
  115.       //rotation: 360,
  116.       scrollTrigger: {
  117.         trigger: '.jhd-gsap-move-up',
  118.         start: 'top 90%',
  119.         //end: 'top 10%',
  120.         end: '+=90%', // end after scrolling x % beyond the start
  121.         scrub: true,
  122.         //markers: true,
  123.         id: 'scrub'
  124.       }
  125.     });
  126.   }
  127. }
  128.  
  129. function jochen_move_down() {
  130.   if (document.querySelector('.jhd-gsap-move-down')) {
  131.     gsap.to('.jhd-gsap-move-down', {
  132.       y: 100,
  133.       //rotation: 360,
  134.       scrollTrigger: {
  135.         trigger: '.jhd-gsap-move-down',
  136.         start: 'top 90%',
  137.         //end: 'top 10%',
  138.         end: '+=90%', // end after scrolling x % beyond the start
  139.         scrub: true,
  140.         //markers: true,
  141.         id: 'scrub'
  142.       }
  143.     });
  144.   }
  145. }
  146.  
  147. function jochen_box_test() {
  148.   gsap.utils.toArray('.box-test').forEach(function (elem) {
  149.     jochen_hide(elem); // assure that the element is hidden when scrolled into view
  150.     ScrollTrigger.create({
  151.       trigger: elem,
  152.       //markers: true,
  153.       onEnter: function () { jochen_animate_from(elem) },
  154.       onEnterBack: function () { jochen_animate_from(elem, -1) },
  155.       onLeave: function () { jochen_hide(elem) } // assure that the element is hidden when scrolled into view
  156.     });
  157.   });
  158. }
  159.  
  160. document.addEventListener('DOMContentLoaded', () => {
  161.   jochen_box_test();
  162.   jochen_move_left();
  163.   jochen_move_right();
  164.   jochen_left_prlx();
  165.   jochen_right_prlx();
  166.   jochen_move_up();
  167.   jochen_move_down();
  168.  
  169.   console.log('Custom functions executed');
  170. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement