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
Brief description Suitable for readers: Mobile de...
0x0 Introduction First of all, what is a hash alg...
This article uses an example to describe how to u...
mysql master-slave configuration 1. Preparation H...
1. Create a new rtmp directory in the nginx sourc...
Effect display: Environment preparation controlle...
Table of contents Preface Global Lock Full databa...
Table of contents Preface Front-end structure Bac...
*******************Introduction to HTML language (...
1. Command Introduction bzip2 is used to compress...
Table of contents 1. Ref and reactive 1. reactive...
1.1 General marking A general tag consists of an ...
The Kubernetes team recently announced that it wi...
This article uses the "Attribution 4.0 Inter...
Subquery in MySql database: Subquery: nesting ano...