Scroll Nav Limit Fix
April 14, 2025 ยท AI Conversations
const $overlaySliderGrid = $('.overlay-slider-grid');
const $overlayslider = $('.overlay-slider');
const $mainparent = $overlaySliderGrid.find('.content-overlayslider');
const $hoverItems = $mainparent.find('.hover-item');
let currentIndex = 0; // For tracking the selected item for keyboard navigation or scroll
let xPos = 0; // Initialize xPos
let yPos = 0; // Initialize yPos
// Function to update background and content
const updateSlider = ($item) => {
const newBg = $item.data('bg');
$overlayslider.css('background-image', `url(${newBg})`);
$item.find('.content').addClass('active');
$item.find('.description').addClass('active');
$item.siblings().find('.content, .description').removeClass('active');
};
// Scroll event for hover items (mouse wheel)
$hoverItems.on('wheel', function (e) {
e.preventDefault(); // Prevent default scroll behavior
const delta = e.originalEvent.deltaY;
if (delta > 0) { // Scroll Down
currentIndex = (currentIndex + 1) % $hoverItems.length;
} else if (delta < 0) { // Scroll Up
currentIndex = (currentIndex – 1 + $hoverItems.length) % $hoverItems.length;
}
updateSlider($hoverItems.eq(currentIndex));
});
// Keyboard navigation (up/down arrow keys)
$(document).on('keydown', function (e) {
if (e.key === 'ArrowUp') {
currentIndex = (currentIndex – 1 + $hoverItems.length) % $hoverItems.length;
updateSlider($hoverItems.eq(currentIndex));
} else if (e.key === 'ArrowDown') {
currentIndex = (currentIndex + 1) % $hoverItems.length;
updateSlider($hoverItems.eq(currentIndex));
}
});
// Intersection Observer to detect visibility of the overlay-slider-grid
const observerOptions = {
root: null,
rootMargin: '0px',
threshold: 0.5
};
const observer = new IntersectionObserver((entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
$mainparent.addClass('visible');
$hoverItems.eq(currentIndex).trigger('wheel'); // Trigger initial update
} else {
$mainparent.removeClass('visible');
}
});
}, observerOptions);
observer.observe($overlaySliderGrid[0]);
// Browser scrollbar event to update content
$(window).on('scroll', function () {
const scrollTop = $(window).scrollTop();
const overlayOffset = $overlaySliderGrid.offset().top;
const overlayHeight = $overlaySliderGrid.outerHeight();
if (scrollTop >= overlayOffset && scrollTop
Here's the updated logic in your script:
### โ Replace this part:
if (delta > 0) { // Scroll Down
currentIndex = (currentIndex + 1) % $hoverItems.length;
} else if (delta < 0) { // Scroll Up
currentIndex = (currentIndex - 1 + $hoverItems.length) % $hoverItems.length;
}
### ๐ With this:
if (delta > 0 && currentIndex < $hoverItems.length - 1) {
currentIndex++;
} else if (delta < 0 && currentIndex > 0) {
currentIndex--;
}
### Similarly, update the keyboard navigation:
if (e.key === 'ArrowUp' && currentIndex > 0) {
currentIndex--;
updateSlider($hoverItems.eq(currentIndex));
} else if (e.key === 'ArrowDown' && currentIndex < $hoverItems.length - 1) {
currentIndex++;
updateSlider($hoverItems.eq(currentIndex));
}
### Now your scroll/keyboard nav will:
– Go **up/down** within range
– **Not wrap around** to the beginning or end
– **Stop** when the first or last item is reached
Let me know if you want to add visual feedback (e.g. disable scroll or show a message) when the limits are reached.
—
### โ Fix: Only prevent default when there are more slider items to scroll
Update this block:
$hoverItems.on('wheel', function (e) {
const delta = e.originalEvent.deltaY;
if (delta > 0 && currentIndex < $hoverItems.length - 1) {
e.preventDefault();
currentIndex++;
updateSlider($hoverItems.eq(currentIndex));
} else if (delta < 0 && currentIndex > 0) {
e.preventDefault();
currentIndex--;
updateSlider($hoverItems.eq(currentIndex));
}
// Else, allow default scroll
});
—
### Explanation:
– When you're not at the start or end, prevent scrolling and update slider.
– When you're **already at the last item** and scroll down again, do **not call `preventDefault()`**, so the browser continues scrolling down the page.
—
Let me know if you also want the page to auto-scroll to the next section when slider ends โ that can be added too.