الكود الكامل (HTML + CSS + JS)
<!DOCTYPE html>
<html lang="ar" dir="rtl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>تحول زر القائمة إلى إغلاق — Hamburger-to-Close Morph Icon</title>
<style>
body { margin: 0; background: #0d0e11; color: #f3f4f6; font-family: system-ui, sans-serif; }
:root { --font-mono: ui-monospace, Menlo, Consolas, monospace; }
#stage { position: relative; width: 100%; height: 220px; box-sizing: border-box; padding: 16px; display: flex; align-items: center; justify-content: center; overflow: hidden; background: #090a0d; }
</style>
</head>
<body>
<div id="stage"></div>
<script>
function play(stage) {
const fx = { id: 62, mode: "l_menu" };
const timers = [], rafs = [], anims = [], listeners = [];
const regTimer = (t) => { timers.push(t); return t; };
const regRaf = (r) => { rafs.push(r); return r; };
const regAnim = (a) => { anims.push(a); return a; };
const regListen = (el, ev, fn) => { el.addEventListener(ev, fn); listeners.push([el, ev, fn]); };
const accent = '#ff5e1a', white = '#f3f4f6', muted = '#a1a8b3', surface = '#1e2129';
const svgEl = (tag, attrs = {}) => {
const el = document.createElementNS('http://www.w3.org/2000/svg', tag);
Object.entries(attrs).forEach(([k, v]) => el.setAttribute(k, v));
return el;
};
switch (fx.mode) {
case 'l_menu': {
const btn = document.createElement('button');
btn.type = 'button';
btn.setAttribute('aria-label', 'تبديل أيقونة القائمة');
btn.style.cssText = 'background:' + surface + ';border:1px solid #353946;border-radius:10px;width:72px;height:72px;cursor:pointer;display:grid;place-items:center;';
const svg = svgEl('svg', { viewBox: '0 0 40 40', width: '38', height: '38' });
const l1 = svgEl('line', { x1: 8, y1: 12, x2: 32, y2: 12, stroke: white, 'stroke-width': '3', 'stroke-linecap': 'round' });
const l2 = svgEl('line', { x1: 8, y1: 20, x2: 32, y2: 20, stroke: accent, 'stroke-width': '3', 'stroke-linecap': 'round' });
const l3 = svgEl('line', { x1: 8, y1: 28, x2: 32, y2: 28, stroke: white, 'stroke-width': '3', 'stroke-linecap': 'round' });
[l1, l2, l3].forEach(l => { l.style.transformBox = 'fill-box'; l.style.transformOrigin = 'center'; });
svg.append(l1, l2, l3); btn.appendChild(svg); stage.appendChild(btn);
const morph = (open) => {
regAnim(l1.animate([{ transform: open ? 'translateY(8px) rotate(45deg)' : 'none' }], { duration: 350, fill: 'forwards', easing: 'cubic-bezier(0.34,1.56,0.64,1)' }));
regAnim(l2.animate([{ opacity: open ? 0 : 1, transform: open ? 'scaleX(0)' : 'scaleX(1)' }], { duration: 250, fill: 'forwards' }));
regAnim(l3.animate([{ transform: open ? 'translateY(-8px) rotate(-45deg)' : 'none' }], { duration: 350, fill: 'forwards', easing: 'cubic-bezier(0.34,1.56,0.64,1)' }));
};
let isOpen = true; morph(true);
regListen(btn, 'click', () => { isOpen = !isOpen; morph(isOpen); });
break;
}
}
return () => {
timers.forEach(clearTimeout);
rafs.forEach(cancelAnimationFrame);
anims.forEach(a => { try { a.cancel(); } catch (_) {} });
listeners.forEach(([el, ev, fn]) => el.removeEventListener(ev, fn));
};
}
const MS = 0, PAUSE = 1500; // MS = 0: the effect already runs continuously
const host = document.getElementById('stage');
const still = matchMedia('(prefers-reduced-motion: reduce)').matches;
let stop = null, timer = null, shown = false;
function run() { if (stop) stop(); host.innerHTML = ""; stop = play(host); }
function loop() { run(); if (!still && MS) timer = setTimeout(loop, MS + PAUSE); }
new IntersectionObserver(([e]) => {
if (e.isIntersecting && !shown) { shown = true; loop(); }
else if (!e.isIntersecting && shown) { shown = false; clearTimeout(timer); if (stop) stop(); }
}, { threshold: 0.1 }).observe(host);
</script>
</body>
</html>