---
import Compatibility from "~/components/compatibility.astro";
import Features from "~/components/features.astro";
import Footer from "~/components/footer.astro";
import Header from "~/components/header.astro";
import Intro from "~/components/intro.astro";
import Showcase from "~/components/showcase.astro";
import Splash from "~/components/splash.astro";
export interface Props {
title: string;
description: string;
author: string;
image: URL;
}
import SiteMeta from "../components/SiteMeta.astro";
const { generator, site } = Astro;
const { title, description, image, author } = Astro.props;
---
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width" />
<meta name="generator" content={Astro.generator} />
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
<title>{title}</title>
<meta name="generator" content={generator} />
<meta name="description" content={description} />
<!-- social media -->
<meta property="og:title" content="Astro" />
<meta property="og:type" content="website" />
<meta property="og:description" content={description} />
<meta property="og:image" content={image} />
<meta property="og:url" content={site} />
<meta name="twitter:card" content="summary_large_image" />
<SiteMeta
title={title}
description={description.substring(0, 100)}
url={Astro.site
? `${Astro.site}/${title.toLowerCase().replaceAll(" ", "-")}`
: `https://accessible-astro.dev/${title
.toLowerCase()
.replaceAll(" ", "-")}`}
image={image}
author={author}
/>
</head>
<body
class="h-full bg-default text-default text-base selection:bg-secondary selection:text-white"
>
<div class="scroll-section header-scroll-ref">
<Header />
<section class="scrolling-anchor scrolling-top-anchor flex">
<Splash />
</section>
<section>
<div class="space-y-24 px-8 pt-32">
<section class="scrolling-anchor h-screen flex">
<Intro />
</section>
<section class="scrolling-anchor h-screen flex">
<Features />
</section>
<section class="scrolling-anchor h-screen flex">
<Showcase />
</section>
<section class="scrolling-anchor h-screen flex">
<Compatibility />
</section>
</div>
</section>
</div>
<Footer />
<style>
.scroll-section {
height: 100vh;
width: 100%;
overflow-y: scroll;
position: relative;
}
.scrolling-anchor {
position: relative;
display: flex;
justify-content: center;
align-items: center;
}
.scrolling-anchor:not(.scrolling-top-anchor)::before {
content: "";
position: absolute;
top: -10vh;
left: 0;
width: 100%;
height: 10vh;
background-color: rgba(255, 0, 0, 0.5);
}
.scroll-section.shake {
animation: shake 0.5s ease-in-out;
}
@keyframes shake {
0%,
100% {
transform: translateX(0);
}
25%,
75% {
transform: translateX(-10px);
}
50% {
transform: translateX(10px);
}
}
@keyframes float {
0% {
transform: translate3d(0, 0, 0);
}
100% {
transform: translate3d(0, 30px, 0);
}
}
</style>
<!-- initialize theme -->
<script is:inline>
const themeSaved = localStorage.getItem("theme");
if (themeSaved) {
document.documentElement.dataset.theme = themeSaved;
} else {
const prefersDark = window.matchMedia(
"(prefers-color-scheme: dark)"
).matches;
document.documentElement.dataset.theme = prefersDark
? "dark"
: "light";
}
window
.matchMedia("(prefers-color-scheme: dark)")
.addEventListener("change", (event) => {
if (!localStorage.getItem("theme")) {
document.documentElement.dataset.theme = event.matches
? "dark"
: "light";
}
});
</script>
<script>
/*
setTimeout(() => {
scrollSection.classList.add("shake");
setTimeout(() => {
scrollSection.classList.remove("shake");
}, 500);
}, 1500);*/
const scrollSection = document.querySelector(".scroll-section");
const anchors = document.querySelectorAll(".scrolling-anchor");
if (
scrollSection !== null &&
anchors !== undefined &&
anchors.length > 0
) {
let handlingScroll = false;
function normalizeDelta(delta: number) {
return Math.sign(delta);
}
function getClosestAnchorDirection() {
const scrollOffset = scrollSection.scrollTop;
const windowHeight = scrollSection.clientHeight;
let nextAnchorDistance: number | null = null;
let anchorOffset: number | null = null;
let signOfAnchor: number | null = 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: HTMLElement | null = null;
let nextAnchorOffset: number | null = null;
let anchorOffset: number | null = 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: number) {
scrollSection.scrollTo(0, offset);
}
function onMouseWheel(event) {
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) {
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) {
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 == true && !handlingScroll == true) {
handlingScroll = 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) => {
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>
</body>
</html>
I understand your requirements and I have a solution for you that allows scrolling within the section and prevents scrolling into the next anchor section. Follow the steps below to integrate the solution into your existing code.
- First, we need to modify the
normalizeDelta() function to return the actual value of delta instead of just its sign. This will help us in determining how much scroll can occur within an anchor section without going into the next one.
function normalizeDelta(delta: number) {
return delta;
}
- Now we will add a new function called
canScroll() that will determine if we can scroll by the given delta within the current anchor section. It will return a Boolean value indicating whether we can scroll or not.
function canScroll(delta: number) {
const currentScrollTop = scrollSection.scrollTop;
for (const anchor of anchors) {
const anchorTop = anchor.offsetTop;
const anchorBottom = anchorTop + anchor.offsetHeight;
if (currentScrollTop >= anchorTop && currentScrollTop <= anchorBottom) {
const nextScrollTop = currentScrollTop + delta;
if (nextScrollTop < anchorTop) return false;
if (nextScrollTop + scrollSection.clientHeight > anchorBottom) return false;
return true;
}
}
return false;
}
- Modify the
onMouseWheel() function to call the canScroll() function and only allow scrolling if it returns true.
function onMouseWheel(event) {
event.preventDefault();
if (!scrolling == true && !handlingScroll == true) {
handlingScroll = true;
const deltaY = normalizeDelta(event.deltaY);
if (canScroll(deltaY)) {
scrollSection.scrollTop += deltaY;
} else {
const nextAnchorOffset = getAnchorInDirection(deltaY);
if (nextAnchorOffset !== false) {
scrollSection.scrollTop += deltaY;
}
}
handlingScroll = false;
}
}
- Similarly, modify the
onKeyDown() function to call the canScroll() function and only allow scrolling if it returns true.
function onKeyDown(event) {
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;
if (canScroll(deltaY * window.innerHeight * 0.1)) {
scrollSection.scrollTop += deltaY * windowHeight * 0.1;
} else {
const nextAnchorOffset = getAnchorInDirection(deltaY);
if (nextAnchorOffset !== false) {
scrollSection.scrollTop += deltaY;
}
}
handlingScroll = false;
}
}
}
- Lastly, update the
onTouchMove() function to call the canScroll() function and only allow scrolling if it returns true.
function onTouchMove(event) {
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);
if (canScroll(normalizedDelta)) {
scrollSection.scrollTop += normalizedDelta;
} else {
const nextAnchorOffset = getAnchorInDirection(normalizedDelta);
if (nextAnchorOffset !== false) {
scrollSection.scrollTop += normalizedDelta;
}
}
handlingScroll = false;
}
}
}
After making these changes, your code should now allow scrolling within an anchor section without going into the next one. I hope this solution solves your problem. If you have any questions or need further clarification, please let me know.