Use javascript to add night mode for webpages

Source: Internet
Author: User

How can I add the night mode function to a Web page? In fact, the so-called night mode adds a transparent mask layer on the page, but the mask layer blocks the page elements. The solution is to add a DIV, A large outline-width value is given to the outline attribute of the DIV, And the outline border is used as the mask. This allows you to click the page element normally and achieve the effect of the night mode.

HTML + CSS: the code is as follows: <div class = "cover"> </div> the code is as follows: <style>. cover {position: fixed; top: 0px; left: 0px; outline: 5000px solid rgba (0, 0, 0, 0.3); z-index: 99999 ;} </style> then use JavaScript to write a nighttime mode plus: the code is as follows: <script> var brightness; // display mask function cover (brightness) {if (typeof (div) = 'undefined') {div = document. createElement ('div '); div. setAttribute ('style', 'position: fixed; top: 0; left: 0; outline: 5000px solid; z-index: 99999; '); document. body. appendChild (div);} else {div. style. display = '';} div. style. outlineColor = 'rgba (0, 0, 0, '+ brightness +') ';} // event listening window. addEventListener ('keylow', function (e) {if (e. altKey & e. keyCode = 90) {// Alt + Z: Enable night mode cover (brightness = 0.3);} if (e. altKey & e. keyCode = 88) {// Alt + X: Disable cover (brightness = 0);} if (e. altKey & e. keyCode = 38) {// Alt + brightness: Increase the brightness if (brightness-0.05> 0.05) cover (brightness-= 0.05);} if (e. altKey & e. keyCode = 40) {// Alt + brightness: brightness reduction if (brightness + 0.05 <0.95) cover (brightness + = 0.05) ;}}, false ); </script> You can also write a GreaseMonkey script to add the night mode to any page as a browser extension.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.