Welcome
We've been working hard

How to manually add a night mode to your website

Today, Xiaoma SEO will share with you how to manually add a night mode to your website, hoping to help you. Please pay more attention to Xiaoma SEO blog.

First, we need to open the“ footer.php ”Files, in“ </body></html> ”Add the following code before:

 <script type="text/javascript"> function switchNightMode(){ var night = document.cookie.replace(/(?:(?:^|.*;\s*)night\s*\=\s*([^;]*).*$)|^.*$/, "$1") || '0'; if(night == '0'){ document.body.classList.add('night'); document.cookie = "night=1;path=/" Console. log ('night mode on '); }else{ document.body.classList.remove('night'); document.cookie = "night=0;path=/" Console. log ('night mode off '); } } </script>

Save the file and refresh it.

If you want to achieve Automatically switch to night mode The following codes (Copy directly):

 <script type="text/javascript"> function switchNightMode(){ var night = document.cookie.replace(/(?:(?:^|.*;\s*)night\s*\=\s*([^;]*).*$)|^.*$/, "$1") || '0'; if(night == '0'){ document.body.classList.add('night'); document.cookie = "night=1;path=/" Console. log ('night mode on '); }else{ document.body.classList.remove('night'); document.cookie = "night=0;path=/" Console. log ('night mode off '); } } (function(){ if(document.cookie.replace(/(?:(?:^|.*;\s*)night\s*\=\s*([^;]*).*$)|^.*$/, "$1") === ''){ if(new Date().getHours() > 22 || new Date().getHours() < 5){ document.body.classList.add('night'); document.cookie = "night=1;path=/"; Console. log ('Night mode automatically starts'); }else{ document.body.classList.remove('night'); document.cookie = "night=0;path=/"; Console. log ('Night mode automatically turns off '); } }else{ var night = document.cookie.replace(/(?:(?:^|.*;\s*)night\s*\=\s*([^;]*).*$)|^.*$/, "$1") || '0'; if(night == '0'){ document.body.classList.remove('night'); }else if(night == '1'){ document.body.classList.add('night'); } } })();</ script>

22 and 5 in the code start at 22:00 p.m. and end at 5:00 the next day. In fact, this code is not rigorous. Why do you say that? This code is effective for websites that do not record cookies. Once the night mode is manually turned on or off, it will automatically fail. In short, there is no way to perfectly fit the cookies in the browser unless the cookies are cleared (my technology is not good). In fact, we can make a judgment in js, that is, whether the cookies are in the night mode at 22:00 every day, If not, a dialog box will pop up asking whether to enable night mode. If yes, there will be no prompt.

Then open the "header. php" file of the website. We need to fill in a button for the website to manually turn on and off the night mode:

 <a class="at-night" href="javascript:switchNightMode()" target="_self"></a>

Copy the above code, place it where you think it is appropriate, then save it, log in to the background, clear the theme template cache, compile, open the home page, and test whether the night mode is effective.

In fact, the tutorial is generally completed until now, because you will find in the process of testing that turning on the night mode has no effect,,,, and yes, because you do not have css that can adapt to the night mode, this tutorial cannot be written, because the div framework and css names of each theme template are different and cannot be unified, so you need to find the corresponding class class by yourself, Then add the style of night mode, for example:

 Body.night DIV name{ background-color: #263238; color: #aaa;  }

In this way, the background of the night mode will be changed to black, and the font will be white. If there are too many css frameworks on the website, I believe this is definitely a big hole. Oh, yes, to prevent overlapping css styles, it is recommended to adapt the code of night mode at the bottom of the css style sheet.

After all these configurations are completed, the front desk will check the home page, and you will find that the flash screen will appear in the night mode. That is, the moment you open a page, it is the normal mode, and then it will switch to the night mode after 1 second (when the computer speed is slow, it can be more than 1 second). Wow, it is simply blind? Don't worry. I'll teach you a way. Open it“ header.php ”Files, in <body> Add the following code on:( This code is only applicable to Z-Blog

 <body class="{if GetVars('night','COOKIE') } night{/if}">

Other procedures( TP5 or Typecho, etc )Use this:

 <body class="<?php echo($_COOKIE['night'] == '1' ? 'night' : ''); ?>">

This solves the flash screen bug. When the cookie related fields are detected, directly output the body's class as night, which can prevent the page from flashing. Henan SEO reminds everyone here that you should make a backup before modifying the code, just in case.

Like( zero ) Reward
Do not reprint without permission: New Start Blog » How to manually add a night mode to your website


Follow the public account "New Start Software Steward"

Get the latest network resources and cracking software!
Play with all kinds of software

comment seventy

You must log in before commenting!

 

  1. #61

    acx__${98991*97996}__::. x

    lxbfYeaa Three years ago (2021-11-17)
  2. #62

    555<anMltpC<

    lxbfYeaa Three years ago (2021-11-17)
  3. #63

    “acxzzzzzzzzbbbccccdddeeexca”.replace(“z”,”o”)

    lxbfYeaa Three years ago (2021-11-17)
  4. #64

    555begT(9271)

    lxbfYeaa Three years ago (2021-11-17)
  5. #65

    555CBMAG[!+!]

    lxbfYeaa Three years ago (2021-11-17)
  6. #66

    555begT(9948)

    lxbfYeaa Three years ago (2021-11-17)

Reward the author of the article if you think it is useful

Thank you very much for your reward, we will continue to give more high-quality content, let's create a better online world together!

Scan Alipay and reward

Scan WeChat and reward