반응형
기본 인터페이스는 두 개의 입력 필드가 있는 "팁 계산기"라는 컨테이너로 구성됩니다. 하나는 청구 금액을 위한 것이고 다른 하나는 팁 비율을 위한 것입니다. 팁 계산기는 CSS를 사용하여 스타일이 지정된 현대적인 디자인을 갖추고 있습니다. 우리는 JavaScript를 활용하여 두 입력 필드의 값을 얻고 이 값을 기준으로 총액을 계산했습니다.
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tip Calculator</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<h1>Tip Calculator</h1>
<p>Enter the bill amount and tip percentage to calculate the total.</p>
<label for="bill">Bill amount:</label>
<input type="number" id="bill">
<br/>
<label for="tip">Tip percentage:</label>
<input type="number" id="tip">
<br/>
<button id="calculate">Calculate</button>
<br/>
<label for="total">Total:</label>
<span id="total"></span>
</div>
<script src="index.js"></script>
</body>
</html>
index.js
const btnEl = document.getElementById("calculate");
const billInput = document.getElementById("bill");
const tipInput = document.getElementById("tip");
const totalSpan = document.getElementById("total");
function calculateTotal() {
const billValue = billInput.value;
const tipValue = tipInput.value;
const totalValue = billValue * (1 + tipValue / 100);
totalSpan.innerText = totalValue.toFixed(2);
}
btnEl.addEventListener("click", calculateTotal);
style.css
* {
box-sizing: border-box;
}
body {
background-color: #f2f2f2;
font-family: "Helvetica", sans-serif;
}
.container {
background-color: white;
max-width: 600px;
margin: 100px auto;
padding: 20px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
border-radius: 10px;
}
h1 {
margin-top: 0;
text-align: center;
}
input {
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
margin: 10px 0;
width: 100%;
}
button {
background-color: #4caf50;
color: white;
padding: 10px;
border: none;
cursor: pointer;
margin: 10px 0;
width: 100%;
font-size: 18px;
text-transform: uppercase;
transition: background-color 0.2s ease;
}
button:hover {
background-color: #45a049;
}
#total {
font-size: 24px;
font-weight: bold;
margin-top: 10px;
}
반응형