Heim  >  Artikel  >  Web-Frontend  >  使用javascript为网页增加夜间模式_javascript技巧

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

WBOY
WBOYOriginal
2016-05-16 17:02:323019Durchsuche

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脚本,作为浏览器扩展给任意页面增加夜间模式

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn