如何在整个站点和重新加载时存储用户输入(在深色和浅色主题之间切换)
How to store the user input throughout the site and on reload ( switch between dark and light theme )
我基本上是在为我的网站设置暗模式切换,我希望浏览器记住用户从暗主题到亮主题的点击。代码有些混乱且不干净,但无论如何它可以在更改深色和浅色模式样式表中使用。
<link rel='stylesheet' id='3432-css' href='//site-url.org.in/wp-content/uploads/custom-css-js/3432.css?v=3920' type="text/css" media='all' />
<link rel='stylesheet' id='3428-css' href='//site-url.org.in/wp-content/uploads/custom-css-js/3428.css?v=2941' type="text/css" media='all' />
<div id="switch" class="toggle">
<div class="in-toggle tg-left">
<input type="checkbox">
</div>
</div>
JavaScript:
jQuery(document).ready(function($){
var toggle_btn = $(".toggle .in-toggle input");
var i_tg = $(".in-toggle");
var toggle = $(".toggle");
toggle_btn.on("click", clicked);
function clicked() {
if (toggle_btn.is(":checked")) {
i_tg.addClass("tg-right");
toggle.addClass("clicked");
} else {
i_tg.removeClass("tg-right");
toggle.removeClass('clicked');
}
}
var switch_mode = 'dark';
document.getElementById('switch').onclick = function(){
if(switch_mode == 'dark'){
document.getElementById('3428-css').setAttribute('href','');
document.getElementById('3432-css').setAttribute('href','//site-url.org.in/wp-content/uploads/custom-css-js/3432.css');
switch_mode = 'light';
} else {
document.getElementById('3428-css').setAttribute('href','//site-name.org.in/wp-content/uploads/custom-css-js/3428.css');
document.getElementById('3432-css').setAttribute('href','');
switch_mode = 'dark';
}
}
});
如何在切换回切换之前保存用户交互切换?这都是针对未登录用户的,因此本地存储或 cookie 会话可能会有所帮助。
我终于可以使用本地存储了。 js变了。工作 fiddle
我从这个 post 开始工作。一定要在调用 jquery 后添加 JS,因为样式或某些细节将无法按预期工作
Html
<link rel='stylesheet' id='3432-css' href='//your-site.org.in/wp-content/uploads/custom-css-js/3432.css?v=3920' data-theme="dark" type="text/css" media='all' />
<link rel='stylesheet' id='3428-css' href='//your-site.org.in/wp-content/uploads/custom-css-js/3428.css?v=2941' data-theme="light" type="text/css" media='all' />
<div id="switch1" class="container1 toggle">
<div class="toggle-container">
<input type="checkbox" id="switch" /><label for="switch">Toggle</label>
</div>
</div>
JavaScript
let theme = localStorage.getItem('data-theme');
const checkbox = document.getElementById("switch");
const changeThemeToDark = () =>{
document.getElementById('3428-css').setAttribute('href','');
document.getElementById('3432-css').setAttribute('href','//your-site.org.in/wp-content/uploads/custom-css-js/3432.css');
document.documentElement.setAttribute("data-theme", "dark")
document.getElementsByTagName("label")[0].setAttribute("class", "darkbg");
localStorage.setItem("data-theme", "dark")
}
const changeThemeToLight = () =>{
document.getElementById('3428-css').setAttribute('href','//your-site.org.in/wp-content/uploads/custom-css-js/3428.css');
document.getElementById('3432-css').setAttribute('href','');
document.getElementsByTagName("label")[0].setAttribute("class", "lightbg");
localStorage.setItem("data-theme", 'light')
}
if(theme === 'dark'){
changeThemeToDark()
}
checkbox.addEventListener('change', ()=> {
let theme = localStorage.getItem('data-theme');
if (theme ==='dark'){
document.getElementsByTagName("label")[0].setAttribute("class", "darkbg");
changeThemeToLight()
}else{
document.getElementsByTagName("label")[0].setAttribute("class", "lightbg");
changeThemeToDark()
}
});
风格
.container1 {
width: 60px;
height: 30px;
position: fixed;
top: 85%;
left: 85%;
transition: 1s ease;
z-index: 999;
}
div#switch1:before {
content: 'Switch Theme';
position: absolute;
top: -30px;
width: 110px;
text-align: center;
left: -30px;
font-weight: bold;
/* box-shadow: 5px 10px 8px #ffd6d6; */
background: #fff;
color: #000;
border-radius: 50px;
font-size: 13px;
}
input[type=checkbox]{
height: 0;
width: 0;
visibility: hidden;
}
.container1 label {
cursor: pointer;
text-indent: -9999px;
width: 100px;
height: 40px;
background: grey;
display: block;
border-radius: 100px;
position: relative;
}
label:after {
content: '';
position: absolute;
top: 5px;
left: 5px;
width: 30px;
height: 30px;
background: #fff;
border-radius: 90px;
transition: 0.3s;
}
input:checked + label {
background: #bada55;
}
input:checked + label::after {
left: calc(100% - 5px);
transform: translateX(-100%);
}
label:active:after {
width: 130px;
}
label.darkbg {
background: #007cba !important;
}
label.lightbg {
background: grey !important;
}
我基本上是在为我的网站设置暗模式切换,我希望浏览器记住用户从暗主题到亮主题的点击。代码有些混乱且不干净,但无论如何它可以在更改深色和浅色模式样式表中使用。
<link rel='stylesheet' id='3432-css' href='//site-url.org.in/wp-content/uploads/custom-css-js/3432.css?v=3920' type="text/css" media='all' />
<link rel='stylesheet' id='3428-css' href='//site-url.org.in/wp-content/uploads/custom-css-js/3428.css?v=2941' type="text/css" media='all' />
<div id="switch" class="toggle">
<div class="in-toggle tg-left">
<input type="checkbox">
</div>
</div>
JavaScript:
jQuery(document).ready(function($){
var toggle_btn = $(".toggle .in-toggle input");
var i_tg = $(".in-toggle");
var toggle = $(".toggle");
toggle_btn.on("click", clicked);
function clicked() {
if (toggle_btn.is(":checked")) {
i_tg.addClass("tg-right");
toggle.addClass("clicked");
} else {
i_tg.removeClass("tg-right");
toggle.removeClass('clicked');
}
}
var switch_mode = 'dark';
document.getElementById('switch').onclick = function(){
if(switch_mode == 'dark'){
document.getElementById('3428-css').setAttribute('href','');
document.getElementById('3432-css').setAttribute('href','//site-url.org.in/wp-content/uploads/custom-css-js/3432.css');
switch_mode = 'light';
} else {
document.getElementById('3428-css').setAttribute('href','//site-name.org.in/wp-content/uploads/custom-css-js/3428.css');
document.getElementById('3432-css').setAttribute('href','');
switch_mode = 'dark';
}
}
});
如何在切换回切换之前保存用户交互切换?这都是针对未登录用户的,因此本地存储或 cookie 会话可能会有所帮助。
我终于可以使用本地存储了。 js变了。工作 fiddle
我从这个 post 开始工作。一定要在调用 jquery 后添加 JS,因为样式或某些细节将无法按预期工作
Html
<link rel='stylesheet' id='3432-css' href='//your-site.org.in/wp-content/uploads/custom-css-js/3432.css?v=3920' data-theme="dark" type="text/css" media='all' />
<link rel='stylesheet' id='3428-css' href='//your-site.org.in/wp-content/uploads/custom-css-js/3428.css?v=2941' data-theme="light" type="text/css" media='all' />
<div id="switch1" class="container1 toggle">
<div class="toggle-container">
<input type="checkbox" id="switch" /><label for="switch">Toggle</label>
</div>
</div>
JavaScript
let theme = localStorage.getItem('data-theme');
const checkbox = document.getElementById("switch");
const changeThemeToDark = () =>{
document.getElementById('3428-css').setAttribute('href','');
document.getElementById('3432-css').setAttribute('href','//your-site.org.in/wp-content/uploads/custom-css-js/3432.css');
document.documentElement.setAttribute("data-theme", "dark")
document.getElementsByTagName("label")[0].setAttribute("class", "darkbg");
localStorage.setItem("data-theme", "dark")
}
const changeThemeToLight = () =>{
document.getElementById('3428-css').setAttribute('href','//your-site.org.in/wp-content/uploads/custom-css-js/3428.css');
document.getElementById('3432-css').setAttribute('href','');
document.getElementsByTagName("label")[0].setAttribute("class", "lightbg");
localStorage.setItem("data-theme", 'light')
}
if(theme === 'dark'){
changeThemeToDark()
}
checkbox.addEventListener('change', ()=> {
let theme = localStorage.getItem('data-theme');
if (theme ==='dark'){
document.getElementsByTagName("label")[0].setAttribute("class", "darkbg");
changeThemeToLight()
}else{
document.getElementsByTagName("label")[0].setAttribute("class", "lightbg");
changeThemeToDark()
}
});
风格
.container1 {
width: 60px;
height: 30px;
position: fixed;
top: 85%;
left: 85%;
transition: 1s ease;
z-index: 999;
}
div#switch1:before {
content: 'Switch Theme';
position: absolute;
top: -30px;
width: 110px;
text-align: center;
left: -30px;
font-weight: bold;
/* box-shadow: 5px 10px 8px #ffd6d6; */
background: #fff;
color: #000;
border-radius: 50px;
font-size: 13px;
}
input[type=checkbox]{
height: 0;
width: 0;
visibility: hidden;
}
.container1 label {
cursor: pointer;
text-indent: -9999px;
width: 100px;
height: 40px;
background: grey;
display: block;
border-radius: 100px;
position: relative;
}
label:after {
content: '';
position: absolute;
top: 5px;
left: 5px;
width: 30px;
height: 30px;
background: #fff;
border-radius: 90px;
transition: 0.3s;
}
input:checked + label {
background: #bada55;
}
input:checked + label::after {
left: calc(100% - 5px);
transform: translateX(-100%);
}
label:active:after {
width: 130px;
}
label.darkbg {
background: #007cba !important;
}
label.lightbg {
background: grey !important;
}