为什么读取 localStorage 值需要刷新页面才能显示由 jQuery 切换的 CSS 更改?
Why does reading localStorage values require a page refresh to show CSS changes toggled by jQuery?
我正在尝试使用 localStorage
的值来显示两个 Twitter 提要之一,一个用于浅色模式主题,另一个用于深色模式。它有效,但我必须刷新网页才能使正确的 CSS - twitter-dark-display-none
或 twitter-light-display-none
- 正常工作。
使用 jQuery(document).ready(function ()
没有帮助。
Fiddle: https://jsfiddle.net/zpsf5q3x/2/ 但由于 JSFiddle 对显示第三方框架的限制,示例推文未显示。而且,localstorage 可能在那里也不起作用。
Fiddle调用两个外部库:
https://cdn.jsdelivr.net/gh/gitbrent/bootstrap4-toggle@3.6.1/css/bootstrap4-toggle.min.css
和 https://cdn.jsdelivr.net/gh/gitbrent/bootstrap4-toggle@3.6.1/js/bootstrap4-toggle.min.js
HTML:
注意第一块的data-theme="dark"
。
<div class="twitter-dark-display-none">
<a class="twitter-timeline" data-width="170" data-height="200" data-theme="dark"
data-tweet-limit="1" data-chrome="transparent nofooter noborders" href="https://twitter.com/Whosebug?ref_src=twsrc%5Etfw">Tweets</a>
</div>
<div class="twitter-light-display-none">
<a class="twitter-timeline" data-width="170" data-height="200" data-tweet-limit="1" data-chrome="transparent nofooter noborders" href="https://twitter.com/Whosebug?ref_src=twsrc%5Etfw">Tweets</a>
</div>
jQuery:
使用 localStorage 在暗模式和正常模式之间切换整个网站的整体功能。
$('body').toggleClass(localStorage.toggled);
function darkLight() {
if (localStorage.toggled != 'dark') {
$('body').toggleClass('dark', true);
localStorage.toggled = "dark";
} else {
$('body').toggleClass('dark', false);
localStorage.toggled = "";
}
}
我想用什么来切换 CSS:
if (localStorage.toggled === 'dark') {
$('.twitter-light-display-none').addClass("display-none");
$('.twitter-dark-display-none').addClass("display-block");
} else {
$('.twitter-dark-display-none').addClass("display-none");
$('.twitter-light-display-none').addClass("display-block");
}
CSS:
.display-none {
display: none !important;
}
.display-block {
display: block !important;
}
编辑 2020 年 10 月 24 日:
johannchopin's
答案有效,加上 $('body').toggleClass(localStorage.toggled);
就像我上面的原始代码一样。
但是。 gitbrent
暗模式 JS 和 CSS 库存在某种冲突,因此我切换到 https://github.com/coliff/dark-mode-switch
,这导致了一种更简单的方法来切换暗模式并另外使用 localstorage到提供的功能 johannchopin
以在 Twitter 小部件 div 之间切换:
<script>
(function() {
var darkSwitch = document.getElementById("darkSwitch");
if (darkSwitch) {
initTheme();
darkSwitch.addEventListener("change", function(event) {
resetTheme();
});
function initTheme() {
var darkThemeSelected =
localStorage.getItem("darkSwitch") !== null &&
localStorage.getItem("darkSwitch") === "dark";
darkSwitch.checked = darkThemeSelected;
darkThemeSelected
? document.body.setAttribute("data-theme", "dark")
: document.body.removeAttribute("data-theme");
}
function resetTheme() {
if (darkSwitch.checked) {
document.body.setAttribute("data-theme", "dark");
localStorage.setItem("darkSwitch", "dark");
} else {
document.body.removeAttribute("data-theme");
localStorage.removeItem("darkSwitch");
}
updatedarkSwitch();
}
}
})();
function updatedarkSwitch() {
if (localStorage.darkSwitch === 'dark') {
$('.twitter-light-display-none').addClass("display-none").removeClass('display-block');
$('.twitter-dark-display-none').addClass("display-block").removeClass('display-none');
} else {
$('.twitter-dark-display-none').addClass("display-none").removeClass('display-block');
$('.twitter-light-display-none').addClass("display-block").removeClass('display-none');
}
}
updatedarkSwitch()
</script>
然后在风格上使用最基本的深色模式规则sheet:
[data-theme="dark"] {
background-color: #000000 !important;
}
and also add any other more specific CSS rules needed, i.e.
[data-theme="dark"] .post-title{
color:#fff !important;
}
您应该使用方法 getItem()。
let x = localStorage.getItem('toggled');
然后检查变量。
https://developer.mozilla.org/ru/docs/Web/API/Storage/getItem
您的代码中存在多个问题。首先,您切换 CSS,但仅在页面加载时切换,因为此代码仅 运行 一次(加载脚本时):
if (localStorage.toggled === 'dark') {
// ...
只需将此脚本移动到您在 darkLight()
末尾调用的函数(例如:updateInterfaceTheme
)中即可。
另一个问题是您忘记清理之前的 class,这会导致样式冲突:
$('.twitter-light-display-none').addClass("display-none"); // class .display-block still exist
$('.twitter-dark-display-none').addClass("display-block"); // class .display-none still exist
所以不要忘记清理它们:
if (localStorage.toggled === 'dark') {
$('.twitter-light-display-none').addClass("display-none").removeClass('display-block');
$('.twitter-dark-display-none').addClass("display-block").removeClass('display-none');
} else {
$('.twitter-dark-display-none').addClass("display-none").removeClass('display-block');
$('.twitter-light-display-none').addClass("display-block").removeClass('display-none');
}
它就这样工作了:
function darkLight() {
if (localStorage.toggled !== 'dark') {
$('body').toggleClass('dark', true);
localStorage.toggled = "dark";
} else {
$('body').toggleClass('dark', false);
localStorage.toggled = "";
}
updateInterfaceTheme();
}
function updateInterfaceTheme() {
if (localStorage.toggled === 'dark') {
$('.twitter-light-display-none').addClass("display-none").removeClass('display-block');
$('.twitter-dark-display-none').addClass("display-block").removeClass('display-none');
} else {
$('.twitter-dark-display-none').addClass("display-none").removeClass('display-block');
$('.twitter-light-display-none').addClass("display-block").removeClass('display-none');
}
}
updateInterfaceTheme()
.display-none {
display: none !important;
}
.display-block {
display: block !important;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<label class="switch"><span class="switchtext">Dark</span>
<input type="checkbox" data-style="ios" data-onstyle="outline-secondary" data-offstyle="outline-secondary" data-size="sm" data-toggle="toggle" onchange="darkLight()">
<span class="slider"></span><span class="switchtext">Mode</span>
</label>
<div class="twitter-dark-display-none">Dark Mode
<a class="twitter-timeline" data-width="170" data-height="200" data-theme="dark" data-tweet-limit="1" data-chrome="transparent nofooter noborders" href="https://twitter.com/Whosebug?ref_src=twsrc%5Etfw">Tweets</a>
</div>
<div class="twitter-light-display-none">Light Mode
<a class="twitter-timeline" data-width="170" data-height="200" data-tweet-limit="1" data-chrome="transparent nofooter noborders" href="https://twitter.com/Whosebug?ref_src=twsrc%5Etfw">Tweets</a>
</div>
检查 jsfiddle 以查看它是否与 localStorage 一起工作。
我正在尝试使用 localStorage
的值来显示两个 Twitter 提要之一,一个用于浅色模式主题,另一个用于深色模式。它有效,但我必须刷新网页才能使正确的 CSS - twitter-dark-display-none
或 twitter-light-display-none
- 正常工作。
使用 jQuery(document).ready(function ()
没有帮助。
Fiddle: https://jsfiddle.net/zpsf5q3x/2/ 但由于 JSFiddle 对显示第三方框架的限制,示例推文未显示。而且,localstorage 可能在那里也不起作用。
Fiddle调用两个外部库:
https://cdn.jsdelivr.net/gh/gitbrent/bootstrap4-toggle@3.6.1/css/bootstrap4-toggle.min.css
和 https://cdn.jsdelivr.net/gh/gitbrent/bootstrap4-toggle@3.6.1/js/bootstrap4-toggle.min.js
HTML:
注意第一块的data-theme="dark"
。
<div class="twitter-dark-display-none">
<a class="twitter-timeline" data-width="170" data-height="200" data-theme="dark"
data-tweet-limit="1" data-chrome="transparent nofooter noborders" href="https://twitter.com/Whosebug?ref_src=twsrc%5Etfw">Tweets</a>
</div>
<div class="twitter-light-display-none">
<a class="twitter-timeline" data-width="170" data-height="200" data-tweet-limit="1" data-chrome="transparent nofooter noborders" href="https://twitter.com/Whosebug?ref_src=twsrc%5Etfw">Tweets</a>
</div>
jQuery:
使用 localStorage 在暗模式和正常模式之间切换整个网站的整体功能。
$('body').toggleClass(localStorage.toggled);
function darkLight() {
if (localStorage.toggled != 'dark') {
$('body').toggleClass('dark', true);
localStorage.toggled = "dark";
} else {
$('body').toggleClass('dark', false);
localStorage.toggled = "";
}
}
我想用什么来切换 CSS:
if (localStorage.toggled === 'dark') {
$('.twitter-light-display-none').addClass("display-none");
$('.twitter-dark-display-none').addClass("display-block");
} else {
$('.twitter-dark-display-none').addClass("display-none");
$('.twitter-light-display-none').addClass("display-block");
}
CSS:
.display-none {
display: none !important;
}
.display-block {
display: block !important;
}
编辑 2020 年 10 月 24 日:
johannchopin's
答案有效,加上 $('body').toggleClass(localStorage.toggled);
就像我上面的原始代码一样。
但是。 gitbrent
暗模式 JS 和 CSS 库存在某种冲突,因此我切换到 https://github.com/coliff/dark-mode-switch
,这导致了一种更简单的方法来切换暗模式并另外使用 localstorage到提供的功能 johannchopin
以在 Twitter 小部件 div 之间切换:
<script>
(function() {
var darkSwitch = document.getElementById("darkSwitch");
if (darkSwitch) {
initTheme();
darkSwitch.addEventListener("change", function(event) {
resetTheme();
});
function initTheme() {
var darkThemeSelected =
localStorage.getItem("darkSwitch") !== null &&
localStorage.getItem("darkSwitch") === "dark";
darkSwitch.checked = darkThemeSelected;
darkThemeSelected
? document.body.setAttribute("data-theme", "dark")
: document.body.removeAttribute("data-theme");
}
function resetTheme() {
if (darkSwitch.checked) {
document.body.setAttribute("data-theme", "dark");
localStorage.setItem("darkSwitch", "dark");
} else {
document.body.removeAttribute("data-theme");
localStorage.removeItem("darkSwitch");
}
updatedarkSwitch();
}
}
})();
function updatedarkSwitch() {
if (localStorage.darkSwitch === 'dark') {
$('.twitter-light-display-none').addClass("display-none").removeClass('display-block');
$('.twitter-dark-display-none').addClass("display-block").removeClass('display-none');
} else {
$('.twitter-dark-display-none').addClass("display-none").removeClass('display-block');
$('.twitter-light-display-none').addClass("display-block").removeClass('display-none');
}
}
updatedarkSwitch()
</script>
然后在风格上使用最基本的深色模式规则sheet:
[data-theme="dark"] {
background-color: #000000 !important;
}
and also add any other more specific CSS rules needed, i.e.
[data-theme="dark"] .post-title{
color:#fff !important;
}
您应该使用方法 getItem()。
let x = localStorage.getItem('toggled');
然后检查变量。
https://developer.mozilla.org/ru/docs/Web/API/Storage/getItem
您的代码中存在多个问题。首先,您切换 CSS,但仅在页面加载时切换,因为此代码仅 运行 一次(加载脚本时):
if (localStorage.toggled === 'dark') {
// ...
只需将此脚本移动到您在 darkLight()
末尾调用的函数(例如:updateInterfaceTheme
)中即可。
另一个问题是您忘记清理之前的 class,这会导致样式冲突:
$('.twitter-light-display-none').addClass("display-none"); // class .display-block still exist
$('.twitter-dark-display-none').addClass("display-block"); // class .display-none still exist
所以不要忘记清理它们:
if (localStorage.toggled === 'dark') {
$('.twitter-light-display-none').addClass("display-none").removeClass('display-block');
$('.twitter-dark-display-none').addClass("display-block").removeClass('display-none');
} else {
$('.twitter-dark-display-none').addClass("display-none").removeClass('display-block');
$('.twitter-light-display-none').addClass("display-block").removeClass('display-none');
}
它就这样工作了:
function darkLight() {
if (localStorage.toggled !== 'dark') {
$('body').toggleClass('dark', true);
localStorage.toggled = "dark";
} else {
$('body').toggleClass('dark', false);
localStorage.toggled = "";
}
updateInterfaceTheme();
}
function updateInterfaceTheme() {
if (localStorage.toggled === 'dark') {
$('.twitter-light-display-none').addClass("display-none").removeClass('display-block');
$('.twitter-dark-display-none').addClass("display-block").removeClass('display-none');
} else {
$('.twitter-dark-display-none').addClass("display-none").removeClass('display-block');
$('.twitter-light-display-none').addClass("display-block").removeClass('display-none');
}
}
updateInterfaceTheme()
.display-none {
display: none !important;
}
.display-block {
display: block !important;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<label class="switch"><span class="switchtext">Dark</span>
<input type="checkbox" data-style="ios" data-onstyle="outline-secondary" data-offstyle="outline-secondary" data-size="sm" data-toggle="toggle" onchange="darkLight()">
<span class="slider"></span><span class="switchtext">Mode</span>
</label>
<div class="twitter-dark-display-none">Dark Mode
<a class="twitter-timeline" data-width="170" data-height="200" data-theme="dark" data-tweet-limit="1" data-chrome="transparent nofooter noborders" href="https://twitter.com/Whosebug?ref_src=twsrc%5Etfw">Tweets</a>
</div>
<div class="twitter-light-display-none">Light Mode
<a class="twitter-timeline" data-width="170" data-height="200" data-tweet-limit="1" data-chrome="transparent nofooter noborders" href="https://twitter.com/Whosebug?ref_src=twsrc%5Etfw">Tweets</a>
</div>