JS實(shí)現(xiàn)簡(jiǎn)單計(jì)數(shù)器
用HTML CSS和JavaScript實(shí)現(xiàn)簡(jiǎn)單計(jì)數(shù)器,有加、減和零三個(gè)按鈕,分別實(shí)現(xiàn)加一、減一和歸零操作。下面先看一下效果圖。

HTML代碼
<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" οnclick="add()" class="btn1">+</button>
<button type="button" οnclick="zero()" class="btn2">零</button>
<button type="button" οnclick="sub()" class="btn1">-</button>
</div>
</div>
CSS代碼
.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:彈性盒子display:flex布局+margin:auto可實(shí)現(xiàn)完美居中。
JS代碼
<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>
以上代碼即可實(shí)現(xiàn)效果。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
javascript自動(dòng)給文本url地址增加鏈接的方法分享
這篇文章主要介紹了javascript自動(dòng)給文本url地址增加鏈接的方法,有需要的朋友可以參考一下2014-01-01
js中對(duì)函數(shù)設(shè)置默認(rèn)參數(shù)值的3種方法
這篇文章主要介紹了js中對(duì)函數(shù)設(shè)置默認(rèn)參數(shù)值的3種方法嗎,3種方法都具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2015-10-10
JavaScript實(shí)現(xiàn)ASC轉(zhuǎn)漢字及漢字轉(zhuǎn)ASC的方法
這篇文章主要介紹了JavaScript實(shí)現(xiàn)ASC轉(zhuǎn)漢字及漢字轉(zhuǎn)ASC的方法,涉及JavaScript編碼轉(zhuǎn)換的相關(guān)技巧,需要的朋友可以參考下2016-01-01
JS使用canvas繪制旋轉(zhuǎn)風(fēng)車動(dòng)畫
這篇文章主要為大家詳細(xì)介紹了JS使用canvas繪制旋轉(zhuǎn)風(fēng)車動(dòng)畫,有加速減速啟動(dòng)停止功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
javascript實(shí)現(xiàn)異形滾動(dòng)輪播
這篇文章主要為大家詳細(xì)介紹了javascript實(shí)現(xiàn)異形滾動(dòng)輪播,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11

