Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [CSS][JavaScript]DarkMode css
TakiTaki
post
Post #1





Grupa: Zarejestrowani
Postów: 6
Pomógł: 0
Dołączył: 8.08.2021

Ostrzeżenie: (0%)
-----


Cześć,
Mam następujący problem próbuje dodać dark mode do strony i pojawia się błąd "toggleSwitch is null"

  1. <div id="dark-switch" class="custom-control custom-switch tw mode-switch" style="padding-top: 27px; padding-left: 45px;">
  2. <input type="checkbox" class="custom-control-input" id="darkSwitch">
  3. <label class="custom-control-label" for="darkSwitch" style="color: white;">Dark Mode</label>
  4. </div>



  1.  
  2. const toggleSwitch = document.querySelector('.mode-switch input[type="checkbox"]');
  3. const currentTheme = localStorage.getItem('theme');
  4.  
  5. if (currentTheme) {
  6. document.documentElement.setAttribute('data-theme', currentTheme);
  7.  
  8. if (currentTheme === 'dark') {
  9. toggleSwitch.checked = true;
  10. }
  11. }
  12.  
  13. function switchTheme(e) {
  14. if (e.target.checked) {
  15. document.documentElement.setAttribute('data-theme', 'dark');
  16. localStorage.setItem('theme', 'dark');
  17. }
  18. else { document.documentElement.setAttribute('data-theme', 'light');
  19. localStorage.setItem('theme', 'light');
  20. }
  21. }
  22.  
  23. toggleSwitch.addEventListener('change', switchTheme, false);
  24.  


i css


  1. :root {
  2. --background_color_body: #303030;
  3. --text_color_body: #000000;
  4. --background_nav_color: #353A40;
  5. --content-color: #F5F5F5;
  6. --right-form-color: #000000;
  7.  
  8. }
  9.  
  10. [data-theme="dark"] {
  11. --background_color_body: #000000;
  12. --text_color_body: #ffffff;
  13. --background_nav_color: #353A40;
  14. --content-color: #202020;
  15. --right-form-color: #FFFFFF;
  16.  
  17. }
  18.  
  19. .body{
  20. margin-bottom: 100px;
  21. background-color: var(--background_color_body);
  22. color: var(--text_color_body);
  23. }


Ktoś może mi wyjaśnić dlaczego się pojawia ten błąd?
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 22.08.2025 - 06:01