Implementing Scroll-triggered Popup on Website
A scroll-triggered popup appears when the user has scrolled through a certain percentage of the page. The logic: if a person has read through the middle of an article or product page, they are engaged, and a subscription offer or discount will reach them at the right moment.
Implementation with Intersection Observer
IntersectionObserver is more efficient than scroll events: it does not block the main thread and does not require requestAnimationFrame.
// scroll-popup.ts
interface ScrollPopupConfig {
triggerPercent?: number; // % of page scroll (0-100)
triggerElement?: string; // CSS selector of trigger element
cooldownMs?: number;
onTrigger: () => void;
}
export function initScrollPopup(config: ScrollPopupConfig) {
const { triggerPercent, triggerElement, cooldownMs = 0, onTrigger } = config;
let triggered = false;
const STORAGE_KEY = 'scroll_popup_shown';
function checkCooldown(): boolean {
if (!cooldownMs) return true;
const last = localStorage.getItem(STORAGE_KEY);
if (last && Date.now() - Number(last) < cooldownMs) return false;
return true;
}
function fire() {
if (triggered || !checkCooldown()) return;
triggered = true;
if (cooldownMs) localStorage.setItem(STORAGE_KEY, String(Date.now()));
onTrigger();
}
// Option 1: scroll percentage
if (triggerPercent !== undefined) {
// Create invisible marker element at the required height
const marker = document.createElement('div');
marker.style.cssText = 'position:absolute;top:0;left:0;width:1px;height:1px;pointer-events:none;';
document.body.style.position = 'relative';
document.body.appendChild(marker);
// Position marker at triggerPercent of document height
function updateMarker() {
const docHeight = document.documentElement.scrollHeight;
const viewportHeight = window.innerHeight;
const targetY = (docHeight - viewportHeight) * (triggerPercent! / 100);
marker.style.top = `${targetY}px`;
}
updateMarker();
window.addEventListener('resize', updateMarker, { passive: true });
const observer = new IntersectionObserver(
([entry]) => { if (entry.isIntersecting) fire(); },
{ threshold: 0 }
);
observer.observe(marker);
return () => {
observer.disconnect();
marker.remove();
window.removeEventListener('resize', updateMarker);
};
}
// Option 2: specific DOM element
if (triggerElement) {
const el = document.querySelector(triggerElement);
if (!el) {
console.warn(`[scroll-popup] Element not found: ${triggerElement}`);
return () => {};
}
const observer = new IntersectionObserver(
([entry]) => { if (entry.isIntersecting) fire(); },
{ threshold: 0.5 } // 50% of element visible
);
observer.observe(el);
return () => observer.disconnect();
}
return () => {};
}
Usage in React
// BlogPost.tsx
import { useEffect } from 'react';
import { initScrollPopup } from './scroll-popup';
import { NewsletterPopup } from './NewsletterPopup';
import { useState } from 'react';
export function BlogPost({ content }: { content: string }) {
const [showPopup, setShowPopup] = useState(false);
useEffect(() => {
const cleanup = initScrollPopup({
triggerPercent: 60,
cooldownMs: 7 * 24 * 60 * 60 * 1000, // once a week
onTrigger: () => setShowPopup(true),
});
return cleanup;
}, []);
return (
<>
<article dangerouslySetInnerHTML={{ __html: content }} />
{showPopup && (
<NewsletterPopup onClose={() => setShowPopup(false)} />
)}
</>
);
}
Newsletter Popup
// NewsletterPopup.tsx
import { useRef, useEffect, useState } from 'react';
export function NewsletterPopup({ onClose }: { onClose: () => void }) {
const dialogRef = useRef<HTMLDialogElement>(null);
const [email, setEmail] = useState('');
const [status, setStatus] = useState<'idle' | 'loading' | 'done'>('idle');
useEffect(() => {
dialogRef.current?.showModal();
return () => dialogRef.current?.close();
}, []);
async function subscribe() {
if (!email || status !== 'idle') return;
setStatus('loading');
await fetch('/api/subscribe', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ email, source: 'scroll_popup' }),
});
setStatus('done');
setTimeout(onClose, 2000);
}
return (
<dialog
ref={dialogRef}
className="rounded-2xl p-8 max-w-sm w-full shadow-xl backdrop:bg-black/40"
>
{status === 'done' ? (
<p className="text-center text-green-700 font-medium">You are subscribed!</p>
) : (
<>
<h2 className="text-lg font-bold mb-2">Liked the article?</h2>
<p className="text-sm text-gray-600 mb-4">
Get the best materials once a week. No spam.
</p>
<input
type="email"
value={email}
onChange={e => setEmail(e.target.value)}
onKeyDown={e => e.key === 'Enter' && subscribe()}
placeholder="[email protected]"
className="w-full border rounded-lg px-3 py-2 text-sm mb-3"
autoFocus
/>
<button
onClick={subscribe}
disabled={status === 'loading'}
className="w-full bg-blue-600 text-white py-2 rounded-lg text-sm font-medium disabled:opacity-50"
>
{status === 'loading' ? 'Subscribing...' : 'Subscribe'}
</button>
<button
onClick={onClose}
className="mt-2 w-full text-xs text-gray-400 hover:text-gray-600"
>
No, thanks
</button>
</>
)}
</dialog>
);
}
Timeline
One to two days accounting for responsive design, testing on real devices, and cooldown logic tuning. If you need conversion analytics (GA4 + custom tracking) — add half a day.







