欢迎访问我的博客地址 : 博客地址
只需一行代码,轻轻一点就可以把任意网站变成暗黑模式。
1、document.documentElement 获取文档对象的根元素,即元素
2、给html元素的.style样式添加filter滤镜样式为invert(85%) hue-rotate(180deg)
3、invert() 反转图像。
4、hue-rotate()色相旋转。
document.documentElement.style.filter='invert(85%) hue-rotate(180deg)'
(function () { const docStyle = document.documentElement.style; if (!window.modeIndex) { window.modeIndex = 0; } const styleList = [ '', 'invert(85%) hue-rotate(180deg)', 'invert(100%) hue-rotate(180deg)', ]; modeIndex = modeIndex >= styleList.length - 1 ? 0 : modeIndex + 1; docStyle.filter = styleList[modeIndex]; document.body.querySelectorAll('img, picture, video').forEach(el => el.style.filter = modeIndex ? 'invert(1) hue-rotate(180deg)' : '');})();