HTML,CSS,JavaScript实现简易计算器

写在前面
用一定的的js代码,实现一个简单的四则运算的计算器,样式大概就是下面这个样子滴, 然后是可以实现基本运算的哈!
在这里插入图片描述

进入代码

  1. html代码
  <div class="container">
    <h1>HTML,CSS,JavaScript实现简易计算器</h1>
    <form name="calculator">
      <input type="text" id="display">
      <input type="button" id="clear" class="btn other" value="C">
      <br>
      <input type="button" onclick="get(this.value)" class="btn number" value="7">
      <input type="button" onclick="get(this.value)" class="btn number" value="8">
      <input type="button" onclick="get(this.value)" class="btn number" value="9">
      <input type="button" onclick="get(this.value)" class="btn operator" value="/">
      <br>
      <input type="button" onclick="get(this.value)" class="btn number" value="4">
      <input type="button" onclick="get(this.value)" class="btn number" value="5">
      <input type="button" onclick="get(this.value)" class="btn number" value="6">
      <input type="button" onclick="get(this.value)" class="btn operator" value="*">
      <br>
      <input type="button" onclick="get(this.value)" class="btn number" value="1">
      <input type="button" onclick="get(this.value)" class="btn number" value="2">
      <input type="button" onclick="get(this.value)" class="btn number" value="3">
      <input type="button" onclick="get(this.value)" class="btn operator" value="+">
      <br>
      <input type="button" onclick="get(this.value)" class="btn number" value="0">
      <input type="button" onclick="get(this.value)" class="btn operator" value=".">
      <input type="button" onclick="calculate();" class="btn other" value="=">
      <input type="button" onclick="get(this.value)" class="btn operator" value="-">
    </form>
  </div>
  1. css代码
* {
  margin: 0;
  padding: 0;
  border: none; /* input标签默认有border要去掉*/
}
.container {
  width: 600px;
  height: 600px;
  margin: auto;
}
h1 {
  text-align: center;
  margin-top: 20px;
  padding: 40px 0;
  color: #e66767;
}

form {
  width: 280px;
  margin: 40px auto;
  padding: 40px 0 30px 30px;
  background-color: #495678;
  box-shadow: 4px 4px #2c3e50;
  border-radius: 25px;
}

.btn {
  cursor: pointer;
  outline: none;
  width: 45px;
  height: 45px;
  margin:10px 0 5px 10px;
  border-radius: 50%;
  font-size: 20px;
  background-color: #57606f;
}

#display {
  outline: none;
  width: 165px;
  height: 40px;
  background-color: #98d1dc;
  box-shadow: inset 4px 4px 0px #3facc0;
  border-radius: 20px;
  font-size: 20px;
  color: aliceblue;
  padding-left: 10px;
  margin-left: 10px;
}

#clear {
  margin-left: 0;
}

.number {
  color: azure;
  background-color: #72778b;
  box-shadow: 0px 4px #5f6680;
}

.operator {
  color: #57606f;
  background-color: #dce1f5;
  box-shadow: 0 4px #c0c1c7;
}

.other {
  color: azure;
  background-color: #e67e22;
  box-shadow: 0 4px #e76a3d;
}

.number:active,
.operator:active,
.other:active {
  box-shadow: 0 2px #5f6680;
  transform: translateY(2px);
  -webkit-transform: translateY(2px);
  -moz-transform: translateY(2px);
  -ms-transform: translateY(2px);
  -o-transform: translateY(2px);
}
  1. js代码
function get(value) {
  document.getElementById('display').value += value;
}

function calculate() {
  var result = 0;
  result = document.getElementById('display').value;
  document.getElementById('display').value = eval(result);  //计算字符串的值
};

document.getElementById('clear').addEventListener('click',function() {
  document.getElementById('display').value = '';
});
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值