Skip to content

Commit

Permalink
update links
Browse files Browse the repository at this point in the history
  • Loading branch information
QC20 committed Jul 4, 2024
1 parent e807d65 commit 7e008e8
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
3 changes: 3 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,8 @@ <h1>scroll.</h1>
-->
</ul>
<script src="src/javascript/loader.js"></script>
<!--
<script src="src/javascript/scroll.js"></script>
-->
</body>
</html>
54 changes: 54 additions & 0 deletions src/javascript/scroll.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
document.addEventListener('DOMContentLoaded', function() {
const container = document.querySelector('ul');
let isScrolling = false;
let startY;
let startScrollLeft;

function smoothScroll(targetScrollLeft) {
const startScrollLeft = container.scrollLeft;
const distance = targetScrollLeft - startScrollLeft;
const duration = 300; // milliseconds
let start;

function step(timestamp) {
if (!start) start = timestamp;
const elapsed = timestamp - start;
const progress = Math.min(elapsed / duration, 1);
container.scrollLeft = startScrollLeft + distance * easeInOutCubic(progress);

if (progress < 1) {
window.requestAnimationFrame(step);
}
}

window.requestAnimationFrame(step);
}

function easeInOutCubic(t) {
return t < 0.5 ? 4 * t * t * t : (t - 1) * (2 * t - 2) * (2 * t - 2) + 1;
}

container.addEventListener('wheel', function(e) {
e.preventDefault();
const scrollAmount = e.deltaY || e.deltaX;
smoothScroll(container.scrollLeft + scrollAmount);
}, { passive: false });

container.addEventListener('touchstart', function(e) {
isScrolling = true;
startY = e.touches[0].pageY;
startScrollLeft = container.scrollLeft;
}, { passive: false });

container.addEventListener('touchmove', function(e) {
if (!isScrolling) return;

e.preventDefault();
const touchDeltaY = startY - e.touches[0].pageY;
smoothScroll(startScrollLeft + touchDeltaY);
}, { passive: false });

container.addEventListener('touchend', function() {
isScrolling = false;
});
});

0 comments on commit 7e008e8

Please sign in to comment.