Now that macOS and iOS all support Dark Mode, it’s time to keep up with the design trend for our websites and use Darkmode.js, a js script that makes it easy to switch between dark-mode / night-mode in a second.
All you need to do is put the following code into the web code.
- <script src=”https://cdn.jsdelivr.net/npm/darkmode-js@1.3.4/lib/darkmode-js.min.js”></script>
- <script>
- new Darkmode().showWidget();
- </script>
You can then use the option code to personalise the style and position of the mode switch button, such as left or right, the base colour, the trigger time, the icon inside and other elements.
- var options = {
- bottom: ’64px’, // default: ’32px’
- right: ‘unset’, // default: ’32px’
- left: ’32px’, // default: ‘unset’
- time: ‘0.5s’, // default: ‘0.3s’
- mixColor: ‘#fff’, // default: ‘#fff’
- backgroundColor: ‘#fff’, // default: ‘#fff’
- buttonColorDark: ‘#100f2c’, // default: ‘#100f2c’
- buttonColorLight: ‘#fff’, // default: ‘#fff’
- saveInCookies: false, // default: true,
- label: ‘?’ // default: ” }
- const darkmode = new Darkmode(options); darkmode.showWidget();