HTML配置(bootstrap)
https://getbootstrap.com/docs/4.5/getting-started/introduction/
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
<title>Hello, world!</title>
</head>
<body class="bg-dark"> <!-- //设置颜色 -->
<div class="container">
<div class="row">
<div class="col-md-6 mx-auto">
<div class="card card-body text-center mt-5">
<h1 class="heading display-5 pb-3">Loan calculator</h1>
<form id="loan-form">
<div class="form-group">
<div class="input-group">
<span class="input-group-text">$</span>
<input type="number" class="form-control" id="amount" placeholder="Loan Amount">
</div>
</div>
<div class="form-group">
<div class="input-group">
<span class="input-group-text">%</span>
<input type="number" class="form-control" id="interst" placeholder="Interest">
</div>
</div>
<div class="form-group">
<input type="number" class="form-control" id="years" placeholder="Year To Repay">
</div>
<div class="form-group">
<input type="submit" value="Calculate" class="btn btn-dark btn-block">
</div>
</form>
<!-- LOADER HERE-->
<!--
<div id="loading">
<img src='img/loading.gif' alt="">
</div>
-->
<!-- RESULTS-->
<div id="results" class="pt-4">
<h5>Results</h5>
<div class="form-group">
<div class="input-group">
<span class="input-group-text">Monthly Payment</span>
<input type="number" class="form-control" id="monthly-payment" disabled>
</div>
</div>
<div class="form-group">
<div class="input-group">
<span class="input-group-text">Total Payment</span>
<input type="number" class="form-control" id="total-payment" disabled>
</div>
</div>
<div class="form-group">
<div class="input-group">
<span class="input-group-text">Total Interest</span>
<input type="number" class="form-control" id="total-interest" disabled>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script>
<script src="app.js"></script>
</body>
</html>
设置计算公式——显示+warning
计算+显示结果
设定submit的event;
函数中得到所有input框;
取前三个计算;
给后三个赋值;
warining
若输入的数字不对,设置showerror函数;
即在title上面显示error;
settimeout设置函数,等待执行时间;
//Insert error above heading
card.insertBefore(errorDiv,heading); //(要放入的parent,要放入的before)
//clear error after 3sec
setTimeout(clearError,3000); //3000ms后执行clearError
代码:
//listen for submit
document.getElementById('loan-form').addEventListener('submit',calculateresults);
//calculate result
function calculateresults(e){
console.log('Calculating...');
//UI Vars
const amount = document.getElementById('amount');
const interest=document.getElementById('interest');
const years=document.getElementById("years");
const monthlyPayment=document.getElementById("monthly-payment");
const totalPayment=document.getElementById("total-payment");
const totalInterest=document.getElementById("total-interest");
//得到float类型
const principal=parseFloat(amount.value);
const calculatedInterest = parseFloat(interest.value) /100 /12;
//计算interest
const calculatedPayments= parseFloat(years.value)*12;
//Compute monthly payment
const x = Math.pow(1+calculatedInterest,calculatedPayments);
const monthly = (principal*x*calculatedInterest)/(x-1);
if(isFinite(monthly)){ //检查是否为无穷大,是则说明输入错误
//给下面结果input赋值
monthlyPayment.value=monthly.toFixed(2);//设定小数
totalPayment.value=(monthly*calculatedPayments).toFixed(2);
totalInterest.value=((monthly*calculatedPayments)-principal).toFixed(2);
}else{
showError("Please check your numbers");
}
e.preventDefault();
}
//Show Error
function showError(error){
//Create a div
const errorDiv=document.createElement('div');
//Get elements
const card=document.querySelector(".card"); //得到card
const heading = document.querySelector(".heading"); //heding为title
//Add class
errorDiv.className="alert alert-danger"; //alert-danger设定样式,alert方便后续clear
//Create text node and append to div
errorDiv.appendChild(document.createTextNode(error));
//Insert error above heading
card.insertBefore(errorDiv,heading); //(要放入的parent,要放入的before)
//clear error after 3sec
setTimeout(clearError,3000); //3000ms后执行clearError
}
//Clearerror
function clearError(){
document.querySelector('.alert').remove();
}
加入延时——提高使用体验
(1)设定submit事件——传入函数;
隐藏结果+显示gif;
延时调用计算函数;
//listen for submit
document.getElementById('loan-form').addEventListener('submit',function(e){ //减慢calculateresults的输出
//Hide results
//先隐藏了,算出来再显示
document.getElementById('results').style.display='none';
//Show loader
document.getElementById('loading').style.display='block';
//延时
setTimeout(calculateresults,2000);
e.preventDefault();
});
(2)计算函数
计算并赋值;
成功显示结果,失败显示失败提示;
//calculate result
function calculateresults(e){
//UI Vars
const amount = document.getElementById('amount');
const interest=document.getElementById('interest');
const years=document.getElementById("years");
const monthlyPayment=document.getElementById("monthly-payment");
const totalPayment=document.getElementById("total-payment");
const totalInterest=document.getElementById("total-interest");
//得到float类型
const principal=parseFloat(amount.value);
const calculatedInterest = parseFloat(interest.value) /100 /12;
//计算interest
const calculatedPayments= parseFloat(years.value)*12;
//Compute monthly payment
const x = Math.pow(1+calculatedInterest,calculatedPayments);
const monthly = (principal*x*calculatedInterest)/(x-1);
if(isFinite(monthly)){ //检查是否为无穷大,是则说明输入错误
//给下面结果input赋值
monthlyPayment.value=monthly.toFixed(2);//设定小数
totalPayment.value=(monthly*calculatedPayments).toFixed(2);
totalInterest.value=((monthly*calculatedPayments)-principal).toFixed(2);
//设置显示
document.getElementById('results').style.display='block';
}else{
showError("Please check your numbers");
}
//隐藏gif
document.getElementById('loading').style.display='none';
//e.preventDefault();
}
(3)失败提示
//Show Error
function showError(error){
//Create a div
const errorDiv=document.createElement('div');
//Get elements
const card=document.querySelector(".card"); //得到card
const heading = document.querySelector(".heading"); //heding为title
//Add class
errorDiv.className="alert alert-danger"; //alert-danger设定样式,alert方便后续clear
//Create text node and append to div
errorDiv.appendChild(document.createTextNode(error));
//Insert error above heading
card.insertBefore(errorDiv,heading); //(要放入的parent,要放入的before)
//clear error after 3sec
setTimeout(clearError,3000); //3000ms后执行clearError
}
//Clearerror
function clearError(){
document.querySelector('.alert').remove();
}