首页 > 其他分享 >原生JS实现一个好看计数器

原生JS实现一个好看计数器

时间:2023-02-09 12:32:55浏览次数:40  
标签:原生 index span JS 计数器 num container border display


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

原生JS实现一个好看计数器_ci

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

<!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>

let index = 0;
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');


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>

标签:原生,index,span,JS,计数器,num,container,border,display
From: https://blog.51cto.com/u_15959833/6046806

相关文章

  • P11:JSX代码注释、HTML添加class、JSX中解析html、JSX中label激活文本框
    React16基础​​阐述​​​​JSX代码注释​​​​JSX中的class陷阱​​​​JSX中的html解析问题​​​​JSX中``标签的坑​​阐述通过之前的教程作完“大宝剑”菜单后,如......
  • 2022阿里云云原生年度成绩单来了
    作者:全新出发的阿里云云原生......
  • JSON-概念 JSON-语法定义 JSON-语法-值的获取
    JSON-概念 概念:JavaScript ObjectNotation  JavaScript对象表示法Person p = new Person();p.setName("张三");、p.setAge(23);p.se......
  • js请求后端9
    用JavaScript写AJAX前面已经介绍过了,主要问题就是不同浏览器需要写不同代码,并且状态和错误处理写起来很麻烦。用jQuery的相关对象来处理AJAX,不但不需要考虑浏览器问题,代码也......
  • threejs 第四十六用 ImageBitmapLoader
    threejs交流群511163089这东西。。今天研究worker读url那。发现这个可以用这种bitmap可以读来传给主线程然后创建CanvasTextureloader.load(url,function(res){self.post......
  • js 判断对象里面有没有值
    转载自:https://blog.csdn.net/renfeideboke/article/details/128256858方法一:varobj={}if(Object.values(obj).length>0){//有值}else{//无}方法二:l......
  • three.js第四十九用 连续运算符号封装
    threejs交流群511163089functionSubV(a,b,normalize=false){if(normalize)returna.clone().sub(b).normalize();returna.clone().sub(b);}functionAddV(......
  • Vue.js 3 开源组件推荐:代码差异查看器插件
    一个Vue.js差异查看器插件,可以用来比较两个代码片断之间的差异。Github地址:https://github.com/hoiheart/vue-diff支持语言:cssxml:xml,html,xhtml,rss,atom,xjb,......
  • JSON的概念和语法定义
    JSON的概念概念:JavaScript Object Notation JacaScript对象表示法json现在多用于存储和交换文本信息的语法进行数据的传输JSON比XML更小更快,更易解析 ......
  • jmeter--jsr223组件使用和功能详解
    相比于BeanShell取样器,JSR223取样器具有可大大提高性能的功能(编译)如果需要,一定要使用JSR223取样器编写脚本是更好的选择!!!属性描述名称:显示的此取样器的描述性名称,可自定义......