我希望深色模式按鈕在您單擊時更改為 css,在等待模式下按鈕為深色,在深色模式下按鈕為淺色!
var toggleButton = document.getElementById('mode-toggle')
var isDarkMode = false;
function myFunction() {
isDarkMode = !isDarkMode;
document.querySelectorAll('div.scrollseite,div.scrollseite2').forEach(e => e.classList.toggle('dark-mode'))
document.querySelectorAll('div.btndarkmode').forEach(e => e.classList.toggle('btnwhitemode'))
toggleButton.innerHTML = isDarkMode ? 'Light mode' : 'Dark mode'
}
.btndarkmode {
background-color: #36393F;
text-align: center;
cursor: pointer;
display: inline-block;
font-size: 100%;
font-weight: bold;
position: fixed;
}
.btndarkmode:hover {
background-color: #32353B;
color: gray;
}
.btnwhitemode {
background-color: white;
text-align: center;
cursor: pointer;
display: inline-block;
font-size: 100%;
font-weight: bold;
position: fixed;
}
.btnwhitemode:hover {
background-color: white;
color: gray;
}
.dark-mode {
background-color: #36393F !important;
color: white !important;
}
<button onclick="myFunction()" class="btndarkmode" id="mode-toggle">Dark mode</button>
uj5u.com熱心網友回復:
var toggleButton = document.getElementById('mode-toggle')
function myFunction() {
// To Check if element is in Darkmode
if(toggleButton.textContent === "Dark mode") {
toggleButton.textContent = "Light mode"
toggleButton.style.background = "#eee"
return
}
// To Check if element is in Lightmode
if(toggleButton.textContent === "Light mode") {
toggleButton.textContent = "Dark mode"
toggleButton.style.background = "#333"
return
}
}
.btndarkmode {
background-color: #36393F;
text-align: center;
cursor: pointer;
display: inline-block;
font-size: 100%;
font-weight: bold;
position: fixed;
}
.btndarkmode:hover {
background-color: #32353B;
color: gray;
}
.btnwhitemode {
background-color: white;
text-align: center;
cursor: pointer;
display: inline-block;
font-size: 100%;
font-weight: bold;
position: fixed;
}
.btnwhitemode:hover {
background-color: white;
color: gray;
}
.dark-mode {
background-color: #36393F !important;
color: white !important;
}
<button onclick="myFunction()" class="btndarkmode" id="mode-toggle">Dark mode</button>
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/361530.html
標籤:javascript html css 主题 暗模式
上一篇:如何從json結果中選擇一些屬性
