@@ -120,13 +120,37 @@ <h3 class="archive__item-title">
120120</ section >
121121
122122< script >
123+ var lastHeights = [ ] ;
124+ var masonryAttempts = 0 ;
125+ var isStable = false ;
126+
123127function applyMasonry ( ) {
124128 var grid = document . querySelector ( '.media-grid' ) ;
125129 var items = grid . querySelectorAll ( '.media-card' ) ;
126130
127131 var rowHeight = 20 ; // Match CSS grid-auto-rows
128132 var verticalGap = 1 ; // Match CSS row-gap
129- var horizontalGap = 20 ; // Match CSS column-gap (not used in calculation)
133+
134+ masonryAttempts ++ ;
135+
136+ // Check current heights first
137+ var currentHeights = [ ] ;
138+ for ( var i = 0 ; i < items . length ; i ++ ) {
139+ var rect = items [ i ] . getBoundingClientRect ( ) ;
140+ currentHeights . push ( rect . height ) ;
141+ }
142+
143+ // Only proceed if heights have changed or this is the first run
144+ var heightsChanged = JSON . stringify ( currentHeights ) !== JSON . stringify ( lastHeights ) ;
145+
146+ if ( ! heightsChanged && masonryAttempts > 1 ) {
147+ if ( ! isStable ) {
148+ isStable = true ; // Mark as stable, stop periodic checks
149+ }
150+ return ; // No need to recalculate
151+ }
152+
153+ lastHeights = currentHeights . slice ( ) ;
130154
131155 // Reset all items first
132156 for ( var i = 0 ; i < items . length ; i ++ ) {
@@ -136,39 +160,51 @@ <h3 class="archive__item-title">
136160 // Force reflow
137161 grid . offsetHeight ;
138162
139- // Calculate spans
163+ // Wait for content to settle, then calculate
140164 setTimeout ( function ( ) {
141165 for ( var i = 0 ; i < items . length ; i ++ ) {
142166 var rect = items [ i ] . getBoundingClientRect ( ) ;
143167 var itemHeight = rect . height ;
144168
145- // Only use vertical gap in calculation since that's what affects row spans
146169 var rowSpan = Math . ceil ( ( itemHeight + verticalGap ) / rowHeight ) ;
147-
148170 items [ i ] . style . gridRowEnd = 'span ' + rowSpan ;
149171 }
150172 } , 100 ) ;
151173}
152174
153- // Apply masonry multiple times to handle loading content
175+ // Initial load timing - less aggressive
154176setTimeout ( applyMasonry , 500 ) ;
155177setTimeout ( applyMasonry , 1500 ) ;
156178setTimeout ( applyMasonry , 3000 ) ;
157- setTimeout ( applyMasonry , 5000 ) ;
179+ setTimeout ( applyMasonry , 6000 ) ;
158180
159181window . addEventListener ( 'resize' , function ( ) {
160- setTimeout ( applyMasonry , 200 ) ;
182+ isStable = false ; // Reset stability on resize
183+ setTimeout ( applyMasonry , 300 ) ;
161184} ) ;
162185
163186// Handle Instagram embeds
164- if ( window . instgrm ) {
165- setTimeout ( function ( ) {
166- if ( window . instgrm . Embeds ) {
167- window . instgrm . Embeds . process ( ) ;
168- setTimeout ( applyMasonry , 2000 ) ;
169- }
170- } , 1000 ) ;
187+ function handleInstagramEmbeds ( ) {
188+ if ( window . instgrm && window . instgrm . Embeds ) {
189+ window . instgrm . Embeds . process ( ) ;
190+ setTimeout ( function ( ) {
191+ isStable = false ; // Reset stability when Instagram loads
192+ applyMasonry ( ) ;
193+ } , 2000 ) ;
194+ }
171195}
196+
197+ setTimeout ( handleInstagramEmbeds , 2000 ) ;
198+ setTimeout ( handleInstagramEmbeds , 5000 ) ;
199+
200+ // Only check periodically if not stable, and less frequently
201+ var periodicCheck = setInterval ( function ( ) {
202+ if ( ! isStable && masonryAttempts < 20 ) {
203+ applyMasonry ( ) ;
204+ } else {
205+ clearInterval ( periodicCheck ) ;
206+ }
207+ } , 5000 ) ; // Every 5 seconds instead of 2
172208</ script >
173209
174210< script async src ="https://www.instagram.com/embed.js "> </ script >
0 commit comments