简体   繁体   English

JavaScript 黑暗模式正在影响我的 AOS?

[英]JavaScript Dark Mode is Affecting My AOS?

hello, newbie here.你好,这里是新手。

So I'm working on my portfolio website(local) and I've added AOS( https://michalsnik.github.io/aos/ ) and a dark mode( https://dev.to/ananyaneogi/create-a-dark-light-mode-switch-with-css-variables-34l8 ) in my scripts.所以我在我的投资组合网站(本地)上工作,我添加了 AOS( https://michalsnik.github.io/aos/ )和暗模式( https://dev.to/ananyaneogi/create-a -dark-light-mode-switch-with-css-variables-34l8 ) 在我的脚本中。

Before adding the dark mode, the animated scrolls worked.在添加黑暗模式之前,动画卷轴起作用了。

After I added the dark mode, my website no longer animates the fades automatically when it's first loaded or when it's refreshed.添加深色模式后,我的网站在首次加载或刷新时不再自动设置淡入淡出动画。

Ex.前任。 My About Me section is shown when the site is loaded/refreshed;我的关于我部分在网站加载/刷新时显示; when it should have been hidden and only fades in when scrolled into.当它应该被隐藏并且只有在滚动时才会淡入。

How do I fix this?我该如何解决? (if there's no solution I can just live with this minor bug lmao) (如果没有解决方案我只能忍受这个小错误lmao)

Example of the fade on the h2 tag: h2 标签上的淡入淡出示例:

<div data-aos="fade-right" data-aos-duration="1000" data-aos-easing="ease-in-out"
     data-aos-mirror="true" data-aos-once="false" class="aos-init aos-animate">
     <h2>About Me</h2>
</div>

My JavaScript file:我的 JavaScript 文件:

 document.addEventListener("DOMContentLoaded", function() { setTimeout(function() { AOS.refresh(); }, 500); }); // Dark Mode Switcher // Source: https://dev.to/ananyaneogi/create-a-dark-light-mode-switch-with-css-variables-34l8 var toggleSwitch = document.querySelector('.theme-switch input[type="checkbox"]'); const currentTheme = localStorage.getItem('theme'); if (currentTheme) { document.documentElement.setAttribute('data-theme', currentTheme); if (currentTheme === 'dark') { toggleSwitch.checked = true; } } function switchTheme(e) { if (e.target.checked) { document.documentElement.setAttribute('data-theme', 'dark'); localStorage.setItem('theme', 'dark'); } else { document.documentElement.setAttribute('data-theme', 'light'); localStorage.setItem('theme', 'light'); } } window.addEventListener('load', AOS.refresh) toggleSwitch.addEventListener('change', switchTheme, false);

It is a common issue of AOS, when you first load a website it may happen after a change in css, in your case when your trigger dark mode.这是 AOS 的一个常见问题,当您第一次加载网站时,它可能会在更改 css 后发生,在您的情况下,您会触发黑暗模式。

try forcing the page to reload after you change to dark mode with window.location.reload()使用window.location.reload()更改为暗模式后,尝试强制重新加载页面

function switchTheme(e) {
    if (e.target.checked) {
        document.documentElement.setAttribute('data-theme', 'dark');
        localStorage.setItem('theme', 'dark');
        window.location.reload()

    }
    else {
        document.documentElement.setAttribute('data-theme', 'light');
        localStorage.setItem('theme', 'light');
        window.location.reload()
    }
}

If you use any kind of switch then try it,如果您使用任何一种开关,请尝试一下,

             <DarkModeSwitch
              className={darkMode ? 'dark__icon' : 'light__icon'}
              checked={darkMode}
              onClick={() => toggleTheme() + window.location.reload()}
              />
           

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM