Native JS implements a very good-looking counter

Native JS implements a very good-looking counter

Today I will share with you a good-looking counter implemented with native JS. The effect is as follows:

The following is the code implementation, you are welcome to copy, paste and collect it.

<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Native JS implements a good-looking counter</title>
    <style>
        * {
            font-family: 'Microsoft YaHei', sans-serif;
            box-sizing: border-box;
            margin: 0;
            padding: 0;
        }
 
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            background: #000d0f;
        }
 
        .container {
            position: relative;
            width: 80px;
            height: 50px;
            border-radius: 40px;
            border: 2px solid rgba(255, 255, 255, 0.2);
            transition: 0.5s;
        }
 
        .container:hover {
            width: 120px;
            border: 2px solid rgba(255, 255, 255, 1);
        }
 
        .container .next {
            position: absolute;
            top: 50%;
            right: 30px;
            display: block;
            width: 12px;
            height: 12px;
            border-top: 2px solid #fff;
            border-left: 2px solid #fff;
            z-index: 1;
            transform: translateY(-50%)rotate(135deg);
            cursor: pointer;
            transition: 0.5s;
            opacity: 0;
        }
 
        .container:hover .next {
            opacity: 1;
            right: 20px;
        }
 
        .container .prev {
            position: absolute;
            top: 50%;
            left: 30px;
            display: block;
            width: 12px;
            height: 12px;
            border-top: 2px solid #fff;
            border-left: 2px solid #fff;
            z-index: 1;
            transform: translateY(-50%)rotate(315deg);
            cursor: pointer;
            transition: 0.5s;
            opacity: 0;
        }
 
        .container:hover .prev {
            opacity: 1;
            left: 20px;
        }
 
        #box span {
            position: absolute;
            display: none;
            width: 100%;
            height: 100%;
            text-align: center;
            line-height: 46px;
            color: #00deff;
            font-size: 24px;
            font-weight: 700;
            user-select: none;
        }
 
        #box span:nth-child(1) {
            display: initial;
        }
    </style>
</head>
 
<body>
    <div class="container">
        <div class="next" onclick="nextNum()"></div>
        <div class="prev" onclick="prevNum()"></div>
        <div id="box">
            <span>0</span>
        </div>
    </div>
 
    <script>
        var numbers = document.getElementById('box')
        for (let i = 0; i < 100; i++) {
            let span = document.createElement('span')
            span.textContent = i
            numbers.appendChild(span)
        }
        let num = numbers.getElementsByTagName('span')
        let index = 0
 
        function nextNum() {
            num[index].style.display = 'none'
            index = (index + 1) % num.length
            num[index].style.display = 'initial'
        }
        function prevNum() {
            num[index].style.display = 'none'
            index = (index - 1 + num.length) % num.length
            num[index].style.display = 'initial'
        }
    </script>
</body>
 
</html>

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:
  • js counter code
  • 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
  • JS implements a simple counter

<<:  Summary of commonly used SQL in MySQL operation tables

>>:  Centos7.3 automatically starts or executes specified commands when booting

Recommend

Superficial Web Design

<br />I have always believed that Yahoo'...

Vue implements sample code to disable browser from remembering password function

Find information Some methods found on the Intern...

How to upgrade all Python libraries in Ubuntu 18.04 at once

What is pip pip is a Python package management to...

About 3 common packages of rem adaptation

Preface I wrote an article about rem adaptation b...

Vue uses echart to customize labels and colors

This article example shares the specific code of ...

Summary on Positioning in CSS

There are four types of positioning in CSS, which...

Hyperlink icon specifications: improve article readability

1. What is the hyperlink icon specification ?<...

Detailed explanation of Kubernetes pod orchestration and lifecycle

Table of contents K8S Master Basic Architecture P...

A brief talk on responsive design

1. What is responsive design? Responsive design i...

IE6 distortion problem

question: <input type="hidden" name=...

How to install and modify the initial password of mysql5.7.18 under Centos7.3

This article shares with you the installation of ...

MySQL 5.7.21 installation and password configuration tutorial

MySQL5.7.21 installation and password setting tut...

Realizing provincial and municipal linkage effects based on JavaScript

This article shares the specific code of JavaScri...

How to solve the front-end cross-domain problem using Nginx proxy

Preface Nginx (pronounced "engine X") i...