oaky look, you can not remove that if over there as it is to prevent a specific bug. Instead I added a check on top of the scroll event to let scrolling happen normally but there are problems. First of all the problem that I mentioned in your task which is, scrolling past the next anchor. We must check if the next "free scroll" can scroll past next anchor and if so we prevent it and just scroll to the bottom of current anchor. This way we wait at the bottom of the next anchor and when user scrolls it will play animation scrolling to the next anchor. If this is not implemented user can scroll past "next anchor" freely and problems happen. There are other bugs too but first please fix this as this is important. Here is the new code, your instructions to how to respond is same as before: ```
.scrolling-anchor {
min-height: 100vh;
position: relative;
display: flex;
justify-content: center;
align-items: center;
}
.scrolling-anchor:not(:first-child)::before {
content: "";
position: absolute;
top: -10vh;
left: 0;
width: 100%;
height: 10vh;
background-color: rgba(255, 0, 0, 0.5);
}
.last {
height: 100vh;
}
</style>
<script>
const scrollSection = document.querySelector(".scroll-section");
const anchors = document.querySelectorAll(".scrolling-anchor");
function isScrollableSection() {
const currentAnchor = Array.from(anchors).find(
(anchor) =>
scrollSection.scrollTop >= anchor.offsetTop &&
scrollSection.scrollTop < anchor.offsetTop + anchor.offsetHeight
);
return (
currentAnchor &&
currentAnchor.scrollHeight > scrollSection.clientHeight
);
}
if (
scrollSection !== null &&
anchors !== undefined &&
anchors.length > 0
) {
let handlingScroll = false;
function normalizeDelta(delta) {
return Math.sign(delta);
}
function getClosestAnchorDirection() {
const scrollOffset = scrollSection.scrollTop;
const windowHeight = scrollSection.clientHeight;
let nextAnchorDistance= null;
let anchorOffset= null;
let signOfAnchor= null;
anchors.forEach((anchor) => {
anchorOffset = anchor.offsetTop;
let distanceToAnchor = Math.abs(
anchorOffset - scrollOffset
);
if (nextAnchorDistance == null) {
nextAnchorDistance = distanceToAnchor;
signOfAnchor = normalizeDelta(
anchorOffset - scrollOffset
);
} else if (distanceToAnchor < nextAnchorDistance) {
nextAnchorDistance = distanceToAnchor;
signOfAnchor = normalizeDelta(
anchorOffset - scrollOffset
);
}
});
return signOfAnchor;
}
function getAnchorInDirection(delta) {
const scrollOffset = scrollSection.scrollTop;
const windowHeight = scrollSection.clientHeight;
let nextAnchor= null;
let nextAnchorOffset= null;
let anchorOffset= null;
anchors.forEach((anchor) => {
anchorOffset = anchor.offsetTop;
if (
(delta > 0 && anchorOffset > scrollOffset) ||
(delta < 0 && anchorOffset < scrollOffset)
) {
if (
nextAnchor === null ||
Math.abs(anchorOffset - scrollOffset) <
Math.abs(nextAnchorOffset - scrollOffset)
) {
nextAnchor = anchor;
nextAnchorOffset = anchorOffset;
}
}
});
if (nextAnchor === null) {
return false;
}
return nextAnchorOffset;
}
function scrollToAnchor(offset) {
scrollSection.scrollTo(0, offset);
}
function onMouseWheel(event) {
if (!scrolling && !handlingScroll && isScrollableSection()) {
return;
}
event.preventDefault();
if (!scrolling == true && !handlingScroll == true) {
handlingScroll = true;
const deltaY = normalizeDelta(event.deltaY);
const nextAnchorOffset = getAnchorInDirection(deltaY);
if (nextAnchorOffset !== false) {
scrollSection.scrollTop += deltaY;
} else {
handlingScroll = false;
}
}
}
scrollSection.addEventListener("wheel", onMouseWheel, {
passive: false,
});
// Handle keydown events
const scrollKeys = [
32, 33, 34, 35, 36, 37, 38, 39, 40, 107, 109,
];
function onKeyDown(event) {
if (!scrolling && !handlingScroll && isScrollableSection()) {
return;
}
event.preventDefault();
if (!scrolling == true && !handlingScroll == true) {
if (scrollKeys.includes(event.keyCode)) {
handlingScroll = true;
const deltaY =
event.keyCode === 38 ||
event.keyCode === 107 ||
event.keyCode === 36
? -1
: 1;
const nextAnchorOffset =
getAnchorInDirection(deltaY);
if (nextAnchorOffset !== false) {
scrollSection.scrollTop += deltaY;
} else {
handlingScroll = false;
}
}
}
}
scrollSection.addEventListener("keydown", onKeyDown);
scrollSection.tabIndex = 0;
scrollSection.focus();
function onTouchStart(event) {
if (!scrolling == true && !handlingScroll == true) {
if (event.touches.length === 1) {
startY = event.touches[0].pageY;
}
}
}
function onTouchMove(event) {
if (!scrolling && !handlingScroll && isScrollableSection()) {
return;
}
event.preventDefault();
if (!scrolling == true && !handlingScroll == true) {
if (event.touches.length === 1) {
handlingScroll = true;
const deltaY = startY - event.touches[0].pageY;
const normalizedDelta = normalizeDelta(deltaY);
const nextAnchorOffset =
getAnchorInDirection(normalizedDelta);
if (nextAnchorOffset !== false) {
scrollSection.scrollTop += normalizedDelta;
} else {
handlingScroll = false;
}
}
}
}
let startY;
scrollSection.addEventListener("touchstart", onTouchStart, {
passive: false,
});
scrollSection.addEventListener("touchmove", onTouchMove, {
passive: false,
});
function onGamepadConnected(event) {
const gamepad = event.gamepad;
gamepadLoop(gamepad);
}
let holdingScrollBar = false;
function gamepadLoop(gamepad) {
if (!scrolling && isScrollableSection()) {
return;
}
if (!scrolling == true) {
const axes = gamepad.axes;
const deltaY = axes[1];
if (Math.abs(deltaY) > 0.5) {
const normalizedDelta = normalizeDelta(deltaY);
const nextAnchorOffset =
getAnchorInDirection(normalizedDelta);
if (nextAnchorOffset !== false) {
scrollSection.scrollTop += normalizedDelta;
} else {
handlingScroll = false;
}
}
requestAnimationFrame(() => gamepadLoop(gamepad));
}
}
function clickedOnScrollBar(mouseX) {
if (scrollSection.clientWidth <= mouseX) {
return true;
}
return false;
}
scrollSection.addEventListener("mousedown", (e) => {
if (clickedOnScrollBar(e.clientX)) {
holdingScrollBar = true;
cancelScroll = true;
}
});
scrollSection.addEventListener("mouseup", (e) => {
if (holdingScrollBar) {
scrolling = false;
handlingScroll = false;
oldScroll = scrollSection.scrollTop;
cancelScroll = false;
const normalizedDelta = getClosestAnchorDirection();
scrollSection.scrollTop += normalizedDelta;
holdingScrollBar = false;
}
});
scrollSection.addEventListener(
"gamepadconnected",
onGamepadConnected
);
let lastDirection = 0;
let scrolling = false;
let cancelScroll = false;
let oldScroll = 0;
scrollSection.addEventListener("scroll", (event) => {
if (isScrollableSection()) {
return;
}
event.preventDefault();
if (scrolling) {
const delta =
oldScroll >= scrollSection.scrollTop ? -1 : 1;
if (lastDirection !== 0 && lastDirection !== delta) {
cancelScroll = true;
}
return;
} else {
const animF = (now) => {
const delta =
oldScroll > scrollSection.scrollTop ? -1 : 1;
lastDirection = delta;
const nextAnchorOffset =
getAnchorInDirection(delta);
if (nextAnchorOffset !== null) {
const scrollOffset = scrollSection.scrollTop;
const windowHeight = scrollSection.clientHeight;
const distanceToAnchor = Math.abs(
nextAnchorOffset - scrollOffset
);
const scrollLockDistance = 10; // vh
const scrollLockPixels =
(windowHeight * scrollLockDistance) / 100;
if (distanceToAnchor <= scrollLockPixels) {
scrolling = true;
scrollLastBit(
nextAnchorOffset,
distanceToAnchor,
true,
delta
);
} else {
const freeScrollValue =
distanceToAnchor - scrollLockPixels;
const newScrollOffset =
scrollOffset + delta * freeScrollValue;
scrolling = true;
scrollCloseToAnchor(
newScrollOffset,
freeScrollValue,
false,
() => {
scrollLastBit(
nextAnchorOffset,
scrollLockPixels,
true,
delta
);
}
);
}
}
};
requestAnimationFrame(animF);
}
});
function scrollLastBit(offset, distance, braking, direction) {
offset = Math.round(offset);
distance = Math.round(distance);
const start = scrollSection.scrollTop;
const startTime = performance.now();
const scrollDuration = braking
? distance * 10
: distance * 1;
let endTick = false;
if (offset == scrollSection.scrollTop) {
scrolling = false;
handlingScroll = false;
oldScroll = scrollSection.scrollTop;
cancelScroll = false;
return;
}
let difference = Math.abs(scrollSection.scrollTop - offset);
const tick = (now) => {
if (cancelScroll) {
lastDirection = 0;
cancelScroll = false;
} else {
if (
Math.abs(scrollSection.scrollTop - offset) >
difference
) {
difference = Math.abs(
scrollSection.scrollTop - offset
);
requestAnimationFrame(tick);
} else {
difference = Math.abs(
scrollSection.scrollTop - offset
);
if (endTick) {
if (direction < 0) {
if (offset >= scrollSection.scrollTop) {
scrolling = false;
handlingScroll = false;
cancelScroll = false;
oldScroll = scrollSection.scrollTop;
} else {
requestAnimationFrame(tick);
}
} else {
if (offset <= scrollSection.scrollTop) {
scrolling = false;
handlingScroll = false;
oldScroll = scrollSection.scrollTop;
} else {
requestAnimationFrame(tick);
}
}
} else {
const elapsed = now - startTime;
const fraction = elapsed / scrollDuration;
if (fraction < 1) {
const easeOut = braking
? -Math.pow(2, -10 * fraction) + 1
: fraction;
scrollToAnchor(
start + (offset - start) * easeOut
);
requestAnimationFrame(tick);
} else {
scrollToAnchor(offset);
endTick = true;
requestAnimationFrame(tick);
}
}
}
}
};
requestAnimationFrame(tick);
}
function scrollCloseToAnchor(
offset,
distance,
braking,
callback = null
) {
if (offset == scrollSection.scrollTop) {
scrolling = false;
handlingScroll = false;
oldScroll = scrollSection.scrollTop;
cancelScroll = false;
return;
}
offset = Math.round(offset);
distance = Math.round(distance);
const start = scrollSection.scrollTop;
const startTime = performance.now();
const scrollDuration = braking
? distance * 10
: distance * 1;
let difference = Math.abs(scrollSection.scrollTop - offset);
const tick = (now) => {
if (cancelScroll) {
lastDirection = 0;
cancelScroll = false;
} else {
if (
Math.abs(scrollSection.scrollTop - offset) >
difference
) {
difference = Math.abs(
scrollSection.scrollTop - offset
);
requestAnimationFrame(tick);
} else {
difference = Math.abs(
scrollSection.scrollTop - offset
);
const elapsed = now - startTime;
const fraction = elapsed / scrollDuration;
if (fraction < 1) {
const easeOut = braking
? -Math.pow(2, -10 * fraction) + 1
: fraction;
scrollToAnchor(
start + (offset - start) * easeOut
);
requestAnimationFrame(tick);
} else {
if (callback !== null) callback();
scrollToAnchor(offset);
}
}
}
};
requestAnimationFrame(tick);
}
}
</script>