-
Notifications
You must be signed in to change notification settings - Fork 0
/
card-scrol.js
57 lines (45 loc) · 2 KB
/
card-scrol.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
document.addEventListener("DOMContentLoaded", function () {
const lineHeight = 20; // Adjust this value to match your line height
const linesPerScroll = 1; // Number of lines to scroll per scroll event
let scrolling = false; // Track if scrolling is already in progress
const cardTextElements = document.querySelectorAll(".card-text");
cardTextElements.forEach(function (element) {
const cardText = element.textContent.trim();
const linesToShow = Math.ceil(cardText.length / lineHeight);
if (linesToShow > linesPerScroll) {
element.classList.add("scrollable");
element.addEventListener("wheel", function (event) {
if (!scrolling) { // Only trigger the scroll if not currently scrolling
scrolling = true;
const direction = event.deltaY > 0 ? 1 : -1;
smoothScroll(element, direction * linesPerScroll * lineHeight);
event.preventDefault();
// Reset scrolling state after a brief delay
setTimeout(() => {
scrolling = false;
}, 300);
}
});
}
});
});
function smoothScroll(element, amount) {
const duration = 50; // Duration of the smooth scroll in milliseconds
const startTime = performance.now();
const startPosition = element.scrollTop;
const targetPosition = startPosition + amount;
function scrollFrame(timestamp) {
const elapsed = timestamp - startTime;
const progress = Math.min(elapsed / duration, 1);
const easedProgress = easeInOutCubic(progress);
const newPosition = startPosition + (targetPosition - startPosition) * easedProgress;
element.scrollTop = newPosition;
if (progress < 1) {
requestAnimationFrame(scrollFrame);
}
}
requestAnimationFrame(scrollFrame);
}
function easeInOutCubic(t) {
return t < 0.5 ? 4 * t * t * t : (t - 1) * (2 * t - 2) * (2 * t - 2) + 1;
}