How we can create a dark and light mode switch with an animation? Solution: See this CSS Dark Mode Toggle With Clip Path Animation, Dark / Light Mode switch.
Previously I have shared Light & Dark mode change program, but this with cool clip-path animation. Basically, in this program, we can switch between dark and light mode. When it’s changing that time we can see a cool animation with expanding effect. Dark themes reduce the luminance emitted by device screens, while still meeting minimum color contrast ratios.
Today you will learn to create a dark and light mode switch program with CSS and jQuery. This design has a sticky navbar with logo placeholder, come text cards with icons, and a toggle button with day and night icon. When you will click on the icon then the theme will be dark with an expanding circle clip-path animation. It is similar to Clip Path Animation Card program, but it is expanding around the whole webpage.
So, Today I am sharing CSS Dark Mode Toggle With Clip Path Animation. There I have used jQuery for handling the toggle feature and modify other CSS values. We use libraries for easy and fast work, and we can do any complicated JS program in a few lines of codes using jQuery. And the animation will not work in IE browser, but the dark/light mode switch will work without animation.
If you are thinking now how this Dark/ Light Mode Switch program actually is, then see the preview given below.
Preview of Dark / Light Mode Switch With jQuery
See this video preview to getting an idea of how this toggle animation looks like.
Now you can see this visually, you also can see it live by pressing the button given above. If you like this, then get the source code of its.
You May Also Like:
- jQuery Horizontal Tab Menu
- Full Page Scrolling Website
- Highlight Active Tab Animation
- Bootstrap Responsive Mega Menu
CSS Dark Mode Toggle With Clip Path Animation Source Code
Before sharing source code, let’s talk about it. First, using HTML I have put one fixed nav, 3 text cards, and an icon for switch modes. For placing icons, I have used Font-Awesome icon library with the latest release. And in HTML file, I have linked many files like jQuery, font, icons, etc.
Now using CSS I have placed all the elements in the right place, as you can see in the preview. I gave style to text elements like a card and put an icon in every text card for better UI. See in the preview, there is a circle which expands on toggle switch that is the main feature of this program. For creating this, I have used CSS clip-path property (info).
jQuery handling the toggle feature and change CSS values dynamically. I have created CSS variables for store colors and gave separate values for light and dark mode. And there is also a little Scroll to Top feature in every card, when you click on the right icons then you will see. Also, jQuery changes the night-day icon on the toggle.
Left all other things you will understand after getting the codes, I can’t explain all in writing. For creating this program, you have to create 3 files. First for HTML, second for CSS, and the third for JavaScript. Follow the steps to creating this without any error.
index.html
Create an HTML file named ‘index.html‘ and put these codes given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
<!DOCTYPE html> <!--Code By Webdevtrick ( https://webdevtrick.com )--> <html lang="en" > <head> <meta charset="UTF-8"> <title>Clip Path Dark/Light Mode | Webdevtrick.com</title> <link href="https://fonts.googleapis.com/css?family=Big+Shoulders+Text&display=swap" rel="stylesheet"> <link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/normalize/8.0.1/normalize.min.css'> <link rel='stylesheet' href='https://pro.fontawesome.com/releases/v5.10.2/css/all.css'> <link rel='stylesheet' href='style.css'> </head> <body> <div id="container" class="container"> <div class="header card"> <div class="section-width"> <div class="logotext">LOGO</div> </div> </div> <div class="content"> <div class="page inner-card section-width"> <div id="post1"></div> <h1>Heading 1<a href="#post1" class="fad fa-bolt"></a></h1> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris vehicula quis lectus a tempor. Praesent lobortis mi eu finibus lacinia. Aenean lacinia ornare libero, vel congue nunc convallis dictum. Vestibulum aliquam tempus posuere. Nullam vitae tellus ligula.<br> Maecenas sit amet tortor nec lacus vulputate suscipit. Aliquam placerat, urna in maximus placerat, lectus ex tempor odio, facilisis rutrum arcu sapien rutrum libero. Nunc vestibulum posuere elementum.<br> Mauris ultricies vitae eros a vestibulum. Pellentesque auctor facilisis massa sit amet euismod. Quisque magna lacus,<br> vulputate non risus vel, luctus luctus enim. Aenean fringilla sit amet ipsum in dapibus. Vivamus tristique diam ut tellus elementum gravida a vel arcu.</p> </div> <div class="page inner-card section-width"> <div id="post2"></div> <h1>Heading 2<a href="#post2" class="fad fa-bolt"></a></h1> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris vehicula quis lectus a tempor. Praesent lobortis mi eu finibus lacinia. Aenean lacinia ornare libero, vel congue nunc convallis dictum. Vestibulum aliquam tempus posuere. Nullam vitae tellus ligula.<br> Maecenas sit amet tortor nec lacus vulputate suscipit. Aliquam placerat, urna in maximus placerat, lectus ex tempor odio, facilisis rutrum arcu sapien rutrum libero. Nunc vestibulum posuere elementum.<br> Mauris ultricies vitae eros a vestibulum. Pellentesque auctor facilisis massa sit amet euismod. Quisque magna lacus,<br> vulputate non risus vel, luctus luctus enim. Aenean fringilla sit amet ipsum in dapibus. Vivamus tristique diam ut tellus elementum gravida a vel arcu.</p> </div> <div class="page inner-card section-width"> <div id="post3"></div> <h1>Heading 3<a href="#post3" class="fad fa-bolt"></a></h1> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris vehicula quis lectus a tempor. Praesent lobortis mi eu finibus lacinia. Aenean lacinia ornare libero, vel congue nunc convallis dictum. Vestibulum aliquam tempus posuere. Nullam vitae tellus ligula.<br> Maecenas sit amet tortor nec lacus vulputate suscipit. Aliquam placerat, urna in maximus placerat, lectus ex tempor odio, facilisis rutrum arcu sapien rutrum libero. Nunc vestibulum posuere elementum.<br> Mauris ultricies vitae eros a vestibulum. Pellentesque auctor facilisis massa sit amet euismod. Quisque magna lacus,<br> vulputate non risus vel, luctus luctus enim. Aenean fringilla sit amet ipsum in dapibus. Vivamus tristique diam ut tellus elementum gravida a vel arcu.</p> </div> </div> <div class="darkmode inner-card fas fa-moon-cloud"></div> </div> <div class="clip"></div> <script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js'></script> <script src="function.js"></script> </body> </html> |
style.css
Now create a CSS file named ‘style.css‘ and put these codes given here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
/* Code By Webdevtrick ( https://webdevtrick.com ) */ @charset "UTF-8"; body { font-family: 'Big Shoulders Text', cursive; } .clip { z-index: 2; position: fixed; bottom: 3rem; left: 3rem; width: 0rem; height: 0rem; border-radius: 100%; } .clip.anim { -webkit-animation: open 1.5s ease-in; animation: open 1.5s ease-in; } @-webkit-keyframes open { 0% { bottom: 3rem; left: 3rem; width: 0rem; height: 0rem; -webkit-clip-path: circle(0rem at center); clip-path: circle(0rem at center); } 100% { bottom: calc(-250vmax + 3rem); left: calc(-250vmax + 3rem); width: 500vmax; height: 500vmax; -webkit-clip-path: circle(100% at center); clip-path: circle(100% at center); } } @keyframes open { 0% { bottom: 3rem; left: 3rem; width: 0rem; height: 0rem; -webkit-clip-path: circle(0rem at center); clip-path: circle(0rem at center); } 100% { bottom: calc(-250vmax + 3rem); left: calc(-250vmax + 3rem); width: 500vmax; height: 500vmax; -webkit-clip-path: circle(100% at center); clip-path: circle(100% at center); } } .container { display: flex; flex-direction: column; align-items: center; overflow: auto; position: fixed; top: 0px; left: 0px; right: 0px; bottom: 0px; background: var(--background); color: var(--color); } .container:not(.dark) { --background: #fcfbfe; --color:#323133; --sectionBackground: #ffffff; --borderColor: #e9e9e9; --text1:#323133; --text2:#3C3B3D; --shadow:rgba(0,0,0,0.1); --iconColor:#0fce3d; } .container.dark { --background: #1C1B20; --color:#F5F7FA; --sectionBackground: #222126; --borderColor: #252429; --text1:#F5F7FA; --text2:#E6E9ED; --shadow:rgba(0,0,0,0.1); --iconColor:#05c534; } .container *[class*="card"] { background: var(--sectionBackground); color: var(--text1); box-shadow: 0 0 1rem -0.25rem var(--shadow); } .container *[class*="card"].inner-card { border: 1px solid var(--borderColor); } .container .darkmode { position: fixed; bottom: 1rem; left: 1rem; font-size: 1.75rem; padding: 1rem; margin: 1px; border-radius: 100%; border-color: var(--iconColor) !important; box-shadow: 0 0 1rem -0.25rem var(--iconColor), inset 0 0 1rem -0.75rem var(--iconColor); color: var(--iconColor); cursor: pointer; transition: .25s -.05s; } .container .darkmode:hover { box-shadow: 0 0 1rem -0.25rem var(--iconColor), inset 0 0 1rem -0.25rem var(--iconColor); } .container .section-width { max-width: 800px; width: calc(100vw - 8rem); } .container .header { z-index: 1; align-self: stretch; display: flex; flex-direction: row; justify-content: center; position: -webkit-sticky; position: sticky; top: 0; border-bottom: 1px solid var(--borderColor); height: 4rem; line-height: 4rem; font-size: 1.75rem; } .container .header .section-width { display: flex; flex-direction: row; justify-content: space-between; } .container .header .logotext { font-weight: 900; letter-spacing: 1.2rem; color: var(--iconColor); } @-webkit-keyframes flash { 0%, 50%, 100% { color: var(--iconColor); } 70%, 80% { color: var(--text1); } } @keyframes flash { 0%, 50%, 100% { color: var(--iconColor); } 70%, 80% { color: var(--text1); } } body .container .page { position: relative; margin: 2rem 0; padding: 2rem; } body .container .page:not(:last-child) { margin-bottom: 0rem; } body .container .page:target-within { border-color: var(--iconColor) !important; } body .container .page h1, body .container .page h2, body .container .page h3, body .container .page h4, body .container .page h5, body .container .page h6 { margin: 0; } body .container .page h1:last-child, body .container .page h2:last-child, body .container .page h3:last-child, body .container .page h4:last-child, body .container .page h5:last-child, body .container .page h6:last-child { margin-bottom: 0; } body .container .page h1 { display: flex; flex-direction: row; justify-content: space-between; padding-bottom: 1rem; border-bottom: 1px solid var(--borderColor); } body .container .page h1 a { text-decoration: none; font-weight: 200; } body .container .page h1 a:hover { font-weight: 400; } body .container .page p:last-child { margin-bottom: 0; } body .container .page a { color: var(--iconColor); } body .container .page a:not(:hover) { text-decoration: none; } body .container .page i { color: var(--text2); font-family: "Poly", sans-serif; } body .container .page ul { list-style: none; padding-left: 1rem; } body .container .page ul li:before { content: "•"; margin-right: 1rem; color: var(--iconColor); } body .container .page > div[id] { position: absolute; top: calc(-6rem - 2px); } body iframe { top: unset !important; bottom: 9px; right: 25px !important; } |
function.js
The final step, Create a JavaScript file named ‘function.js‘ and put the codes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
// Code By Webdevtrick ( https://webdevtrick.com ) let buttonenabled = true, scroll = 0; $(document).on("click", ".darkmode", function(){ if(!buttonenabled) return; buttonenabled = false; $(".clip").html($("body >.container")[0].outerHTML); scrollbind($(".clip .container")); $(".clip .container").toggleClass("dark").scrollTop(scroll); $(".clip .darkmode").toggleClass("fa-moon").toggleClass("fa-sun"); $(".clip").addClass("anim"); setTimeout(function(){ $("body >.container").replaceWith($(".clip").html()) scrollbind($("body >.container")); $("body >.container").scrollTop(scroll); $(".clip").html("").removeClass("anim"); buttonenabled = true; }, 1000); }); const scrollbind = el => el.bind("scroll", function(){ scroll = $(this).scrollTop(); if($(".container").length > 1) $(".container").scrollTop(scroll); }); scrollbind($(".container")); |
That’s It. Now you have successfully created CSS Dark Mode Toggle With Clip Path Animation, Dark / Light Mode Switch. If you have any doubt or question comment down below.
Thanks For Visiting, Keep Visiting.
U R FANTASTIC
thanks
Unfortunately in Line 51 of the HTML source code is a typo error
…
… should read
fixed, check again
cool … its fixed and working