Vue入門實戰-實現簡單的計數器
阿新 • • 發佈:2021-02-07
技術標籤:Vuevuejsjavascriptcsshtml5
效果圖
實現程式碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>計數器</title>
<link rel="stylesheet" href="./css/index.css" />
</head>
<body>
<!-- html結構 -->
<div id="app">
<!-- 計數器功能區域 -->
<div class="input-num" >
<!-- 減少按鈕 -->
<button @click="sub">-</button>
<!-- 顯示的數字 -->
<span>{{result}}</span>
<!-- 增加按鈕 -->
<button @click="add">+</button>
</div>
</div>
<!-- 開發環境版本,包含了有幫助的命令列警告 -->
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<!-- 編碼 -->
<script>
var vue = new Vue({
el: ".input-num",
data: {
result: 0,
},
methods: {
add() {
if (this.result >= 5) {
alert("已經超過5了!!");
} else {
this.result++;
}
},
sub() {
if (this.result <= 0) {
alert("已經小於0了!!");
} else {
this.result--;
}
},
},
});
</script>
</body>
</html>
CSS
body{
background-color: #f5f5f5;
}
#app {
width: 480px;
height: 80px;
margin: 200px auto;
}
.input-num {
margin-top:20px;
height: 100%;
display: flex;
border-radius: 10px;
overflow: hidden;
box-shadow: 4px 4px 4px #adadad;
border: 1px solid #c7c7c7;
background-color: #c7c7c7;
}
.input-num button {
width: 150px;
height: 100%;
font-size: 40px;
color: #ad2a27;
cursor: pointer;
border: none;
outline: none;
background-color:rgba(0, 0, 0, 0);
}
.input-num span {
height: 100%;
font-size: 40px;
flex: 1;
text-align: center;
line-height: 80px;
font-family:auto;
background-color: white;
}
img{
float: right;
margin-top: 50px;
}