This time I will show you how to achieve the switch effect with JS, and what are the precautions for JS to achieve the switch effect. The following is a practical case, let's take a look.
Using JavaScriptAchieving the switch effect is also an operation that many people will encounter. This article will give you a brief introduction to the method. If you are interested, take a look.
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>开关灯</title> <style type="text/css"> html, body { margin: 0px; padding: 0px; width: 100%; height: 100%; cursor: pointer; background-color: white; } </style> </head> <body id="bodyEle"> <script type="text/javascript"> var oBody = document.getElementById("bodyEle"); oBody.onclick = function () { var bg = this.style.backgroundColor; switch (bg) { case "white": this.style.backgroundColor = "red"; break; case "red": this.style.backgroundColor = "black"; break; default: this.style.backgroundColor = "white"; } } </script> </body> </html>
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
The above is the detailed content of How to achieve switch effect in JS. For more information, please follow other related articles on the PHP Chinese website!