Home > database >  Fade into light mode when toggle is clicked?
Fade into light mode when toggle is clicked?

Time:12-11

In the top left corner of this page, I have a "Light/Dark mode" toggle. When 'Light mode' is clicked, it fades into dark mode nicely no problem. But once it's in Dark mode, and you click 'light mode', it doesn't fade in nicely and I can't seem to get it to work. What would I need to add to my code to get it to fade nicely in to light mode?

<!DOCTYPE html>
<html id= "mode" lang="en-au">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<title> Test </title>
<head>
<link rel = "icon" type = "image/png" href = "https://ibb.co/bRc1Qqq">

<link rel = "apple-touch-icon" type = "image/png" href = "https://ibb.co/bRc1Qqq"/>
<!-- Square Windows tiles -->
<meta name="msapplication-square70x70logo" content="https://ibb.co/bRc1Qqq"></meta>
<meta name="msapplication-square150x150logo" content="https://ibb.co/bRc1Qqq"></meta>
<meta name="msapplication-square310x310logo" content="https://ibb.co/bRc1Qqq"></meta>
<!-- Rectangular Windows tile -->
<meta name="msapplication-wide310x150logo" content="https://ibb.co/bRc1Qqq"></meta>

<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Gugi|Raleway|Abril Fatface|Unica One|Press Start 2P|Bungee">


 

<!-- Makes stuff fadein on pageload-->
<script>
  window.onload = function()
  {document.body.className  = " loaded";
  document.querySelector("body").style.opacity = 1;
  }
  </script>



<style>
html {
  height: 100%;
  background-color:#b8b8b8;
}

body {
  text-decoration: none;
  font-family: "Raleway";
  border-radius: 7px;
  /* color-scheme: light dark; */

}

h1, ul {
  padding-top: 5%;
}

body, .fadein {
  opacity: 0;
  -moz-transition: opacity 3s;
  -webkit-transition: opacity 3s;
  -o-transition: opacity 3s;
  transition: opacity 3;
}

body.loaded .fadein {
  opacity:  1;
}

.container {
  max-width: 800px;
  margin: 0 auto;
}

.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.header h1 {
  margin: 0;
}

.tabs {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  
}

.tabs li {
  flex-grow: 1;
  text-align: center;
  padding: 0.5%;
  
}

.tabs a {
  display: block;
  padding: 10px;
  color: #333;
  text-decoration: none;
  border-radius: 7px;
}

.tabs a:hover,
.tabs a.active {
  background-color: #ddd;
}

.main {
  padding: 20px;
}

/* Hides all sections by default 
.section {
  display: none;
}
*/

.section.active {
  display: block;
}

.dark-mode {
  background-color: #020C17;
  color: #ffffff;
  transition: background-color 0.5s ease-in-out, color 0.5s ease-in-out;
}

.dark-mode ul a{
  background-color: #020C17;
  color: #ffffff;
}

.dark-mode ul a:hover,
.dark-mode ul a.active {
  background-color: #081334;
  color: #ddd;
  
}

#dark-mode-button {
  color: #ddd;
  padding: 10px;
  border-radius: 7px;
  cursor: pointer;
  transition: background-color 0.5s ease-in-out, color 0.5s ease-in-out;
  position: fixed; 
}

</style>

<!-- makes scrolling smooth when using anchor -->
<script> 
  document.querySelector('a').addEventListener('click', function(e) {
    e.preventDefault();
    var target = document.querySelector(this.getAttribute('href'));
    var offset = target.offsetTop;
    window.scrollTo({
      top: offset,
      behavior: 'smooth'
    });
  });
</script>


<!-- The HTML for the website -->
<body> 
  <div id="dark-mode-button"><p id="mango" >Dark mode</p></div>
<div >
<div >
    <!-- The header with the title and tabs -->
    <div >
        
      <h1>Testing</h1>
      <ul >
        <li><a href="#section1" >About</a></li>
        <li><a href="#section2">Projects</a></li>
        <li><a href="#section3">Test</a></li>
      </ul>
    </div>
    <!-- The main content of the website, with the sections -->
    <div >
      <div id="section1">
        <h2>About</h2>
        <p>This is the content of section 1.</p>      
  
      </div>
      <div  id="section2">
        <h2>Projects</h2>
        <p>This is the content of section 2.</p>
      </div>
      <div  id="section3">
        <h2>Test</h2>
        <p>This is the content of section 3.</p>
      </div>

      
    </div>
  </div>
</div>
</body>
  

<script>
  // Get the elements for the tabs and sections
const tabs = document.querySelectorAll('.tabs a');
const sections = document.querySelectorAll('.section');

// Add a click event listener to each tab
tabs.forEach(tab => {
tab.addEventListener('click', event => {
  event.preventDefault(); // prevent the default link behavior
  // Remove the active class from all tabs and sections
  tabs.forEach(tab => tab.classList.remove('active'));
  sections.forEach(section => section.classList.remove('active'));
  // Add the active class to the clicked tab and corresponding section
  tab.classList.add('active');
  document.querySelector(tab.getAttribute('href')).classList.add('active');
});
});

</script>


<!-- JavaScript code to handle the button click and switch between modes. also uses button id and the body element -->
<script>
  // Get the button and add a click event listener to it
  const button = document.getElementById('dark-mode-button');
  button.addEventListener('click', () => {
    // Get the current body element and toggle the "dark-mode" class
    const body = document.getElementById("mode");
    body.classList.toggle('dark-mode');

  });
</script>

<script>
/* Fetch the buttom element */
const mode = document.getElementById('mango');
/* Add click event listener where we will provide logic that updates the button text */
mode.addEventListener('click', function() {
  /* Update the text of the button to toggle beween "More" and "Less" when clicked */
  if(mode.innerText.toLowerCase() === 'dark mode') {
    mode.innerText = 'Light mode';
  }
  else {
    mode.innerText = 'Dark mode';
  }
});

</script>

CodePudding user response:

You need to apply the tranition effect to the elements affected by your dark theme, not the dark-mode class itself. You did it right for the #dark-mode-button though.

Solution for your code: add a transition (transition: all 0.5s ease-in-out) on html{} and .tabs a {}, and you can remove the transition effect on the dark-mode class. Fiddle: https://jsfiddle.net/40y5axcd/

Note: you applied a white text color to the whole html for dark mode, hence your main content text isn't visible on a white background. I left it as is, since it wasn't a part of your question.

Going forward you're better off using an utility class with the transition effect and apply it to all elements affected by the dark mode, especially if you continue to add elements to your page and want to change the transition duration. You rather only have to change one class, then having to go through every element and change the duration.

  • Related