Add scroll-triggered image loading with 1000px distance limit

- Trigger image loader on scroll events (debounced 50ms)
- Only load images within 1000px of viewport edges
- Images further away wait until user scrolls closer

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Hanson.xyz Dev
2025-12-17 02:47:47 -06:00
parent 51a5c3e166
commit 1c81fd6766
2 changed files with 29 additions and 6 deletions
File diff suppressed because one or more lines are too long
@@ -1877,9 +1877,24 @@
_isRunning: false, _isRunning: false,
_activeLoads: 0, _activeLoads: 0,
MAX_PARALLEL: 2, MAX_PARALLEL: 2,
LOAD_DISTANCE: 1000, // Only load images within 1000px of viewport
init: function() { init: function() {
this.process(); this.process();
this.bindScrollEvent();
},
bindScrollEvent: function() {
var self = this;
var scrollTimeout;
$(window).on('scroll', function() {
// Debounce scroll events
clearTimeout(scrollTimeout);
scrollTimeout = setTimeout(function() {
self.process();
}, 50);
});
}, },
/** /**
@@ -1896,6 +1911,7 @@
/** /**
* Get the next element to load, prioritized by viewport proximity * Get the next element to load, prioritized by viewport proximity
* Returns element in viewport first, then by distance from viewport * Returns element in viewport first, then by distance from viewport
* Only returns elements within LOAD_DISTANCE of viewport
*/ */
_getNextElement: function() { _getNextElement: function() {
var $elements = $('.property-card-image[data-bg]'); var $elements = $('.property-card-image[data-bg]');
@@ -1904,6 +1920,7 @@
var scrollTop = window.pageYOffset || document.documentElement.scrollTop; var scrollTop = window.pageYOffset || document.documentElement.scrollTop;
var viewportTop = scrollTop; var viewportTop = scrollTop;
var viewportBottom = scrollTop + window.innerHeight; var viewportBottom = scrollTop + window.innerHeight;
var maxDistance = this.LOAD_DISTANCE;
var inViewport = []; var inViewport = [];
var outOfViewport = []; var outOfViewport = [];
@@ -1924,15 +1941,21 @@
// Out of viewport - calculate distance // Out of viewport - calculate distance
var distance; var distance;
if (elementTop > viewportBottom) { if (elementTop > viewportBottom) {
// Element is below viewport
distance = elementTop - viewportBottom; distance = elementTop - viewportBottom;
} else { } else {
// Element is above viewport
distance = viewportTop - elementBottom; distance = viewportTop - elementBottom;
} }
// Only include if within LOAD_DISTANCE
if (distance <= maxDistance) {
outOfViewport.push({ outOfViewport.push({
el: this, el: this,
distance: distance distance: distance
}); });
} }
}
}); });
// Sort in-viewport by position (top to bottom) // Sort in-viewport by position (top to bottom)
@@ -1945,7 +1968,7 @@
return a.distance - b.distance; return a.distance - b.distance;
}); });
// Return first in-viewport, or closest out-of-viewport // Return first in-viewport, or closest out-of-viewport within range
if (inViewport.length) return inViewport[0].el; if (inViewport.length) return inViewport[0].el;
if (outOfViewport.length) return outOfViewport[0].el; if (outOfViewport.length) return outOfViewport[0].el;
return null; return null;