PythonMindset commited on
Commit
5fb7b7b
·
1 Parent(s): cda9ef1

Fix scroll freeze bug: properly cleanup observers on page navigation

Browse files
Files changed (1) hide show
  1. static/js/script.js +21 -9
static/js/script.js CHANGED
@@ -1,4 +1,7 @@
1
- // Navigation entre les pages
 
 
 
2
  document.addEventListener("DOMContentLoaded", function() {
3
  const navButtons = document.querySelectorAll(".nav-btn");
4
  const pages = document.querySelectorAll(".page");
@@ -32,7 +35,18 @@ document.addEventListener("DOMContentLoaded", function() {
32
  }
33
 
34
  // Scroll vers le haut
35
- window.scrollTo(0, 0); // Pas de smooth scroll sur mobile
 
 
 
 
 
 
 
 
 
 
 
36
  });
37
  });
38
 
@@ -79,7 +93,7 @@ function createMobileMenuButton() {
79
  });
80
  }
81
 
82
- // Fonction pour détecter les éléments visibles et les animer - VERSION MOBILE OPTIMISÉE
83
  function initScrollAnimations() {
84
  const animatedElements = document.querySelectorAll('.animate-on-scroll, .animate-from-left, .animate-from-right, .animate-zoom');
85
 
@@ -88,7 +102,7 @@ function initScrollAnimations() {
88
  animatedElements.forEach(element => {
89
  element.classList.add('animated');
90
  });
91
- return; // Arrêter ici pour mobile
92
  }
93
 
94
  // Sur desktop : utiliser l'observer normalement
@@ -97,17 +111,17 @@ function initScrollAnimations() {
97
  rootMargin: '0px 0px -50px 0px'
98
  };
99
 
100
- const observer = new IntersectionObserver((entries) => {
101
  entries.forEach(entry => {
102
  if (entry.isIntersecting) {
103
  entry.target.classList.add('animated');
104
- observer.unobserve(entry.target);
105
  }
106
  });
107
  }, observerOptions);
108
 
109
  animatedElements.forEach(element => {
110
- observer.observe(element);
111
  });
112
  }
113
 
@@ -156,5 +170,3 @@ document.addEventListener("DOMContentLoaded", function() {
156
  });
157
  }
158
  });
159
-
160
-
 
1
+ // Variable globale pour stocker l'observer
2
+ let scrollObserver = null;
3
+
4
+ // Navigation entre les pages
5
  document.addEventListener("DOMContentLoaded", function() {
6
  const navButtons = document.querySelectorAll(".nav-btn");
7
  const pages = document.querySelectorAll(".page");
 
35
  }
36
 
37
  // Scroll vers le haut
38
+ window.scrollTo(0, 0);
39
+
40
+ // CRITICAL: Nettoyer l'ancien observer avant d'en créer un nouveau
41
+ if (scrollObserver) {
42
+ scrollObserver.disconnect();
43
+ scrollObserver = null;
44
+ }
45
+
46
+ // Réinitialiser les animations pour la nouvelle page
47
+ setTimeout(() => {
48
+ initScrollAnimations();
49
+ }, 100);
50
  });
51
  });
52
 
 
93
  });
94
  }
95
 
96
+ // Fonction pour détecter les éléments visibles et les animer - VERSION FIXÉE
97
  function initScrollAnimations() {
98
  const animatedElements = document.querySelectorAll('.animate-on-scroll, .animate-from-left, .animate-from-right, .animate-zoom');
99
 
 
102
  animatedElements.forEach(element => {
103
  element.classList.add('animated');
104
  });
105
+ return; // Arrêter ici pour mobile - PAS D'OBSERVER
106
  }
107
 
108
  // Sur desktop : utiliser l'observer normalement
 
111
  rootMargin: '0px 0px -50px 0px'
112
  };
113
 
114
+ scrollObserver = new IntersectionObserver((entries) => {
115
  entries.forEach(entry => {
116
  if (entry.isIntersecting) {
117
  entry.target.classList.add('animated');
118
+ scrollObserver.unobserve(entry.target);
119
  }
120
  });
121
  }, observerOptions);
122
 
123
  animatedElements.forEach(element => {
124
+ scrollObserver.observe(element);
125
  });
126
  }
127
 
 
170
  });
171
  }
172
  });