快速为网站添加暗黑模式(DarkMode.js)

1.Add a dark-mode / night-mode to your website in a few seconds

This library uses the css mix-blend-mode in order to bring Dark-mode to any of your websites. Just copy paste the snippet and you will get a widget to turn on and off the dark-mode. You can also use it without the widget programmatically. The plugin is lightweight, built in VanillaJS. It also uses localstorage by default so your last setting will be remembered !

I have been inspired by this article: https://dev.to/wgao19/night-mode-with-mix-blend-mode-difference-23lm

2.Features

  • Widget appears automatically
  • Saving users choice
  • Automatically shows Darkmode if the OS preferred theme is dark (if the browsers supports prefers-color-scheme)
  • Can be used programmatically without widget

3.Demo

Check out the demo in these websites (there is a button on the bottom, right hand corner, just press it!):

4.Wordpress plugins

If you are using Wordpress you may want have a look to these plugins based on Darkmode.js:

5.Nuxt.js module

If you are using Nuxt.js there is a module for Darkmode.js:

6.How to use

Darkmode.js is very easy to use, just copy paste the following code or use the npm package.

7.Easy way (using the JSDelivr CDN)

Just add this code to your html page:

<script src="https://cdn.jsdelivr.net/npm/darkmode-js@1.5.7/lib/darkmode-js.min.js"></script>
<script>
  function addDarkmodeWidget() {
    new Darkmode().showWidget();
  }
  window.addEventListener('load', addDarkmodeWidget);
</script>

8.Using NPM

npm install darkmode-js

Then add the following javascript code:

import Darkmode from 'darkmode-js';

new Darkmode().showWidget();

9.Options

Here are the option availables:

const 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: ''
  autoMatchOsTheme: true // default: true
}

const darkmode = new Darkmode(options);
darkmode.showWidget();

10.Methods

If you don't want to show the widget and enable/disable Darkmode programatically you can use the method toggle(). You can also check if the darkmode is activated with the method isActivated(). See them in action in the following example.

const darkmode =  new Darkmode();
darkmode.toggle();
console.log(darkmode.isActivated()) // will return true

11.Override style

  • A CSS class darkmode--activated is added to the body tag when the darkmode is activated. You can take advantage of it to override the style and have a custom style
  • Use the class darkmode-ignore where you don't want to apply darkmode
  • You can also add this style: isolation: isolate; in your css, this will also ignore the darkmode.
  • It is also possible to revert the dark-mode with this style mix-blend-mode: difference;

12.Examples

.darkmode--activated p, .darkmode--activated li {
  color: #000;
}

.button {
  isolation: isolate;
}

.darkmode--activated .logo {
  mix-blend-mode: difference;
}
<span class="darkmode-ignore">😬<span>

13.Debug

If it does not work you may have to add the following code, but this will invalidate the classes to override.

.darkmode-layer, .darkmode-toggle {
  z-index: 500;
}

14.Browser compatibility

This library uses the CSS mix-blend-mode: difference; in order to provide the Dark Mode. It may not be compatible with all the browsers. Therefore the widget has been hidden in Internet Explorer and Edge. This library also uses prefers-color-scheme: dark to automatically enable the Dark Mode if the OS prefered theme is dark.

Check the compatibility here:

  • https://caniuse.com/#search=mix-blend-mode
  • https://caniuse.com/#search=prefers-color-scheme (to activate Dark Mode automatically)

15.Development

  • yarn build or npm run build - produces production version of your library under the lib folder
  • yarn dev or npm run dev - produces development version of your library and runs a watcher
  • yarn test or npm run test - it runs the tests 😃
  • yarn test:watch or npm run test:watch - same as above but in a watch mode
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Dark Mode 是一种较新的设计趋势,它可以在不同操作系统或应用程序中自动切换白天或黑夜模式。如果您想禁用Dark Mode并始终使用浅色主题,可以按照以下步骤: 1. 在<head>标签中添加以下元标记: ```html <meta name="color-scheme" content="light"> ``` 2. 在<style>标签中添加以下样式规则: ```html @media (prefers-color-scheme: dark) { body { color: black; background: white; } } ``` 这样设置后,无论用户的操作系统或浏览器是否启用 Dark Mode,都将强制使用浅色主题。 如果您希望您的网站能够自适应 Dark Mode 主题,可以使用 CSS 变量和 JavaScript 来实现。以下是一些示例代码: 1. 使用 CSS 变量 ```css :root { --background-color: white; --text-color: black; } @media (prefers-color-scheme: dark) { :root { --background-color: black; --text-color: white; } } body { background-color: var(--background-color); color: var(--text-color); } ``` 2. 使用 JavaScript ```javascript const prefersDarkScheme = window.matchMedia("(prefers-color-scheme: dark)"); if (prefersDarkScheme.matches) { document.documentElement.style.setProperty('--background-color', 'black'); document.documentElement.style.setProperty('--text-color', 'white'); } else { document.documentElement.style.setProperty('--background-color', 'white'); document.documentElement.style.setProperty('--text-color', 'black'); } ``` 这些代码将根据用户的操作系统设置,自动切换网站的主题。您可以将 CSS 变量和 JavaScript 代码放在单独的文件中,并在<head>标签中引用它们,以便在整个网站中重用它们。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值