jQuery Smooth Scrolling Update
February 14, 2025 Β· AI Conversations
let currentIndex = 0;
const slides = $(".slide");
const totalSlides = slides.length;
let isScrolling = false;
const sliderWrapper = $(".slider-wrapper");
const sliderContainer = $(".slider-container");
function updateSliderHeight() {
let slideHeight = window.innerHeight;
$(".slide").css("height", slideHeight + "px");
}
function scrollToSection(index, updateHash = true) {
if (index >= 0 && index < totalSlides) {
isScrolling = true;
let offset = index * -window.innerHeight;
sliderWrapper.css("transform", `translate(-50%, ${offset}px)`);
slides.removeClass("active");
slides.eq(index).addClass("active");
if (updateHash) {
history.pushState(null, null, `#slide-${index + 1}`);
}
setTimeout(() => isScrolling = false, 800);
}
}
function isSliderInView() {
const rect = sliderContainer[0].getBoundingClientRect();
return rect.top < window.innerHeight && rect.bottom > 0;
}
function preventBodyScroll(event) {
if (isSliderInView()) {
event.preventDefault();
}
}
$(window).on("wheel", function (event) {
if (!isSliderInView()) return;
event.preventDefault();
if (isScrolling) return;
if (event.originalEvent.deltaY > 0) {
currentIndex++;
} else {
currentIndex–;
}
if (currentIndex < 0) currentIndex = 0;
if (currentIndex >= totalSlides) currentIndex = totalSlides – 1;
scrollToSection(currentIndex);
});
window.addEventListener("wheel", preventBodyScroll, { passive: false });
function updateSlideFromHash() {
const hash = window.location.hash;
if (hash.startsWith("#slide-")) {
let index = parseInt(hash.replace("#slide-", "")) – 1;
if (!isNaN(index) && index >= 0 && index < totalSlides) {
currentIndex = index;
scrollToSection(currentIndex, false);
}
}
}
$(window).on("resize", function () {
updateSliderHeight();
scrollToSection(currentIndex, false);
});
updateSliderHeight();
updateSlideFromHash();
});
scroll to the page when slides finished
### Changes:
1. **Allow normal scrolling after the last slide:** If the user scrolls beyond the last slide, remove the `preventDefault()` behavior.
2. **Enable body scrolling when slides are finished:** If the user scrolls past the last slide, let the normal page scroll continue.
### Updated Code:
jQuery(document).ready(function ($) {
let currentIndex = 0;
const slides = $(".slide");
const totalSlides = slides.length;
let isScrolling = false;
const sliderWrapper = $(".slider-wrapper");
const sliderContainer = $(".slider-container");
function updateSliderHeight() {
let slideHeight = window.innerHeight;
slides.css("height", slideHeight + "px");
}
function scrollToSection(index, updateHash = true) {
if (index >= 0 && index < totalSlides) {
isScrolling = true;
let offset = index * -window.innerHeight;
sliderWrapper.css("transform", `translate(-50%, ${offset}px)`);
slides.removeClass("active");
slides.eq(index).addClass("active");
if (updateHash) {
history.pushState(null, null, `#slide-${index + 1}`);
}
setTimeout(() => isScrolling = false, 800);
}
}
function isSliderInView() {
const rect = sliderContainer[0].getBoundingClientRect();
return rect.top < window.innerHeight && rect.bottom > 0;
}
function preventBodyScroll(event) {
if (isSliderInView() && currentIndex < totalSlides - 1) {
event.preventDefault();
}
}
$(window).on("wheel", function (event) {
if (!isSliderInView()) return;
if (isScrolling) return;
if (event.originalEvent.deltaY > 0) {
if (currentIndex < totalSlides - 1) {
event.preventDefault();
currentIndex++;
scrollToSection(currentIndex);
}
} else {
if (currentIndex > 0) {
event.preventDefault();
currentIndex--;
scrollToSection(currentIndex);
}
}
});
window.addEventListener("wheel", preventBodyScroll, { passive: false });
function updateSlideFromHash() {
const hash = window.location.hash;
if (hash.startsWith("#slide-")) {
let index = parseInt(hash.replace("#slide-", "")) - 1;
if (!isNaN(index) && index >= 0 && index < totalSlides) {
currentIndex = index;
scrollToSection(currentIndex, false);
}
}
}
$(window).on("resize", function () {
updateSliderHeight();
scrollToSection(currentIndex, false);
});
updateSliderHeight();
updateSlideFromHash();
});
### Whatβs Changed?
– **If the last slide is reached, default scrolling resumes**:
– `preventDefault()` is only applied when `currentIndex < totalSlides – 1`.
– This allows the user to scroll naturally to the rest of the page when slides are finished.
Now, once the last slide is displayed, the user can scroll down naturally to view the rest of the page. π
1. **Check if the user is inside the `.slider-container`.**
2. **Apply the scroll effect only when hovering over the `.slider-container`.**
3. **Allow normal scrolling elsewhere.**
—
### **Updated Code:**
jQuery(document).ready(function ($) {
let currentIndex = 0;
const slides = $(".slide");
const totalSlides = slides.length;
let isScrolling = false;
const sliderWrapper = $(".slider-wrapper");
const sliderContainer = $(".slider-container");
function updateSliderHeight() {
let slideHeight = window.innerHeight;
slides.css("height", slideHeight + "px");
}
function scrollToSection(index, updateHash = true) {
if (index >= 0 && index < totalSlides) {
isScrolling = true;
let offset = index * -window.innerHeight;
sliderWrapper.css("transform", `translate(-50%, ${offset}px)`);
slides.removeClass("active");
slides.eq(index).addClass("active");
if (updateHash) {
history.pushState(null, null, `#slide-${index + 1}`);
}
setTimeout(() => isScrolling = false, 800);
}
}
function isCursorInsideSlider(event) {
const rect = sliderContainer[0].getBoundingClientRect();
return (
event.clientX >= rect.left &&
event.clientX <= rect.right &&
event.clientY >= rect.top &&
event.clientY <= rect.bottom
);
}
$(window).on("wheel", function (event) {
if (!isCursorInsideSlider(event.originalEvent)) return; // Restrict effect inside `.slider-container`
if (isScrolling) return;
if (event.originalEvent.deltaY > 0) {
if (currentIndex < totalSlides - 1) {
event.preventDefault();
currentIndex++;
scrollToSection(currentIndex);
}
} else {
if (currentIndex > 0) {
event.preventDefault();
currentIndex--;
scrollToSection(currentIndex);
}
}
});
$(window).on("resize", function () {
updateSliderHeight();
scrollToSection(currentIndex, false);
});
updateSliderHeight();
});
—
### **Key Changes:**
β
**Scrolling Effect Works Only Inside `.slider-container`:**
– The function `isCursorInsideSlider(event)` checks if the mouse pointer is within `.slider-container` before applying the scrolling effect.
β
**Rest of the Page Scrolls Normally:**
– If the mouse is **outside** `.slider-container`, normal page scrolling works.
—
### **Now, the scrolling slide effect only works when the user is inside the designated div (`.slider-container`).** π―
—
### **Fixed Code:**
jQuery(document).ready(function ($) {
let currentIndex = 0;
const slides = $(".slide");
const totalSlides = slides.length;
let isScrolling = false;
const sliderWrapper = $(".slider-wrapper");
const sliderContainer = $(".slider-container");
let isMouseOverSlider = false; // Track whether the mouse is over the slider
function updateSliderHeight() {
let slideHeight = window.innerHeight;
slides.css("height", slideHeight + "px");
}
function scrollToSection(index, updateHash = true) {
if (index >= 0 && index < totalSlides) {
isScrolling = true;
let offset = index * -window.innerHeight;
sliderWrapper.css("transform", `translateY(${offset}px)`); // Fixed transform
slides.removeClass("active");
slides.eq(index).addClass("active");
if (updateHash) {
history.pushState(null, null, `#slide-${index + 1}`);
}
setTimeout(() => isScrolling = false, 800);
}
}
// Detect if the cursor is inside the slider
sliderContainer.on("mouseenter", function () {
isMouseOverSlider = true;
});
sliderContainer.on("mouseleave", function () {
isMouseOverSlider = false;
});
$(window).on("wheel", function (event) {
if (!isMouseOverSlider) return; // Only trigger scroll inside `.slider-container`
if (isScrolling) return;
if (event.originalEvent.deltaY > 0) {
if (currentIndex < totalSlides - 1) {
event.preventDefault();
currentIndex++;
scrollToSection(currentIndex);
}
} else {
if (currentIndex > 0) {
event.preventDefault();
currentIndex--;
scrollToSection(currentIndex);
}
}
});
$(window).on("resize", function () {
updateSliderHeight();
scrollToSection(currentIndex, false);
});
updateSliderHeight();
});
—
### **Fixes & Improvements:**
β
**Corrected transform property:**
– Fixed `sliderWrapper.css("transform", `translateY(${offset}px)`)`;
– Your original code used `translate(-50%, ${offset}px)`, which might not be necessary.
β
**Detects if mouse is over `.slider-container`:**
– Added `mouseenter` and `mouseleave` events to track when the user is hovering over the slider.
– Uses `isMouseOverSlider` instead of `event.clientX` checks.
β
**Ensures smooth scrolling within the slider only.**
– When outside `.slider-container`, normal scrolling resumes.
—
### **Now, the scrolling effect works only inside `.slider-container`, and normal page scrolling functions elsewhere.** π
Try this version and let me know if you need further refinements! π―
To fix your issue while ensuring the scrolling works properly, let's restore the original transformation logic but with corrections:
—
### **Fixed Code:**
jQuery(document).ready(function ($) {
let currentIndex = 0;
const slides = $(".slide");
const totalSlides = slides.length;
let isScrolling = false;
const sliderWrapper = $(".slider-wrapper");
const sliderContainer = $(".slider-container");
let isMouseOverSlider = false; // Track whether the mouse is over the slider
function updateSliderHeight() {
let slideHeight = window.innerHeight;
slides.css("height", slideHeight + "px");
}
function scrollToSection(index, updateHash = true) {
if (index >= 0 && index < totalSlides) {
isScrolling = true;
let offset = index * -window.innerHeight;
sliderWrapper.css("transform", `translate(-50%, ${offset}px)`); // Restore original transform
slides.removeClass("active");
slides.eq(index).addClass("active");
if (updateHash) {
history.pushState(null, null, `#slide-${index + 1}`);
}
setTimeout(() => isScrolling = false, 800);
}
}
// Detect if the cursor is inside the slider
sliderContainer.on("mouseenter", function () {
isMouseOverSlider = true;
});
sliderContainer.on("mouseleave", function () {
isMouseOverSlider = false;
});
$(window).on("wheel", function (event) {
if (!isMouseOverSlider) return; // Only trigger scroll inside `.slider-container`
if (isScrolling) return;
if (event.originalEvent.deltaY > 0) {
if (currentIndex < totalSlides - 1) {
event.preventDefault();
currentIndex++;
scrollToSection(currentIndex);
}
} else {
if (currentIndex > 0) {
event.preventDefault();
currentIndex--;
scrollToSection(currentIndex);
}
}
});
$(window).on("resize", function () {
updateSliderHeight();
scrollToSection(currentIndex, false);
});
updateSliderHeight();
});
—
### **Key Fixes:**
β
**Restored `translate(-50%, Ypx)`,** so your images or elements remain correctly aligned.
β
**Ensures the scrolling only happens when hovering over `.slider-container`.**
β
**Fixes issue where the left-side image disappears by maintaining the correct transform positioning.**
Now your full-page slider should work within a specific div while keeping its layout intact. Let me know if you still face issues! π
—
### **Updated Solution:**
1. **Prevent page scrolling only inside `.slider-container`.**
2. **Allow normal scrolling outside.**
3. **Ensure smooth transitions.**
—
### **Fixed Code:**
jQuery(document).ready(function ($) {
let currentIndex = 0;
const slides = $(".slide");
const totalSlides = slides.length;
let isScrolling = false;
const sliderWrapper = $(".slider-wrapper");
const sliderContainer = $(".slider-container");
let isMouseOverSlider = false;
function updateSliderHeight() {
let slideHeight = window.innerHeight;
slides.css("height", slideHeight + "px");
}
function scrollToSection(index, updateHash = true) {
if (index >= 0 && index < totalSlides) {
isScrolling = true;
let offset = index * -window.innerHeight;
sliderWrapper.css("transform", `translate(-50%, ${offset}px)`);
slides.removeClass("active");
slides.eq(index).addClass("active");
if (updateHash) {
history.pushState(null, null, `#slide-${index + 1}`);
}
setTimeout(() => isScrolling = false, 800);
}
}
// Detect if the cursor is inside the slider
sliderContainer.on("mouseenter", function () {
isMouseOverSlider = true;
$("body").css("overflow", "hidden"); // Prevent scrolling when inside slider
});
sliderContainer.on("mouseleave", function () {
isMouseOverSlider = false;
$("body").css("overflow", "auto"); // Allow normal page scrolling
});
$(window).on("wheel", function (event) {
if (!isMouseOverSlider) return; // Only trigger scroll inside `.slider-container`
event.preventDefault(); // Stop page from scrolling
if (isScrolling) return;
if (event.originalEvent.deltaY > 0) {
if (currentIndex < totalSlides - 1) {
currentIndex++;
scrollToSection(currentIndex);
}
} else {
if (currentIndex > 0) {
currentIndex--;
scrollToSection(currentIndex);
}
}
});
$(window).on("resize", function () {
updateSliderHeight();
scrollToSection(currentIndex, false);
});
updateSliderHeight();
});
—
### **Improvements:**
β
**Stops page scrolling inside `.slider-container` using `overflow: hidden`.**
β
**Allows normal scrolling outside the `.slider-container`.**
β
**Ensures smooth slide transitions without affecting the whole page.**
β
**Restores `overflow: auto` when leaving the slider area.**
—
### **How it Works:**
– When your cursor **enters** `.slider-container`, the page scrolling is disabled (`overflow: hidden`).
– When your cursor **leaves**, normal page scrolling is restored (`overflow: auto`).
– The scrolling is now **completely isolated** inside the slider div.
π **Now it should work perfectly without affecting the rest of the page!** Let me know if you need more tweaks! π
—
### **Solution:**
β
**Disable page scroll inside `.slider-container` until the last slide is reached.**
β
**Once the last slide is reached, allow normal page scrolling.**
β
**If scrolling back up from the last slide, re-lock page scrolling.**
—
### **Fixed Code:**
jQuery(document).ready(function ($) {
let currentIndex = 0;
const slides = $(".slide");
const totalSlides = slides.length;
let isScrolling = false;
const sliderWrapper = $(".slider-wrapper");
const sliderContainer = $(".slider-container");
let isMouseOverSlider = false;
let isLastSlide = false;
function updateSliderHeight() {
let slideHeight = window.innerHeight;
slides.css("height", slideHeight + "px");
}
function scrollToSection(index, updateHash = true) {
if (index >= 0 && index < totalSlides) {
isScrolling = true;
let offset = index * -window.innerHeight;
sliderWrapper.css("transform", `translate(-50%, ${offset}px)`);
slides.removeClass("active");
slides.eq(index).addClass("active");
if (updateHash) {
history.pushState(null, null, `#slide-${index + 1}`);
}
setTimeout(() => isScrolling = false, 800);
}
// Enable normal scrolling only when the last slide is reached
if (index === totalSlides - 1) {
$("body").css("overflow", "auto");
isLastSlide = true;
} else {
$("body").css("overflow", "hidden");
isLastSlide = false;
}
}
// Detect if the cursor is inside the slider
sliderContainer.on("mouseenter", function () {
isMouseOverSlider = true;
if (!isLastSlide) {
$("body").css("overflow", "hidden"); // Prevent page scroll inside slider
}
});
sliderContainer.on("mouseleave", function () {
isMouseOverSlider = false;
});
$(window).on("wheel", function (event) {
if (!isMouseOverSlider && !isLastSlide) return; // Allow normal scroll outside slider
event.preventDefault(); // Stop default page scroll
if (isScrolling) return;
if (event.originalEvent.deltaY > 0) {
if (currentIndex < totalSlides - 1) {
currentIndex++;
scrollToSection(currentIndex);
}
} else {
if (currentIndex > 0) {
currentIndex--;
scrollToSection(currentIndex);
}
}
});
$(window).on("resize", function () {
updateSliderHeight();
scrollToSection(currentIndex, false);
});
updateSliderHeight();
});
—
### **How It Works:**
– **Prevents page scrolling** while inside `.slider-container` unless the last slide is reached.
– **Once the last slide is reached**, `overflow: auto` is restored, allowing normal page scrolling.
– **If scrolling back up,** page scrolling is locked again until the user reaches the last slide.
—
π **Now scrolling is locked inside the slider until the last slide is reached, then normal scrolling starts!** Let me know if you need further refinements. π