最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Adding Animation while switching to dark mode HTML - Stack Overflow

matteradmin2PV0评论

I created a dark mode for my webpage by following the link I want to add an animation effect so that when the dark mode is applied, the transition is smooth. How do I do it?

I know CSS keyframes are used to add animations or alternatively jQuery can be used, but I can't seem to get how to use them.

I created a dark mode for my webpage by following the link https://dev.to/ananyaneogi/create-a-dark-light-mode-switch-with-css-variables-34l8 I want to add an animation effect so that when the dark mode is applied, the transition is smooth. How do I do it?

I know CSS keyframes are used to add animations or alternatively jQuery can be used, but I can't seem to get how to use them.

Share Improve this question asked Apr 9, 2020 at 20:08 user185887user185887 7131 gold badge7 silver badges21 bronze badges 1
  • 1 It's better to include relevant code in your question, as it gives the potential answerer easier access to your problem. Furthermore links may break. – Andre Nuechter Commented Apr 9, 2020 at 20:54
Add a ment  | 

1 Answer 1

Reset to default 12

You could use a css transition. Here follows a simple example, click in the snippet to transition the background color:

const rootDataset = document.documentElement.dataset;

document.onclick = () => {
    const inDarkMode = (rootDataset.theme === 'dark');
    rootDataset.theme = inDarkMode ? '' : 'dark';
}
:root {
    --primary-color: beige;    
}
[data-theme="dark"] {
    --primary-color: grey;    
}
body {
    background: var(--primary-color);
    transition: background 2s;
}

Post a comment

comment list (0)

  1. No comments so far