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:
+1
-1
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,14 +1941,20 @@
|
|||||||
// 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;
|
||||||
}
|
}
|
||||||
outOfViewport.push({
|
|
||||||
el: this,
|
// Only include if within LOAD_DISTANCE
|
||||||
distance: distance
|
if (distance <= maxDistance) {
|
||||||
});
|
outOfViewport.push({
|
||||||
|
el: this,
|
||||||
|
distance: distance
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -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;
|
||||||
|
|||||||
Reference in New Issue
Block a user