Maison  >  Article  >  interface Web  >  使用javascript为网页增加夜间模式_javascript技巧

使用javascript为网页增加夜间模式_javascript技巧

WBOY
WBOYoriginal
2016-05-16 17:02:323019parcourir

HTML+CSS:

复制代码 代码如下:


复制代码 代码如下:


接着用JavaScript写个夜间模式plus:

复制代码 代码如下:

<script><BR>var brightness;<BR>//显示遮罩<BR>function cover(brightness) {<BR> if (typeof(div) == 'undefined') {<BR> div = document.createElement('div');<BR> div.setAttribute('style', 'position:fixed;top:0;left:0;outline:5000px solid;z-index:99999;');<BR> document.body.appendChild(div);<BR> } else {<BR> div.style.display = '';<BR> }<BR> div.style.outlineColor = 'rgba(0,0,0,' + brightness + ')';<BR>}<BR>//事件监听<BR>window.addEventListener('keydown', function(e) {<BR> if (e.altKey && e.keyCode == 90) { //Alt+Z:打开夜间模式<BR> cover(brightness = 0.3);<BR> }<BR> if (e.altKey && e.keyCode == 88) { //Alt+X:关闭<BR> cover(brightness = 0);<BR> }<BR> if (e.altKey && e.keyCode == 38) { //Alt+↑:增加亮度<BR> if (brightness - 0.05 > 0.05) cover(brightness -= 0.05);<BR> }<BR> if (e.altKey && e.keyCode == 40) { //Alt+↓:降低亮度<BR> if (brightness + 0.05 < 0.95) cover(brightness += 0.05);<BR> }<BR>}, false);<BR></script>

还可以写成GreaseMonkey脚本,作为浏览器扩展给任意页面增加夜间模式

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn