Calculator

In this example below you will see how to do a Calculator with some HTML / CSS and Javascript

Thumbnail
This awesome code was written by vanderdrilu, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright vanderdrilu ©

Technologies

  • HTML
  • CSS
  • JavaScript
<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  <title>Calculator</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  
  <link rel='stylesheet prefetch' href='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css'>
<link rel='stylesheet prefetch' href='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0/css/bootstrap.css'>

      <link rel="stylesheet" href="css/style.css">

  
</head>

<body>

  <html>

<body>
  <div class="main shadowbox">
    <H6 class="marcfont">calculator </H6>

    <div class="disp">
      <H2 id="dispUP" class="pismo">0.</H2>
      <H6 id="dispBo" class="pismo">00.</H6>

    </div>
    <hr>
    <div class="container lato">

      <button id=AC class="btn btn-danger shadowbox  " onclick="this.blur()">AC</button>
      <button id=CE class="btn btn-danger shadowbox " onclick="this.blur()">CE</button>
      <button id=Bdeleno class="btn  shadowbox " onclick="this.blur()">÷</button>
      <button id=BX class="btn   shadowbox" onclick="this.blur()">x</button>
      <button id=B7 class="btn  shadowbox" onclick="this.blur()">7</button>
      <button id=B8 class="btn  shadowbox" onclick="this.blur()">8</button>
      <button id=B9 class="btn shadowbox " onclick="this.blur()">9</button>
      <button id=Bminus class="btn   shadowbox onclick=" this.blur() "">-</button>
      <button id=B4 class="btn   shadowbox" onclick="this.blur()">4</button>
      <button id=B5 class="btn  shadowbox" onclick="this.blur()">5</button>
      <button id=B6 class="btn  shadowbox" onclick="this.blur()">6</button>
      <button id=Bplus class="btn   shadowbox" onclick="this.blur()">+</button>
      <button id=B1 class="btn   shadowbox " onclick="this.blur()">1</button>
      <button id=B2 class="btn  shadowbox" onclick="this.blur()">2</button>
      <button id=B3 class="btn  shadowbox" onclick="this.blur()">3</button>
      <button id=B0 class="btn  shadowbox " style="width: 92px;" onclick="this.blur()">0</button>
      <button id=Bdot class="btn  shadowbox" onclick="this.blur()">.</button>
      <button id=Brovnasa class="btn   shadowbox " onclick="this.blur()">=</button>
    </div>

  </div>

</body>

</html>
  <script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js'></script>

  

    <script  src="js/index.js"></script>




</body>

</html>

/*Downloaded from https://www.codeseek.co/vanderdrilu/calculator-YLJZyM */
.main {
  width: 260px;
  height: 380px;
  background-color: lavender;
  border: 1px solid;
  padding: 5px;
  margin: 10% 100px 100px 50%;
  border-radius: 5px;
}

.pismo {
  color: ghostwhite;
  text-align: right;
  font-family: "Trebuchet MS", "Lucida Sans Unicode";
}

.btnred {
  background-color: brown;
}

body {
  background-image: url(https://wallportal.com/uploads/posts/office-desk-wallpaper/office_desk_wallpaper_001.jpg);
  background-repeat: no-repeat;
  background-position: 0px 40%;
}

.btn:focus,
.btn:hover,
.btn:visited {
  color: aqua;
  text-decoration: none !important;
  outline: none !important;
  -webkit-transform: translate(0px, 3px);
          transform: translate(0px, 3px);
}

button {
  width: 40px;
  height: 42px;
  text-align: left;
  padding: 1px;
  margin: 5px;
  background-color: black;
  font-size: 100%;
  color: ghostwhite;
}

#Brovnasa {
  position: absolute;
  margin-left: 9px;
  margin-top: -42px;
  height: 92px;
}

.disp {
  width: 200px;
  height: 50px;
  background-color: #0e0905;
  border: 3px solid darkgray;
  box-shadow: -2px 2px 8px whitesmoke;
  margin: 20px 20px;
  padding: 1px 30px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  box-shadow: inset -2px 1px 5px 0px rgba(242, 233, 233, 0.75);
}

.shadowbox {
  box-shadow: -2px 2px 8px rgba(0, 0, 0, 0.6), -3px 4px 12px rgba(0, 0, 0, 0.5), -4px 6px 15px rgba(0, 0, 0, 0.4), -5px 8px 18px rgba(0, 0, 0, 0.3);
}

.latofont {
  font-family: 'Lato', sans-serif;
}

.fixfont {
  font-family: 'Permanent Marker', cursive;
}

.marcfont {
  font-family: 'Marck Script', cursive;
}


/*Downloaded from https://www.codeseek.co/vanderdrilu/calculator-YLJZyM */
$(document).ready(function () {
    var valueString = " ";
    var historyclicked = "";
    var historyclickedUP = "";
    var dotBoo = false;
    var Maxdecimalplace = 0;
    $('button').click(function () {
        valueString = $(this).text();
        disp(valueString);
    });
    function disp(val) {
        var result = 0;
        var ACBoo = false;
        var operators = false;
        var f = 2.2 - 1;
        if (val === "AC") {
            historyclicked = "";
            historyclickedUP = "";
            val = "0";
            ACBoo = true;
        }
        else if (val === "CE") {
            historyclicked = historyclicked.substr(0, historyclicked.length - 1);
            historyclickedUP = historyclickedUP.substr(0, historyclickedUP.length - 1);
            val = "0";
        }
        else if (val === "x" || val === "÷" || val === "+" || val === "-") {
            if (historyclicked.includes("=")) {
                historyclicked = historyclicked.substring(historyclicked.indexOf("=") + 1);
            }
            historyclickedUP = "";
            historyclicked += val;
            operators = true;
        }
        else if (val === "=") {
            result = eval(historyclicked.replace("÷", "/").replace("x", "*")).toFixed(Maxdecimalplace);
            historyclicked += val + result;
            val = result;
            historyclickedUP = "";
            Maxdecimalplace = 0;
        }
        else if (val === ".") {
            if (historyclickedUP.includes(".")) {
                val = "";
            }
            else {
                historyclicked += val;
            }
        }
        else {
            continueaftereqaul();
            historyclicked += val;
        }
        var todisp = historyclickedUP + val;
        if ((todisp).length > 14 && !isNaN(todisp)) {
            todisp = todisp.toPrecision(11);
        }
        $('#dispUP').text(todisp);
        $('#dispBo').text(historyclicked);
        if (ACBoo === false && operators === false) {
            historyclickedUP += val;
        }
        else {
            ACBoo = false;
            operators = false;
        }
        if (historyclickedUP.indexOf(".") > -1) {
            var point = historyclickedUP.substring(historyclickedUP.indexOf(".") + 1).length;
            if (point > Maxdecimalplace) {
                Maxdecimalplace = point;
            }
        }
        function continueaftereqaul() {
            if (historyclicked.includes("=")) {
                historyclicked = "";
                historyclickedUP = "";
                Maxdecimalplace = 0;
            }
        }
    }
});

Comments