Overlay Slider Functionality Update
January 20, 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
// 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');
$this.css({
'will-change': 'transform',
'transform': `translate3d(${(xPos – 50) * 0.1}px, ${(yPos – 50) * 0.1}px, 0) scale3d(1, 1, 1) rotateX(0deg) rotateY(0deg) rotateZ(0deg) skew(0deg, 0deg)`,
'transform-style': 'preserve-3d'
});
};
// Scroll event for hover items
$hoverItems.on('wheel', function () {
updateSlider($(this));
});
// 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
if ($('.overlay-slider-grid').length) {
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
// 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');
$item.css({
'will-change': 'transform',
'transform': `translate3d(${(xPos - 50) * 0.1}px, ${(yPos - 50) * 0.1}px, 0) scale3d(1, 1, 1) rotateX(0deg) rotateY(0deg) rotateZ(0deg) skew(0deg, 0deg)`,
'transform-style': 'preserve-3d'
});
};
// Scroll event for hover items
$hoverItems.on('wheel', function () {
updateSlider($(this));
});
// Mouse enter event for hover items
$hoverItems.on('mouseenter', function () {
updateSlider($(this));
});
// 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 <= overlayOffset + overlayHeight) {
const relativeScroll = (scrollTop - overlayOffset) / overlayHeight; // 0 to 1 range
const newIndex = Math.floor(relativeScroll * $hoverItems.length);
if (newIndex !== currentIndex) {
currentIndex = newIndex;
updateSlider($hoverItems.eq(currentIndex));
}
}
});
// Optional: Add mouseleave event to reset to initial state
$hoverItems.on('mouseleave', function () {
// Optionally reset to default slide or clear active classes
const defaultItem = $hoverItems.eq(currentIndex);
updateSlider(defaultItem);
});
}
### Key Additions:
1. **Mouseenter Event:** Added a mouseenter event on `$hoverItems` to update the slider whenever a user hovers over an item.
2. **Mouseleave Event:** Optionally added a mouseleave event to reset to the current item when the mouse leaves a hover item.
### Adjustments:
– Ensure you adjust the `updateSlider` function if you want to customize the animation or effects that should happen when an item is hovered over or focused on.
– You may also want to manage the transformation or animation effects depending on how you want the slide interaction to behave.
Here's an updated version of the code that initializes `xPos` and `yPos` based on the mouse events:
if ($('.overlay-slider-grid').length) {
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');
$item.css({
'will-change': 'transform',
'transform': `translate3d(${(xPos - 50) * 0.1}px, ${(yPos - 50) * 0.1}px, 0) scale3d(1, 1, 1) rotateX(0deg) rotateY(0deg) rotateZ(0deg) skew(0deg, 0deg)`,
'transform-style': 'preserve-3d'
});
};
// Scroll event for hover items
$hoverItems.on('wheel', function () {
updateSlider($(this));
});
// Mouse enter event for hover items
$hoverItems.on('mouseenter', function (event) {
// Update xPos and yPos based on mouse position
xPos = event.clientX; // Get mouse X position
yPos = event.clientY; // Get mouse Y position
updateSlider($(this));
});
// 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 <= overlayOffset + overlayHeight) {
const relativeScroll = (scrollTop - overlayOffset) / overlayHeight; // 0 to 1 range
const newIndex = Math.floor(relativeScroll * $hoverItems.length);
if (newIndex !== currentIndex) {
currentIndex = newIndex;
updateSlider($hoverItems.eq(currentIndex));
}
}
});
// Optional: Add mouseleave event to reset to initial state
$hoverItems.on('mouseleave', function () {
// Optionally reset to default slide or clear active classes
const defaultItem = $hoverItems.eq(currentIndex);
updateSlider(defaultItem);
});
}
### Key Changes:
1. **Initialization of `xPos` and `yPos`:** These variables are initialized to `0` at the beginning.
2. **Mouse Position Capture:** Inside the mouseenter event handler, the `xPos` and `yPos` are updated using `event.clientX` and `event.clientY` to get the current mouse position when the user hovers over an item.
This should resolve the `ReferenceError` and allow the transformation to work based on the mouse position when hovering over the items.