Use HTML CSS and JavaScript to implement a simple counter with three buttons: plus, minus and zero, which respectively implement the operations of adding one, subtracting one and returning to zero. Let’s take a look at the effect diagram first. HTML Code <div class="body"> <div class="text"> <font>Counter</font> </div> <div class="count" > <span id="demo" class="ft"> 2 </span> </div> <div class="btn"> <button type="button" click="add()" class="btn1">+</button> <button type="button" click="zero()" class="btn2">Zero</button> <button type="button" click="sub()" class="btn1">-</button> </div> </div> CSS Code .body { width: 300px; height: 500px; background-color: #211d5a; border-radius: 20px; display: flex; flex-direction: column; align-items: center; } .text { font-size: 24px; color: white; margin-top: 60px; text-shadow: 2px 2px 2px #fff; } .count { position: relative; width: 200px; height: 200px; margin-top: 60px; border: 10px solid; border-color: rgb(79, 59, 156); border-radius: 50%; display: flex; } .ft { font-size: 100px; color: #fff; /*left: 50%; margin-left: -102px; margin-top: 40px;*/ margin: auto; } .btn { width: 220px; display: flex; /*flex-direction: row;*/ justify-content: space-between; margin-top: 60px; } .btn1 { width: 50px; height: 30px; border: 0px; border-radius: 4px; background-color: rgb(79, 59, 156); font-size: 20px; color: #efdaff; } .btn2 { width: 50px; height: 30px; border: 0px; border-radius: 4px; background-color: rgb(79, 59, 156); font-size: 22px; color: #efdaff; } Tips: Flexible box display: flex layout + margin: auto can achieve perfect centering. JS code <script> var counter = document.getElementById("demo").innerHTML; function add() { counter++; document.getElementById("demo").innerHTML = counter; } function sub() { if(counter == 0) { counter = 0; } else { counter--; document.getElementById("demo").innerHTML = counter; } } function zero() { counter = 0; document.getElementById("demo").innerHTML = counter; } </script> The above code can achieve the effect. The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: Reduce memory and CPU usage by optimizing web pages
>>: VMware installation of CentOS virtual machine and configuration network graphic tutorial
To create a flex container, simply add a display:...
Docker is a management tool that uses processes a...
Table of contents 1 Introduction to nginx 1 What ...
Click here to return to the 123WORDPRESS.COM HTML ...
Table of contents $nextTick() $forceUpdate() $set...
HTML5 adds more semantic tags, such as header, fo...
Preface I am used to writing less/sass, but now I...
Table of contents Overview 1. Menu and routing pr...
Copy code The code is as follows: <!DOCTYPE ht...
10.4.1 The difference between Frameset and Frame ...
1. Set a directory whitelist: Do not set restrict...
css3 background image related Compatibility: IE9+...
Table of contents Structural inheritance (impleme...
Preface The database has always been my weak poin...
Preface In order to follow the conventional WEB l...