原生JS实现非常好看的计数器

作者:aiguangyuan 时间:2024-04-19 09:59:20 

今天给大家分享一个用原生JS实现的好看计数器,效果如下:

原生JS实现非常好看的计数器

以下是代码实现,欢迎大家复制粘贴和收藏。


<!DOCTYPE html>
<html lang="en">

<head>
   <meta charset="UTF-8">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <title>原生JS实现一个好看计数器</title>
   <style>
       * {
           font-family: '微软雅黑', 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>

来源:https://blog.csdn.net/weixin_40629244/article/details/106724984

标签:js,计数器
0
投稿

猜你喜欢

  • 如何实现表单提交时提示正在发送

    2008-12-23 13:30:00
  • 详解Python查找谁删了你的微信

    2021-01-02 02:32:02
  • Python3多线程爬虫实例讲解代码

    2021-01-10 21:45:28
  • python接入使用百度翻译流程

    2022-11-26 01:01:43
  • Golang的性能优化和调试技巧

    2024-04-25 15:30:24
  • 微信小程序开发注意指南和优化实践(小结)

    2024-04-17 10:38:05
  • 利用d3.js力导布局绘制资源拓扑图实例教程

    2024-05-22 10:35:26
  • ASP短日期格式为长日期

    2009-06-11 12:53:00
  • python中defaultdict方法的使用详解

    2022-06-25 05:07:09
  • python中pyplot直方图的绘制方式

    2023-11-20 07:58:17
  • mysql二进制日志文件恢复数据库

    2024-01-16 10:55:05
  • 学习 Vue.js 遇到的那些坑

    2023-07-02 16:31:54
  • python 获取微信好友列表的方法(微信web)

    2022-02-20 10:05:21
  • Python实现的拟合二元一次函数功能示例【基于scipy模块】

    2023-10-09 00:32:02
  • 对python当中不在本路径的py文件的引用详解

    2022-12-17 15:12:42
  • SQL实现数据过滤流程详解

    2024-01-13 02:52:48
  • SQL Server控制语句的基本应用

    2024-01-24 12:52:28
  • 如何在Go中使用切片容量和长度

    2024-04-28 10:49:16
  • 浅析Go汇编语法和MatrixOne使用介绍

    2023-07-13 19:38:18
  • python Event事件、进程池与线程池、协程解析

    2023-09-06 00:39:19
  • asp之家 网络编程 m.aspxhome.com