JS implements a simple counter

JS implements a simple counter

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:
  • JavaScript implements the basic method of counter
  • How to create a simple counter using JavaScript
  • The counter automatically increases by 1 per second under javascript
  • Help me write a counter using JAVASCRIPT
  • Native JS implements a very good-looking counter

<<:  Reduce memory and CPU usage by optimizing web pages

>>:  VMware installation of CentOS virtual machine and configuration network graphic tutorial

Recommend

Example code for css flex layout with automatic line wrapping

To create a flex container, simply add a display:...

How to manage users and groups when running Docker

Docker is a management tool that uses processes a...

Detailed process record of nginx installation and configuration

Table of contents 1 Introduction to nginx 1 What ...

Markup Language - Image Replacement

Click here to return to the 123WORDPRESS.COM HTML ...

Detailed explanation of common methods of Vue development

Table of contents $nextTick() $forceUpdate() $set...

Use semantic tags to write your HTML compatible with IE6,7,8

HTML5 adds more semantic tags, such as header, fo...

How to use less in WeChat applet (optimal method)

Preface I am used to writing less/sass, but now I...

The difference between html Frame, Iframe and Frameset

10.4.1 The difference between Frameset and Frame ...

How to set directory whitelist and IP whitelist in nginx

1. Set a directory whitelist: Do not set restrict...

Several ways to implement inheritance in JavaScript

Table of contents Structural inheritance (impleme...

Learn MySQL in a simple way

Preface The database has always been my weak poin...

Detailed explanation of CSS multiple three-column adaptive layout implementation

Preface In order to follow the conventional WEB l...