drink water by akashnai · Pull Request #459 · codemistic/Web-Development · GitHub
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions drink water/index.html
52 changes: 52 additions & 0 deletions drink water/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const smallCups = document.querySelectorAll('.cup-small')
const liters = document.getElementById('liters')
const percentage = document.getElementById('percentage')
const remained = document.getElementById('remained')

updateBigCup()

smallCups.forEach((cup, idx) => {
cup.addEventListener('click', () => {
highlightCups(idx)
})
})

function highlightCups(idx){
if(smallCups[idx].classList.contains('full') && !smallCups[idx].nextElementSibling.classList.contains('full')) {
idx--;
}
smallCups.forEach((cup, idx2) => {
if(idx2 <= idx) {
cup.classList.add('full')
}
else{
cup.classList.remove('full')
}
})

updateBigCup()
}

function updateBigCup() {
const fullCups = document.querySelectorAll('.cup-small.full').length
const totalCups = smallCups.length

if(fullCups === 0) {
percentage.style.visibility = 'hidden'
percentage.style.height = 0
}
else{
percentage.style.visibility = 'visible'
percentage.style.height = `${fullCups / totalCups * 330}px`
percentage.innerText = `${fullCups / totalCups * 100}%`
}

if(fullCups === totalCups) {
remained.style.visibility = 'hidden'
remained.style.height = 0
}
else{
remained.style.visibility = 'visible'
liters.innerText = `${2 - (250 * fullCups / 1000)}L`
}
}
104 changes: 104 additions & 0 deletions drink water/style.css