【发布时间】:2021-01-29 14:26:19
【问题描述】:
所以我有以下代码:
let darkMode = false;
const DOMDarkMode = document.querySelector(".dark");
const DOMLightMode = document.querySelector(".light");
function toggle(x, y) {
// Calculating circle size to fill a background
bubbleSize = Math.max(
// Distances calculating: to the click point..
Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2)), // ..from left-top point
Math.sqrt(Math.pow(innerWidth - x, 2) + Math.pow(y, 2)), // ..from right-top point
Math.sqrt(Math.pow(x, 2) + Math.pow(innerHeight - y, 2)), // ..from bottom-left point
Math.sqrt(Math.pow(innerWidth - x, 2) + Math.pow(innerHeight - y, 2)), // ..from bottom-right point
);
if (darkMode) {
darkMode = false;
DOMLightMode.style.setProperty("--x", x + "px");
DOMLightMode.style.setProperty("--y", y + "px");
DOMLightMode.style.setProperty("--size-to-fill", bubbleSize + "px");
DOMLightMode.classList.add("active");
DOMDarkMode.classList.remove("active");
} else {
darkMode = true;
DOMDarkMode.style.setProperty("--x", x + "px");
DOMDarkMode.style.setProperty("--y", y + "px");
DOMDarkMode.style.setProperty("--size-to-fill", bubbleSize + "px");
DOMDarkMode.classList.add("active");
DOMLightMode.classList.remove("active");
}
}
document.addEventListener("click", e=>{
toggle(e.x, e.y)
});
// IFRAME EFFECT
let iframe = true;
let tick = false;
let end = false;
setTimeout(()=>{ tick = true; }, 500);
setTimeout(()=>{ tick = true; }, 1500);
setTimeout(()=>{ end = true; }, 2100);
document.addEventListener("mouseover", e=>{ iframe = false; });
effects = setInterval(()=>{
if (!iframe)
end = true;
if (tick == true) {
tick = false;
toggle(0, 0);
}
if (end) {
darkMode = false;
DOMLightMode.classList.remove("active");
DOMDarkMode.classList.remove("active");
clearInterval(effects);
}
}, 0);
body {
margin: 0;
overflow: hidden;
}
* {
font-family: 'Open Sans', sans-serif;
font-size: 20px;
text-align: center;
user-select: none;
}
h1 {
font-family: 'Yusei Magic', sans-serif;
font-size: 2em;
margin: 0;
margin-bottom: .4em;
}
p {
margin: 0;
}
.main {
position: absolute;
}
.mode {
z-index: 1;
position: absolute;
display: flex;
justify-content: center;
align-items: center;
left: 0;
top: 0;
width: 100vw;
height: 100vh;
}
.box {
margin: 2em;
}
.active {
z-index: 2;
animation: show .6s ease;
}
@keyframes show {
from {
clip-path: circle(0 at var(--x) var(--y));
}
to {
clip-path: circle(var(--size-to-fill) at var(--x) var(--y));
}
}
.light {
background: #eee;
color: #000;
}
.dark {
background: #222;
color: #fff;
}
<div class="main">
<div class="dark mode">
</div>
<div class="light mode">
</div>
</div>
如果您单击任意位置,它会从暗模式切换到亮模式,反之亦然。我的问题是我应该在我的网站中的哪里包含上述代码的html,以便它适用于整个网站,因为现在,每当我点击某个地方时,只有我的主页从暗模式切换到亮模式,反之亦然。但是我将如何使上述代码适用于整个网站,并且无论我在我的网站中单击的哪个位置,整个网站都会从暗模式切换到亮模式,反之亦然。
我尝试在我的网站index.html 顶部的head 部分之后包含代码,但它仍然不起作用。有什么建议吗?
【问题讨论】:
标签: javascript html jquery css animation