Переключатель дня и ночи
Переключатель с солнцем и луной. Новая тема расходится кругом от ручки по всей странице через View Transitions API.
- Кнопки и переключатели
- Бесплатно
- transition
- Добавлен 14 сентября 2026
<style>
:root { --bg: #0A0D16; --card: #121726; --text: #E7EBF5; --sub: #8A93A8; --accent: #8FA8FF; color-scheme: dark; }
[data-theme="light"] { --bg: #EAF1FA; --card: #FFFFFF; --text: #0E1320; --sub: #6B7385; --accent: #FF9F1C; color-scheme: light; }
/* the browser snapshots old and new page, we only animate the circle */
::view-transition-old(root), ::view-transition-new(root) { animation: none; mix-blend-mode: normal; }
.settings { box-sizing: border-box; width: min(380px, 92vw); padding: 26px; border-radius: 26px; background: var(--card); text-align: center; }
.settings p { margin: 10px 0 0; color: var(--sub); font-size: 13px; }
.theme-toggle { position: relative; width: 148px; height: 64px; border: 0; padding: 0; border-radius: 32px; cursor: pointer; overflow: hidden;
background: linear-gradient(#161C3C, #0A0E22); transition: background .4s ease; }
[data-theme="light"] .theme-toggle { background: linear-gradient(#6EBEFF, #BEE6FF); }
.theme-toggle .knob { position: absolute; left: 7px; top: 7px; width: 50px; height: 50px; border-radius: 50%; background: #E8ECF6;
box-shadow: inset -6px -6px 0 #C5CCDA, 0 3px 8px rgba(0,0,0,.3);
transition: transform .7s cubic-bezier(.45, 1.2, .5, 1), background .35s ease, box-shadow .35s ease; }
[data-theme="light"] .theme-toggle .knob { transform: translateX(84px); background: #FFC94D; box-shadow: 0 0 0 6px rgba(255,201,77,.35), 0 3px 8px rgba(0,0,0,.2); }
.theme-toggle .stars, .theme-toggle .clouds { position: absolute; inset: 0; transition: opacity .35s ease, transform .5s ease; }
.theme-toggle .stars { background: radial-gradient(1.5px 1.5px at 80px 18px, #fff, transparent), radial-gradient(1px 1px at 104px 40px, #fff, transparent), radial-gradient(1.5px 1.5px at 124px 22px, #fff, transparent); }
.theme-toggle .clouds { opacity: 0; transform: translateX(-30px);
background: radial-gradient(circle at 26px 44px, #fff 10px, transparent 11px), radial-gradient(circle at 42px 38px, #fff 13px, transparent 14px), radial-gradient(circle at 58px 46px, #fff 9px, transparent 10px); }
[data-theme="light"] .theme-toggle .stars { opacity: 0; }
[data-theme="light"] .theme-toggle .clouds { opacity: 1; transform: none; }
@media (prefers-reduced-motion: reduce) { .theme-toggle .knob { transition: none; } }
</style>
<div class="settings">
<button class="theme-toggle" aria-label="Switch theme" aria-pressed="false"><span class="stars"></span><span class="clouds"></span><span class="knob"></span></button>
<p class="mode">Dark mode</p>
</div>
<script>
const toggle = document.querySelector('.theme-toggle');
const knob = toggle.querySelector('.knob');
const reduce = matchMedia('(prefers-reduced-motion: reduce)').matches;
function flip() {
const light = document.documentElement.dataset.theme !== 'light';
document.documentElement.dataset.theme = light ? 'light' : 'dark';
toggle.setAttribute('aria-pressed', light);
document.querySelector('.mode').textContent = light ? 'Light mode' : 'Dark mode';
}
const knobCenter = () => { const r = knob.getBoundingClientRect(); return { x: r.left + r.width / 2, y: r.top + r.height / 2 }; };
const EASE = 'cubic-bezier(.77, 0, .175, 1)';
toggle.onclick = async () => {
if (!document.startViewTransition || reduce) return flip();
const { x, y } = knobCenter();
const r = Math.hypot(innerWidth, innerHeight);
await document.startViewTransition(flip).ready;
document.documentElement.animate(
{ clipPath: [`circle(0 at ${x}px ${y}px)`,
`circle(${r}px at ${x}px ${y}px)`] },
{ duration: 700, easing: EASE,
pseudoElement: '::view-transition-new(root)' });
};
</script>
Нажмите на переключатель несколько раз.
Что можно поменять
Поменяйте число, сохраните файл и обновите страницу, разница будет видна сразу.
700durationСколько миллисекунд круг закрывает страницу.
EASEcubic-bezierКривая раскрытия. Эта медленная в начале и в конце и быстрая в середине.
--bg, --card, --textin :rootЦвета вашей темы. Используйте их в своём CSS, и переключаться будет весь сайт.
Как поставить на свой сайт
Поставьте переключатель в шапку сайта, а CSS-переменные используйте по всему сайту.
Комментарии в коде на английском, так файл понятен любому разработчику.
CSS
Вставьте в свой файл стилей или в тег <style> внутри <head>.
:root { --bg: #0A0D16; --card: #121726; --text: #E7EBF5; --sub: #8A93A8; --accent: #8FA8FF; color-scheme: dark; } [data-theme="light"] { --bg: #EAF1FA; --card: #FFFFFF; --text: #0E1320; --sub: #6B7385; --accent: #FF9F1C; color-scheme: light; } /* the browser snapshots old and new page, we only animate the circle */ ::view-transition-old(root), ::view-transition-new(root) { animation: none; mix-blend-mode: normal; } .settings { box-sizing: border-box; width: min(380px, 92vw); padding: 26px; border-radius: 26px; background: var(--card); text-align: center; } .settings p { margin: 10px 0 0; color: var(--sub); font-size: 13px; } .theme-toggle { position: relative; width: 148px; height: 64px; border: 0; padding: 0; border-radius: 32px; cursor: pointer; overflow: hidden; background: linear-gradient(#161C3C, #0A0E22); transition: background .4s ease; } [data-theme="light"] .theme-toggle { background: linear-gradient(#6EBEFF, #BEE6FF); } .theme-toggle .knob { position: absolute; left: 7px; top: 7px; width: 50px; height: 50px; border-radius: 50%; background: #E8ECF6; box-shadow: inset -6px -6px 0 #C5CCDA, 0 3px 8px rgba(0,0,0,.3); transition: transform .7s cubic-bezier(.45, 1.2, .5, 1), background .35s ease, box-shadow .35s ease; } [data-theme="light"] .theme-toggle .knob { transform: translateX(84px); background: #FFC94D; box-shadow: 0 0 0 6px rgba(255,201,77,.35), 0 3px 8px rgba(0,0,0,.2); } .theme-toggle .stars, .theme-toggle .clouds { position: absolute; inset: 0; transition: opacity .35s ease, transform .5s ease; } .theme-toggle .stars { background: radial-gradient(1.5px 1.5px at 80px 18px, #fff, transparent), radial-gradient(1px 1px at 104px 40px, #fff, transparent), radial-gradient(1.5px 1.5px at 124px 22px, #fff, transparent); } .theme-toggle .clouds { opacity: 0; transform: translateX(-30px); background: radial-gradient(circle at 26px 44px, #fff 10px, transparent 11px), radial-gradient(circle at 42px 38px, #fff 13px, transparent 14px), radial-gradient(circle at 58px 46px, #fff 9px, transparent 10px); } [data-theme="light"] .theme-toggle .stars { opacity: 0; } [data-theme="light"] .theme-toggle .clouds { opacity: 1; transform: none; } @media (prefers-reduced-motion: reduce) { .theme-toggle .knob { transition: none; } }HTML
Поставьте в то место страницы, где должен быть эффект.
<div class="settings"> <button class="theme-toggle" aria-label="Switch theme" aria-pressed="false"><span class="stars"></span><span class="clouds"></span><span class="knob"></span></button> <p class="mode">Dark mode</p> </div>JavaScript
Вставьте перед закрывающим тегом </body>, внутри <script></script>.
const toggle = document.querySelector('.theme-toggle'); const knob = toggle.querySelector('.knob'); const reduce = matchMedia('(prefers-reduced-motion: reduce)').matches; function flip() { const light = document.documentElement.dataset.theme !== 'light'; document.documentElement.dataset.theme = light ? 'light' : 'dark'; toggle.setAttribute('aria-pressed', light); document.querySelector('.mode').textContent = light ? 'Light mode' : 'Dark mode'; } const knobCenter = () => { const r = knob.getBoundingClientRect(); return { x: r.left + r.width / 2, y: r.top + r.height / 2 }; }; const EASE = 'cubic-bezier(.77, 0, .175, 1)'; toggle.onclick = async () => { if (!document.startViewTransition || reduce) return flip(); const { x, y } = knobCenter(); const r = Math.hypot(innerWidth, innerHeight); await document.startViewTransition(flip).ready; document.documentElement.animate( { clipPath: [`circle(0 at ${x}px ${y}px)`, `circle(${r}px at ${x}px ${y}px)`] }, { duration: 700, easing: EASE, pseudoElement: '::view-transition-new(root)' }); };
Полезно знать
- Работает в Chrome и Edge 111+, Safari 18+ и Firefox 144+. Старые браузеры меняют тему сразу, без круга, ничего не ломается.
Tilda, Webflow, WordPress
Нажмите «Скопировать весь код» вверху и вставьте его в один HTML-блок.
Добавить блок, Другое, T123 HTML-код. Вставьте и сохраните. Проверяйте на опубликованной странице, в редакторе скрипты не запускаются.
Панель Add, Embed. Вставьте и нажмите Save & Close. Свой код работает на опубликованном сайте.
Добавить блок, Произвольный HTML. Вставьте и нажмите «Обновить».



