Skip to content

Commit 5eb1b76

Browse files
authored
docs(demos): keep tour spotlight on its target while scrolling (#162)
1 parent b2c48dc commit 5eb1b76

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

‎docs/demos/engine.js‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,29 @@ window.HttpwareDemo = (function () {
228228
els.ring.classList.add('show');
229229
return { x0, y0, x1, y1 };
230230
}
231+
let spotNodes = null, frame = 0;
232+
function reposition() {
233+
frame = 0;
234+
if (spotNodes) placeCoach(spotlight(spotNodes));
235+
}
236+
function scheduleReposition() {
237+
if (!frame) frame = requestAnimationFrame(reposition);
238+
}
239+
function follow(nodes) {
240+
if (!spotNodes) {
241+
window.addEventListener('scroll', scheduleReposition, { capture: true, passive: true });
242+
window.addEventListener('resize', scheduleReposition, { passive: true });
243+
}
244+
spotNodes = nodes;
245+
}
246+
function unfollow() {
247+
window.removeEventListener('scroll', scheduleReposition, { capture: true });
248+
window.removeEventListener('resize', scheduleReposition);
249+
cancelAnimationFrame(frame);
250+
spotNodes = null; frame = 0;
251+
}
231252
function hideSpot() {
253+
unfollow();
232254
[els.dimT, els.dimB, els.dimL, els.dimR, els.ring].forEach((n) => n.classList.remove('show'));
233255
}
234256
function placeCoach(hole) {
@@ -246,6 +268,7 @@ window.HttpwareDemo = (function () {
246268
top = Math.min(Math.max(10, top), H - ch - 10);
247269
c.style.left = left + 'px'; c.style.top = top + 'px';
248270
const a = els.cArrow;
271+
a.style.borderTop = a.style.borderRight = a.style.borderBottom = a.style.borderLeft = '';
249272
a.style.transform = 'rotate(45deg)';
250273
if (side === 'right') { a.style.left = '-7px'; a.style.top = (cy - top - 6) + 'px'; a.style.borderTop = 'none'; a.style.borderRight = 'none'; a.style.borderLeft = '1px solid var(--hw-line)'; a.style.borderBottom = '1px solid var(--hw-line)'; }
251274
else if (side === 'left') { a.style.left = (cw - 7) + 'px'; a.style.top = (cy - top - 6) + 'px'; a.style.borderBottom = 'none'; a.style.borderLeft = 'none'; a.style.borderTop = '1px solid var(--hw-line)'; a.style.borderRight = '1px solid var(--hw-line)'; }
@@ -265,6 +288,7 @@ window.HttpwareDemo = (function () {
265288
els.cTitle.textContent = stop.title;
266289
els.cBody.textContent = stop.body;
267290
placeCoach(hole);
291+
follow(nodes);
268292
},
269293
hideAll() { els.coach.classList.remove('show'); hideSpot(); },
270294
setContinue(fn) { onContinue = fn; },

0 commit comments

Comments
 (0)