Browse Source

refactor js

master
farhan 4 months ago
parent
commit
a0e1ed6faa
1 changed files with 193 additions and 56 deletions
  1. 193
    56
      resources/js/app.js

+ 193
- 56
resources/js/app.js View File

53
 }, 1000); // Update every second (1000 milliseconds)
53
 }, 1000); // Update every second (1000 milliseconds)
54
 
54
 
55
 $(document).ready(function() {
55
 $(document).ready(function() {
56
+    var partLeftPos = 0;
57
+    var partRightPos = 0;
56
     var audioElement = document.getElementById('player');
58
     var audioElement = document.getElementById('player');
57
     var hasPlayed = false;
59
     var hasPlayed = false;
58
     var scrollCount = 0;
60
     var scrollCount = 0;
60
     var scrollInterval = 200;
62
     var scrollInterval = 200;
61
     var scrollAmount = window.innerHeight / 6;
63
     var scrollAmount = window.innerHeight / 6;
62
 
64
 
63
-    function playAudio() {
64
-        if (!hasPlayed) {
65
-            var playPromise = audioElement.play();
66
-            if (playPromise !== undefined) {
67
-                playPromise.then(function() {
68
-                    hasPlayed = true;
69
-                }).catch(function(error) {
70
-                    console.log('Autoplay failed: ' + error);
71
-                });
72
-            }
73
-        }
74
-    }
75
-
65
+    // Function to handle smooth scrolling
76
     function smoothScroll() {
66
     function smoothScroll() {
77
         if (!hasPlayed) {
67
         if (!hasPlayed) {
78
             playAudio();
68
             playAudio();
84
                 var target = start + scrollAmount;
74
                 var target = start + scrollAmount;
85
                 var screenWidth = window.innerWidth;
75
                 var screenWidth = window.innerWidth;
86
                 var duration;
76
                 var duration;
77
+
87
                 if (screenWidth < 600) {
78
                 if (screenWidth < 600) {
88
-                    duration = 5000;
79
+                    duration = 5000; // Smaller screen
89
                 } else if (screenWidth < 1200) {
80
                 } else if (screenWidth < 1200) {
90
-                    duration = 12000;
81
+                    duration = 12000; // Medium screen
91
                 } else {
82
                 } else {
92
-                    duration = 15000;
83
+                    duration = 15000; // Larger screen
93
                 }
84
                 }
85
+
94
                 var startTime = null;
86
                 var startTime = null;
95
 
87
 
96
                 function scrollStep(timestamp) {
88
                 function scrollStep(timestamp) {
101
                     if (progress < duration) {
93
                     if (progress < duration) {
102
                         requestAnimationFrame(scrollStep);
94
                         requestAnimationFrame(scrollStep);
103
                     } else {
95
                     } else {
104
-                        setTimeout(smoothScroll, scrollInterval);
96
+                        setTimeout(smoothScroll, scrollInterval); // Continues scrolling after a delay
105
                     }
97
                     }
106
                 }
98
                 }
107
 
99
 
110
         }
102
         }
111
     }
103
     }
112
 
104
 
113
-    function handleInteraction() {
114
-        document.body.classList.remove('no-scroll');
115
-        setTimeout(smoothScroll, scrollInterval);
105
+    // Function to play audio (if not already played)
106
+    function playAudio() {
107
+        if (!hasPlayed) {
108
+            var playPromise = audioElement.play();
109
+
110
+            if (playPromise !== undefined) {
111
+                playPromise.then(function() {
112
+                    hasPlayed = true;
113
+                }).catch(function(error) {
114
+                    console.log('Autoplay failed: ' + error);
115
+                });
116
+            }
117
+        }
116
     }
118
     }
117
 
119
 
118
-    window.addEventListener('click', function() {
120
+    // jQuery scroll event handling
121
+    $(window).scroll(function() {
122
+        var distance = $(this).scrollTop() * 2;
123
+        var left = partLeftPos - distance;
124
+        var right = partRightPos - distance;
125
+
126
+        $('.left').css('left', left + "px");
127
+        $('.right').css('right', right + "px");
128
+
129
+        var leftPartRightEdge = $('.left').offset().left + $('.left').outerWidth();
130
+        var isLeftOffScreen = leftPartRightEdge <= 0;
131
+
132
+        if (isLeftOffScreen && left < 0) {
133
+            $('.parent').removeClass('z-30');
134
+        } else {
135
+            $('.parent').addClass('z-30');
136
+        }
137
+    });
138
+
139
+    // Initially check visibility on page load
140
+    $(window).trigger('scroll');
141
+
142
+    // Event listener for click to show overlay after a delay
143
+    document.addEventListener('click', function() {
119
         setTimeout(function() {
144
         setTimeout(function() {
120
             document.getElementById('petalOverlay').style.display = 'block';
145
             document.getElementById('petalOverlay').style.display = 'block';
121
-        }, 1000);
146
+        }, 1000); // Delay of 1 second (1000 milliseconds)
122
     });
147
     });
123
 
148
 
149
+    // Event listener for scroll to reset audio and overlay if scrolled to top
124
     window.addEventListener('scroll', function() {
150
     window.addEventListener('scroll', function() {
125
         if (window.pageYOffset === 0) {
151
         if (window.pageYOffset === 0) {
126
             audioElement.pause();
152
             audioElement.pause();
129
             document.body.classList.add('no-scroll');
155
             document.body.classList.add('no-scroll');
130
         }
156
         }
131
     });
157
     });
158
+});
132
 
159
 
133
-    const observerOptions = {
134
-        root: null,
135
-        rootMargin: '0px',
136
-        threshold: 0.1
137
-    };
138
-
139
-    const observer = new IntersectionObserver((entries, observer) => {
140
-        entries.forEach(entry => {
141
-            if (entry.isIntersecting) {
142
-                entry.target.classList.add('fade-in');
143
-                observer.unobserve(entry.target);
144
-            }
145
-        });
146
-    }, observerOptions);
147
 
160
 
148
-    document.querySelectorAll('.fade-element').forEach(element => {
149
-        observer.observe(element);
150
-    });
151
-
152
-    $(window).scroll(function(event) {
153
-        var distance = $(this).scrollTop() * 2;
154
-        var left = 0 - distance;
155
-        var right = 0 - distance;
161
+// $(function() {
162
+//     var partLeftPos = 0;
163
+//     var partRightPos = 0;
164
+    
165
+//     $(window).scroll(function(event) {
166
+//         var distance = $(this).scrollTop() * 2;
167
+//         var left = partLeftPos - distance;
168
+//         var right = partRightPos - distance;
156
         
169
         
157
-        $('.left').css('left', left + "px");
158
-        $('.right').css('right', right + "px");
170
+//         $('.left').css('left', left + "px");
171
+//         $('.right').css('right', right + "px");
159
 
172
 
160
-        var leftPartRightEdge = $('.left').offset().left + $('.left').outerWidth();
173
+//         // Calculate boundaries
174
+//         var leftPartRightEdge = $('.left').offset().left + $('.left').outerWidth();
161
 
175
 
162
-        var isLeftOffScreen = leftPartRightEdge <= 0;
176
+//         // Check if both parts are completely off-screen
177
+//         var isLeftOffScreen = leftPartRightEdge <= 0;
163
 
178
 
164
-        if (isLeftOffScreen && left < 0) {
165
-            $('.parent').removeClass('z-30');
166
-        } else {
167
-            $('.parent').addClass('z-30');
168
-        }
169
-    });
179
+//         // Toggle visibility and z-index
180
+//         if (isLeftOffScreen && left < 0) {
181
+//             $('.parent').removeClass(['z-30']);
182
+//         } else {
183
+//             $('.parent').addClass(['z-30']);
184
+//         }
185
+//     });
170
 
186
 
171
-    $(window).trigger('scroll');
172
-});
187
+//     // Initially check visibility on page load
188
+//     $(window).trigger('scroll');
189
+
190
+    
191
+// });
192
+
193
+// document.addEventListener('click', function () {
194
+//     setTimeout(function () {
195
+//         document.getElementById('petalOverlay').style.display = 'block';
196
+//     }, 1000); // Delay of 1 second (1000 milliseconds)
197
+// });
198
+
199
+// document.addEventListener('DOMContentLoaded', function() {
200
+//     var audioElement = document.getElementById('player');
201
+//     var hasPlayed = false; // To ensure audio only plays once upon clicking and scrolling
202
+//     var scrollCount = 0;
203
+//     var maxScrolls = 8;
204
+//     var scrollInterval = 200; // Time in milliseconds between each scroll
205
+//     var scrollAmount = window.innerHeight / 6; // Amount to scroll per interval
206
+
207
+//     function playAudio() {
208
+//         if (!hasPlayed) {
209
+//             console.log('play')
210
+//             var playPromise = audioElement.play();
211
+
212
+//             if (playPromise !== undefined) {
213
+//                 playPromise.then(function() {
214
+//                     // Automatic playback started!
215
+//                     hasPlayed = true; // Prevent further attempts to play
216
+//                 }).catch(function(error) {
217
+//                     // Automatic playback failed.
218
+//                     console.log('Autoplay failed: ' + error);
219
+//                 });
220
+//             }
221
+//         }
222
+//     }
223
+
224
+//     function smoothScroll() {
225
+//         if (!hasPlayed) {
226
+//             playAudio();
227
+//             scrollCount++;
228
+//             if (scrollCount >= maxScrolls) {
229
+//                 playAudio();
230
+//             } else {
231
+//                 var start = window.pageYOffset;
232
+//                 var target = start + scrollAmount;
233
+//                  // Set duration based on screen size
234
+//                 var screenWidth = window.innerWidth;
235
+//                 var duration;
236
+//                 if (screenWidth < 600) {
237
+//                     duration = 5000; // Smaller screen
238
+//                 } else if (screenWidth < 1200) {
239
+//                     duration = 12000; // Medium screen
240
+//                 } else {
241
+//                     duration = 15000; // Larger screen
242
+//                 }
243
+//                 var startTime = null;
244
+
245
+//                 function scrollStep(timestamp) {
246
+//                     if (!startTime) startTime = timestamp;
247
+//                     var progress = timestamp - startTime;
248
+//                     var currentPosition = start + ((target - start) * (progress / duration));
249
+//                     window.scrollTo(0, currentPosition);
250
+//                     if (progress < duration) {
251
+//                         requestAnimationFrame(scrollStep);
252
+//                     } else {
253
+//                         setTimeout(smoothScroll, scrollInterval); // Continues scrolling after a delay
254
+//                     }
255
+//                 }
256
+
257
+//                 requestAnimationFrame(scrollStep);
258
+//             }
259
+//         }
260
+//     }
261
+
262
+//     function playOnly() {
263
+//         if (!hasPlayed) {
264
+//             playAudio();
265
+//             scrollCount++;
266
+//             if (scrollCount >= maxScrolls) {
267
+//                 playAudio();
268
+//             } else {
269
+//                 setTimeout(playOnly, scrollInterval);
270
+//             }
271
+//         }
272
+//     }
273
+
274
+//     function handleInteraction() {
275
+//         document.body.classList.remove('no-scroll'); // Enable scrolling
276
+//         setTimeout(smoothScroll, scrollInterval); // Start smooth scrolling after initial delay
277
+//     }
278
+
279
+//     // Listen for click or touch event to start auto-scrolling
280
+//     window.addEventListener('click', handleInteraction);
281
+
282
+//     window.addEventListener('scroll', function() {
283
+//         if (window.pageYOffset === 0) {
284
+//             audioElement.pause();
285
+//             document.getElementById('petalOverlay').style.display = 'none';
286
+//             hasPlayed = false; // Reset to allow replay
287
+//             document.body.classList.add('no-scroll');
288
+//         }
289
+//     });
290
+
291
+//      // Intersection Observer for fading in elements
292
+//     const observerOptions = {
293
+//         root: null,
294
+//         rootMargin: '0px',
295
+//         threshold: 0.1
296
+//     };
297
+
298
+//     const observer = new IntersectionObserver((entries, observer) => {
299
+//         entries.forEach(entry => {
300
+//             if (entry.isIntersecting) {
301
+//                 entry.target.classList.add('fade-in');
302
+//                 observer.unobserve(entry.target);
303
+//             }
304
+//         });
305
+//     }, observerOptions);
173
 
306
 
307
+//     document.querySelectorAll('.fade-element').forEach(element => {
308
+//         observer.observe(element);
309
+//     });
310
+// });

Loading…
Cancel
Save