代码拉取完成,页面将自动刷新
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>drink-water</title>
<style>
*{
margin: 0;
padding: 0;
}
body {
width: 100vw;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
background-color: #3494e4;
color: #fff;
}
.bottom {
width: 180px;
height: 400px;
border: 5px solid #144fc6;
border-bottom-left-radius: 50px;
border-bottom-right-radius: 50px;
background-color: #fff;
color: #144fc6;
overflow: hidden;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.empty {
width: 100%;
height: 100%;
font-size: 24px;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
transition: all 0.3s ease;
}
.empty p {
font-size: 16px;
font-weight: 100;
}
.water {
flex:1;
width: 100%;
font-weight: bold;
font-size: 24px;
color: #144fc6;
background-color: #3494e4;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.cups {
width: 280px;
display: flex;
flex-wrap: wrap;
justify-content: center;
align-items: center;
}
.cup {
width: 50px;
height: 90px;
border: 3px solid #144fc6;
border-radius: 0 0 15px 15px;
margin: 5px;
background-color: rgba(255, 255, 255, .8);
color: #144fc6;
cursor: pointer;
font-size: 16px;
display: flex;
justify-content: center;
align-items: center;
}
.full {
color: #fff;
background-color: #6ab3f8;
}
.hidden {
visibility: hidden;
}
</style>
</head>
<body>
<h1>Drink Water</h1>
<p>Goal : 2 Liters</p>
<div class="bottom">
<div class="empty">
2L
</div>
<div class="water hidden">0%</div>
</div>
<p>Select how many glasses of water that you have drank</p>
<div class="cups">
<div class="cup">250ml</div>
<div class="cup">250ml</div>
<div class="cup">250ml</div>
<div class="cup">250ml</div>
<div class="cup">250ml</div>
<div class="cup">250ml</div>
<div class="cup">250ml</div>
<div class="cup">250ml</div>
</div>
<script>
const cups = document.querySelectorAll('.cup');
const empty = document.querySelector('.empty');
const water = document.querySelector('.water')
let current = 0 //当前占比
let goal = 2
cups.forEach((cup,index)=>{
cup.addEventListener('click',(e)=>{
if(cup.classList.contains('full')){
// 在这之后的全部置空
for(let i=index;i<cups.length;i++){
cups[i].classList.remove('full')
}
current = 100 - (cups.length - index )*12.5
// console.log(current)
}else {
// 在这之前的全部填满
for(let i=0;i<=index;i++){
cups[i].classList.add('full')
}
current = 12.5 * (index + 1)
}
empty.style.height = (100 - current) + '%'
// 空
if(current === 0){
water.classList.add('hidden')
empty.classList.remove('hidden')
}else if(current === 100){
empty.classList.add('hidden')
water.classList.remove('hidden')
}else {
water.classList.remove('hidden')
empty.classList.remove('hidden')
}
empty.innerHTML = `${2 - 2 * current / 100}L`
water.innerHTML = `${current} %`
})
})
</script>
</body>
</html>
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。