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

A collection of possible problems when migrating sqlite3 to mysql

Brief description Suitable for readers: Mobile de...

Nest.js hashing and encryption example detailed explanation

0x0 Introduction First of all, what is a hash alg...

Record the whole process of MySQL master-slave configuration based on Linux

mysql master-slave configuration 1. Preparation H...

Nginx builds rtmp live server implementation code

1. Create a new rtmp directory in the nginx sourc...

The most comprehensive explanation of the locking mechanism in MySQL

Table of contents Preface Global Lock Full databa...

How to manage large file uploads and breakpoint resume based on js

Table of contents Preface Front-end structure Bac...

HTML tutorial, easy to learn HTML language (2)

*******************Introduction to HTML language (...

Use of Linux bzip2 command

1. Command Introduction bzip2 is used to compress...

Detailed explanation and extension of ref and reactive in Vue3

Table of contents 1. Ref and reactive 1. reactive...

HTML basic syntax is convenient for those who are just starting to learn HTML

1.1 General marking A general tag consists of an ...

Will the deprecated Docker be replaced by Podman?

The Kubernetes team recently announced that it wi...

Tutorial on using Docker Compose to build Confluence

This article uses the "Attribution 4.0 Inter...

A Brief Analysis of Subqueries and Advanced Applications in MySql Database

Subquery in MySql database: Subquery: nesting ano...