Skip to content

Commit 632bac5

Browse files
committed
Calibrated
1 parent 69f7bed commit 632bac5

2 files changed

Lines changed: 18 additions & 17 deletions

File tree

index.html

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2549,36 +2549,31 @@ <h2>
25492549
<script>
25502550
(function initMarquees() {
25512551
var SCROLL_DELAY = 85;
2552-
document.querySelectorAll('.marquee').forEach(function (el) {
2552+
2553+
function calibrate(el, inner) {
25532554
var scrollAmount = parseInt(el.getAttribute('scrollamount')) || 6;
2554-
var pxPerMs = scrollAmount / SCROLL_DELAY;
2555+
var duration = inner.offsetWidth * SCROLL_DELAY / scrollAmount;
2556+
inner.style.animationDuration = duration + 'ms';
2557+
}
25552558

2559+
document.querySelectorAll('.marquee').forEach(function (el) {
25562560
var inner = document.createElement('span');
25572561
inner.className = 'marquee-inner';
25582562
while (el.firstChild) inner.appendChild(el.firstChild);
25592563
el.appendChild(inner);
25602564

2565+
calibrate(el, inner);
2566+
25612567
var observer = new MutationObserver(function (mutations) {
2568+
var moved = false;
25622569
mutations.forEach(function (m) {
25632570
Array.from(m.addedNodes).forEach(function (node) {
2564-
if (node !== inner) inner.appendChild(node);
2571+
if (node !== inner) { inner.appendChild(node); moved = true; }
25652572
});
25662573
});
2574+
if (moved) calibrate(el, inner);
25672575
});
25682576
observer.observe(el, { childList: true });
2569-
2570-
var pos = el.offsetWidth;
2571-
var last = null;
2572-
function step(ts) {
2573-
if (last === null) last = ts;
2574-
var dt = ts - last;
2575-
last = ts;
2576-
pos -= pxPerMs * dt;
2577-
if (pos < -inner.offsetWidth) pos = el.offsetWidth;
2578-
inner.style.transform = 'translateX(' + pos + 'px)';
2579-
requestAnimationFrame(step);
2580-
}
2581-
requestAnimationFrame(step);
25822577
});
25832578
})();
25842579

stylesheets/base.css

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,5 +162,11 @@ input:focus + label {
162162

163163
.marquee-inner {
164164
display: inline-block;
165-
will-change: transform;
165+
padding-left: 100%;
166+
animation: marquee-scroll linear infinite;
167+
}
168+
169+
@keyframes marquee-scroll {
170+
from { transform: translateX(0); }
171+
to { transform: translateX(-100%); }
166172
}

0 commit comments

Comments
 (0)