Browse Source

testing bounce

master
farhan 4 months ago
parent
commit
b817c1c3c5
1 changed files with 32 additions and 7 deletions
  1. 32
    7
      resources/js/app.js

+ 32
- 7
resources/js/app.js View File

55
 $(function() {
55
 $(function() {
56
     var partLeftPos = 0;
56
     var partLeftPos = 0;
57
     var partRightPos = 0;
57
     var partRightPos = 0;
58
+    var isScrolling = false;
59
+    var lastScrollTop = 0;
58
 
60
 
59
     function updatePositions() {
61
     function updatePositions() {
60
-        var distance = $(window).scrollTop() * 2;
62
+        var scrollTop = $(window).scrollTop();
63
+        var distance = scrollTop * 2;
61
         var left = partLeftPos - distance;
64
         var left = partLeftPos - distance;
62
         var right = partRightPos - distance;
65
         var right = partRightPos - distance;
63
 
66
 
81
     // Initial positions update
84
     // Initial positions update
82
     updatePositions();
85
     updatePositions();
83
 
86
 
84
-    // Listen to scroll events using requestAnimationFrame for smooth animation
85
-    var ticking = false;
86
-    $(window).scroll(function() {
87
-        if (!ticking) {
87
+    // Debounce function for scroll events
88
+    function debounce(func, delay) {
89
+        var timeout;
90
+        return function() {
91
+            var context = this;
92
+            var args = arguments;
93
+            var later = function() {
94
+                timeout = null;
95
+                func.apply(context, args);
96
+            };
97
+            clearTimeout(timeout);
98
+            timeout = setTimeout(later, delay);
99
+        };
100
+    }
101
+
102
+    // Update positions on scroll using requestAnimationFrame for smoother animation
103
+    function scrollHandler() {
104
+        if (!isScrolling) {
88
             window.requestAnimationFrame(function() {
105
             window.requestAnimationFrame(function() {
89
                 updatePositions();
106
                 updatePositions();
90
-                ticking = false;
107
+                isScrolling = false;
91
             });
108
             });
92
         }
109
         }
93
-        ticking = true;
110
+        isScrolling = true;
111
+    }
112
+
113
+    // Listen to scroll events and debounce them for better performance
114
+    $(window).scroll(debounce(scrollHandler, 20));
115
+
116
+    // Handle window resize to recalculate positions if needed
117
+    $(window).resize(function() {
118
+        updatePositions();
94
     });
119
     });
95
 });
120
 });
96
 
121
 

Loading…
Cancel
Save