原生JS實現(xiàn)非常好看的計數(shù)器
更新時間:2021年10月13日 11:31:29 作者:aiguangyuan
這篇文章主要為大家詳細介紹了原生JS實現(xiàn)非常好看的計數(shù)器,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
今天給大家分享一個用原生JS實現(xiàn)的好看計數(shù)器,效果如下:

以下是代碼實現(xiàn),歡迎大家復(fù)制粘貼和收藏。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>原生JS實現(xiàn)一個好看計數(shù)器</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>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
利用JS測試目標(biāo)網(wǎng)站的打開響應(yīng)速度
本文簡單說明利用JS來測試目標(biāo)網(wǎng)站的打開響應(yīng)速度,方法簡單明了大家一看就明白并附上了腳本源碼2017-12-12
js window.print實現(xiàn)打印特定控件或內(nèi)容
window.print可以打印網(wǎng)頁,但有時候我們只希望打印特定控件或內(nèi)容,怎么辦呢?可以把要打印的內(nèi)容放在div中,然后用下面的代碼進行打印,希望對大家有所幫助2013-09-09
js導(dǎo)出table數(shù)據(jù)到excel即導(dǎo)出為EXCEL文檔的方法
導(dǎo)出table為EXCEL文檔的方法有很多,在本文為大家介紹下js中時如何做到的,感興趣的朋友可以參考下2013-10-10
JavaScript中判斷數(shù)據(jù)類型的方法總結(jié)
這篇文章主要為大家詳細介紹了一些JavaScript中判斷數(shù)據(jù)類型的方法,文中的示例代碼講解詳細,具有一定的學(xué)習(xí)價值,需要的小伙伴可以了解一下2023-07-07
JavaScript使用encodeURI()和decodeURI()獲取字符串值的方法
這篇文章主要介紹了JavaScript使用encodeURI()和decodeURI()獲取字符串值的方法,實例分析了encodeURI()和decodeURI()函數(shù)解析字符串的相關(guān)技巧,需要的朋友可以參考下2015-08-08

