Магнитный прожектор

Мягкий свет следует за курсором по тёмному лендингу. Подведите курсор к главной кнопке, и свет отпускает вас, перескакивает на кнопку и заряжает её, пока она не вспыхнет.

Поводите курсором по странице, потом подведите его к кнопке. На телефоне коснитесь в любом месте, и свет пойдёт за пальцем.

Скачать

Что можно поменять

Поменяйте число, сохраните файл и обновите страницу, разница будет видна сразу.

STIFF0.04

Как сильно свет тянется к цели. 0.1 резко, 0.02 лениво плывёт следом.

DAMP0.76

Сколько скорости свет сохраняет каждый кадр. При 0.9 он перелетает и покачивается вокруг кнопки.

grab150

На каком расстоянии от кнопки в пикселях свет захватывается. При 250 он ловит посетителя издалека.

Как поставить на свой сайт

Поставьте data-spotlight на секцию с предложением и data-spotlight-cta на ту самую главную кнопку. Текст внутри секции остаётся приглушённым, пока по нему не пройдёт свет.

Комментарии в коде на английском, так файл понятен любому разработчику.

  1. CSS

    Вставьте в свой файл стилей или в тег <style> внутри <head>.

    /* Magnetic Spotlight by @codinvisual · t.me/CodinVisual · free for your own site, don't resell this file */
    [data-spotlight] { position: relative; overflow: hidden; isolation: isolate; min-height: 100vh; box-sizing: border-box;
      padding: 48px 20px; background: #07080B; color: #2A2F39; --x: 30%; --y: 30%; --rx: 150px; --ry: 150px; }
    [data-spotlight] .dim-2 { color: #1F232B; }
    .spot-lit {
      position: absolute; inset: 0; z-index: 1;
      padding: inherit; box-sizing: border-box;
      color: #F3F5F8; background: #0B0D12;
      pointer-events: none;
      mask-image: radial-gradient(
        var(--rx) var(--ry) at var(--x) var(--y),
        #000 0,
        rgba(0,0,0,.75) 45%,
        transparent 100%);
    }
    .spot-lit .dim-2 { color: #A5AEBC; }
    .spot-lit [data-spotlight-cta] { visibility: hidden; }
    .spot-glow { position: absolute; left: 0; top: 0; width: 360px; height: 360px; margin: -180px 0 0 -180px; border-radius: 50%; pointer-events: none;
      background: radial-gradient(closest-side, rgba(255,255,255,.16), rgba(255,255,255,.08) 45%, transparent); mix-blend-mode: screen; z-index: 2; }
    .spot-content { position: relative; max-width: 560px; margin: 0 auto; text-align: center; }
    
    .hero h1 { margin: 28px 0 16px; font-size: clamp(40px, 9vw, 64px); line-height: 1.05; letter-spacing: -.03em; font-weight: 800; }
    .hero p { margin: 0 auto 36px; max-width: 360px; font-size: 17px; line-height: 1.4; }
    .hero small { display: block; margin-top: 18px; font-size: 13.5px; }
    .eyebrow { display: inline-block; padding: 6px 14px; border-radius: 99px; box-shadow: inset 0 0 0 1px rgba(255,255,255,.1); font-size: 13.5px; font-weight: 600; }
    
    [data-spotlight-cta] { --charge: 0; position: relative; z-index: 3; width: 232px; height: 58px; border: 0; border-radius: 29px; cursor: pointer;
      background: #11141A; color: #8A93A3; font: 750 19px system-ui, sans-serif;
      box-shadow: inset 0 0 0 1.2px rgba(255,255,255,.13), 0 0 calc(80px * var(--charge)) rgba(108,235,217, calc(.45 * var(--charge))); }
    [data-spotlight-cta]::before { content: ""; position: absolute; inset: 0; border-radius: inherit; background: linear-gradient(#9FF5E7, #4FD6C2);
      opacity: clamp(0, calc((var(--charge) - .15) * 1.18), 1); }
    [data-spotlight-cta] span { position: relative; } [data-spotlight-cta] { transition: transform .16s ease-out; }
    [data-spotlight-cta] svg.ring { position: absolute; inset: 0; width: 100%; height: 100%; overflow: visible; pointer-events: none; }
    [data-spotlight-cta] .ring rect { fill: none; stroke: #6CEBD9; stroke-width: 2.4; filter: drop-shadow(0 0 6px #6CEBD9);
      stroke-dasharray: calc(var(--charge) * 100) 100; }
    [data-spotlight-cta].caught { color: #C9D2DE; } [data-spotlight-cta].lit { color: #06221F; }
    [data-spotlight-cta].full { color: #06221F; animation: cta-breathe 1.7s ease-in-out 1; }
    [data-spotlight-cta].full .ring { display: none; }
    [data-spotlight-cta].full::after { content: ""; position: absolute; inset: 0; border-radius: inherit; background: #fff; opacity: 0; animation: cta-flash .5s ease-out; }
    [data-spotlight-cta] .shock { position: absolute; inset: 0; border-radius: inherit; box-shadow: 0 0 0 2px #BFF7EE; opacity: 0; pointer-events: none; }
    [data-spotlight-cta].full .shock { animation: cta-shock .7s cubic-bezier(.22, 1, .36, 1); }
    [data-spotlight-cta].done { cursor: default; }
    [data-spotlight-cta] .ok { display: none; }
    [data-spotlight-cta].done .ok { display: inline; }
    [data-spotlight-cta].done .go { display: none; }
    [data-spotlight-cta]:active { transform: scale(.955); }
    @keyframes cta-flash { from { opacity: .85; } to { opacity: 0; } }
    @keyframes cta-shock { from { opacity: .7; transform: scale(1); } to { opacity: 0; transform: scale(1.5, 2.6); } }
    @keyframes cta-breathe { 25%, 75% { transform: scale(1.035); } 50% { transform: scale(1); } }
    
    .cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 10px; max-width: 560px; margin: 56px auto 0; text-align: left; }
    .cards div { padding: 20px; border-radius: 18px; box-shadow: inset 0 0 0 1px rgba(255,255,255,.06); }
    .cards b { display: block; margin-bottom: 6px; font-size: 18px; }
    .cards span { font-size: 14.5px; line-height: 1.4; }
    
    @media (prefers-reduced-motion: reduce) {
      [data-spotlight-cta].full, [data-spotlight-cta].full::after, [data-spotlight-cta].full .shock { animation: none; }
    }
  2. HTML

    Поставьте в то место страницы, где должен быть эффект.

    <!-- spotlight section starts -->
    <section data-spotlight>
      <div class="spot-content hero">
        <span class="eyebrow dim-2">New · Motion kit 2.0</span>
        <h1>Pages people<br>remember.</h1>
        <p class="dim-2">Motion that walks every visitor straight to the one button that matters.</p>
        <button data-spotlight-cta type="button"><span class="go">Get started →</span><span class="ok">✓ You’re in</span></button>
        <small class="dim-2">Free for 14 days · No card needed</small>
        <div class="cards">
          <div><b>Scroll scenes</b><span class="dim-2">Sections that play as you read.</span></div>
          <div><b>Cursor light</b><span class="dim-2">The page looks where you look.</span></div>
          <div><b>One file</b><span class="dim-2">No libraries, drop it in.</span></div>
          <div><b>Any stack</b><span class="dim-2">Tilda, Webflow, WordPress, React.</span></div>
        </div>
      </div>
    </section>
    <!-- spotlight section ends -->
  3. JavaScript

    Вставьте перед закрывающим тегом </body>, внутри <script></script>.

    /* Magnetic Spotlight by @codinvisual · t.me/CodinVisual · free for your own site, don't resell this file */
    (() => {
    const STIFF = 0.04, DAMP = 0.76; let grab = 150;
    
    const root = document.querySelector('[data-spotlight]');
    const cta = root.querySelector('[data-spotlight-cta]');
    const calm = matchMedia('(prefers-reduced-motion: reduce)').matches;
    
    const lit = root.querySelector('.spot-content').cloneNode(true);
    const litWrap = document.createElement('div');
    litWrap.className = 'spot-lit'; litWrap.setAttribute('aria-hidden', 'true'); litWrap.inert = true;
    litWrap.append(lit); root.append(litWrap);
    const glow = document.createElement('div');
    glow.className = 'spot-glow'; root.append(glow);
    cta.insertAdjacentHTML('beforeend', '<svg class="ring"><rect x="1" y="1" rx="28" pathLength="100"/></svg><i class="shock"></i>');
    const ring = cta.querySelector('.ring rect');
    
    const dist = (a, b) => Math.hypot(a.x - b.x, a.y - b.y);
    const box = root.getBoundingClientRect();
    const mouse = { x: box.width * .2, y: 40 }, btn = { x: 0, y: 0 };
    let x = mouse.x, y = mouse.y, vx = 0, vy = 0, caught = false, charge = 0, full = false;
    
    function place(e) {
    const r = root.getBoundingClientRect();
    mouse.x = e.clientX - r.left; mouse.y = e.clientY - r.top;
    }
    root.addEventListener('pointermove', place);
    root.addEventListener('pointerdown', place);
    
    function measure() {
    const r = root.getBoundingClientRect(), b = cta.getBoundingClientRect();
    btn.x = b.left - r.left + b.width / 2; btn.y = b.top - r.top + b.height / 2;
    ring.setAttribute('width', b.width - 2); ring.setAttribute('height', b.height - 2);
    }
    
    function paint(x, y, charge) {
    const home = caught ? Math.max(0, 1 - dist({ x, y }, btn) / 140) : 0;
    const speed = Math.min(Math.hypot(vx, vy) / 14, .7);
    const sx = 1 + speed * Math.abs(Math.cos(Math.atan2(vy, vx))), sy = 1 + speed * Math.abs(Math.sin(Math.atan2(vy, vx)));
    const r = 138 - 26 * home;
    litWrap.style.setProperty('--x', x + 'px'); litWrap.style.setProperty('--y', y + 'px');
    litWrap.style.setProperty('--rx', r * (sx + .55 * home) + 'px'); litWrap.style.setProperty('--ry', r * (sy - .22 * home) + 'px');
    glow.style.transform = `translate(${x}px, ${y}px) scale(${sx + .55 * home}, ${sy - .22 * home})`;
    cta.style.setProperty('--charge', charge.toFixed(3));
    cta.classList.toggle('caught', caught); cta.classList.toggle('lit', charge > .5);
    if (!full && charge > .95) { full = true; cta.classList.add('full'); onCharged(); }
    }
    
    function tick() {
    if (dist(mouse, btn) < grab) caught = true;
    const goal = caught ? btn : mouse;
    vx = (vx + (goal.x - x) * STIFF) * DAMP;
    vy = (vy + (goal.y - y) * STIFF) * DAMP;
    x += vx; y += vy;
    if (caught) charge += (1 - charge) * 0.04;
    paint(x, y, charge);
    requestAnimationFrame(tick);
    }
    
    function step() {
    measure();
    if (calm) {
      if (dist(mouse, btn) < grab) caught = true;
      const goal = caught ? btn : mouse; x = goal.x; y = goal.y; vx = vy = 0;
      if (caught) charge = 1;
      paint(x, y, charge);
    }
    }
    
    cta.addEventListener('click', () => {
    if (cta.classList.contains('done')) return;
    cta.classList.add('done', 'full');
    charge = 1; full = true; grab = 0; caught = false;
    onClick();
    });
    
    addEventListener('resize', measure);
    addEventListener('scroll', measure, { passive: true });
    measure();
    if (calm) { root.addEventListener('pointermove', step); step(); } else tick();
    
    function onCharged() {
    // the button is fully lit, a good moment for analytics
    }
    function onClick() {
    // your signup request or redirect goes here, for example
    // fetch('/api/signup', { method: 'POST' });
    }
    })();

Полезно знать

Tilda, Webflow, WordPress

Нажмите «Скопировать весь код» вверху и вставьте его в один HTML-блок.

Tilda

Добавить блок, Другое, T123 HTML-код. Вставьте и сохраните. Проверяйте на опубликованной странице, в редакторе скрипты не запускаются.

Webflow

Панель Add, Embed. Вставьте и нажмите Save & Close. Свой код работает на опубликованном сайте.

WordPress

Добавить блок, Произвольный HTML. Вставьте и нажмите «Обновить».

Ещё в разделе «Первый экран»

Все
Скопировано