css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              
<body>
<div class = "moveable-area"></div>
<div draggable="true" id="calc-frame" onclick="inp.focus();">
  <div id="calc-display">
    <input type = "text" id = "inp-display" onKeyPress="return takeInput(event);" onpaste="return false;" autofocus />
    <div id="tot-display"></div>
    <div id = "sum-display"></div>
  </div>
  <div class="calc-b-panel">
    <button onclick="bPress('/');">÷</button>
    <button onclick="bPress('*');">×</button>
    <button onclick="bPress('-');">-</button>
    <button onclick="bPress('+');">+</button>
    <button onclick="bPress('=');">=</button>
    <button class= "bt-clear" onclick="bPress('a');">AC</button>
    <button onclick="bPress('9');">9</button>
    <button onclick="bPress('6');">6</button>
    <button onclick="bPress('3');">3</button>
    <button onclick="bPress('#');">+/-</button> 
    <button class= "bt-clear" onclick="bPress('c');">C</button>
    <button onclick="bPress('8');">8</button>
    <button onclick="bPress('5');">5</button>
    <button onclick="bPress('2');">2</button>
    <button onclick="bPress('0');">0</button>
    <button onclick="bPress('7');">7</button>
    <button onclick="bPress('4');">4</button>
    <button onclick="bPress('1');">1</button>
    <button onclick="bPress('.');">.</button>
  </div>

</div>

<div class="info-tag">
  <p><b>Calculator</b><br>
  by Kevin Nolan</p>
  <p>Use buttons or keyboard:<br>
  a: all-clear<br>
  c: clear<br>
  #: +/-</p>
  <p>Drag the calculator<br> with your mouse</p>
  
</div>

            
          
!
            
              
* {
  font-family: 'Catamaran', sans-serif;
  box-sizing: border-box;
}
  
body {
  background: url('https://dl.dropbox.com/s/q3sq8g7mlifkg2z/seashells_CbKDpTB.jpg') fixed no-repeat;
  background-position: 0px 0px;
  background-size:1200px 500px;
}
  
#calc-frame {
  background: url('https://dl.dropbox.com/s/q3sq8g7mlifkg2z/seashells_CbKDpTB.jpg') fixed no-repeat;
  background-position: -20px -20px;
  background-size:1240px 540px;
  border: 1px solid rgba(0, 0, 0, .15);
  border-radius: 70px;
  box-shadow: 5px 5px 30px 5px rgba(0, 0, 0, .50);
  width: 300px;
  height: 400px;
  top: 50px;
  margin: auto;
  position:relative;
  cursor: -webkit-grab;
  cursor: -moz-grab;
}
  
#calc-display {
  border: 1px solid #999;
  color: white;
  border-radius: 20px;
  background: linear-gradient(
    rgba(0, 0, 0, .6), 
    rgba(0, 0, 0, .8));
  width: 270px;
  height: 80px;
  position: absolute;
  left:15px;
  top: 30px;

}

#tot-display {
  background-color: transparent;
  width: 250px;
  height: 20px;
  text-align:right;
  position: absolute;
  left:5px;
  top: 55px;
}

#inp-display {
  border:0px;
  background-color: transparent;
  color:white;
  width: 260px;
  height: 40px;
  text-align:right;
  font-size:35px;
  position: absolute;
  left:5px;
  top: 18px;
}
#inp-display:focus {
  outline:0;
}

#sum-display {
  background-color: transparent;
  width: 260px;
  height: 20px;
  position: absolute;
  left:15px;
  top: 0px;
}

.calc-b-panel {
  display: -webkit-flex;
  display: flex;
  -webkit-flex-flow: column wrap-reverse;
  flex-flow: column wrap-reverse;
  -webkit-justify-content: flex-end;
  justify-content: flex-end;
  
  width: 270px;
  height: 260px;
  position: absolute;
  left:10px;
  top: 110px;
}

.calc-b-panel button {
  border: 0px solid green;
  margin: 5px;
  border-radius: 10px;
  color: white;
  background-color:rgba(0, 0, 0, .70);
  font-size: 30px;
  width: 50px;
  height: 40px;
  line-height: 0px;
  cursor: pointer;
}
.calc-b-panel button:hover {
  background-color: rgba(50, 50, 255, .70);
}
.calc-b-panel button:active {
  background-color: rgba(0, 200, 200, .70);
}
  
.calc-b-panel .bt-clear {
  background-color:rgba(255, 50, 50, .70);
}
  
.moveable-area {
  z-index: -2;
  position: absolute;
  width: 1200px;
  height: 500px;
}
  
.info-tag {
  background-color: #fff5f0;
  padding: 5px;
  position: fixed;
  z-index: -1;
  bottom: 0; left: 0;
  font-size: 15px;
}
  .info-tag p {
    margin: 0px 3px 3px 0px;
  }
    
            
          
!
            
              //Calculator:
var sumArr = [];
var numRE = /\d/;
var inp = document.getElementById("inp-display");
var tallied = false; // True if sum is complete
//Movement:
var dm = document.getElementById('calc-frame');
dm.addEventListener('dragstart',drag_start,false);
document.body.addEventListener('dragover',drag_over,false);
document.body.addEventListener('drop',drop,false);  
  
// CALCULATOR FUNCTIONS ------------
// Related to working the calculator

function takeInput(oKeyEvent) {
  var tSF = document.getElementById("tot-display");
  var sSF = document.getElementById("sum-display");

  function soFar(op) {
    
    // Add number and operator to array
    sumArr.push(parseFloat(inp.value)); sumArr.push(op); 
    // Tally sumArr
    var total = sumArr.reduce(function (acc,item,ind) {
      if (ind == 0) {acc = item} // First item in array
      else if (numRE.test(item)) {  // Only act on numbers
        if (sumArr[ind-1] == '+') acc += item;
        else if (sumArr[ind-1] == '−') acc -= item;
        else if (sumArr[ind-1] == '×') acc *= item;
        else if (sumArr[ind-1] == '÷') acc /= item;
      }
      return acc;
    },0);
    sSF.innerHTML = sumArr.reduce(function(acc,item) {
      acc+=item + " ";
      return acc;
    },"");
    if (op == "=") {
      inp.value = total;
      tSF.innerHTML = "";
      // sumArr = []; // Redundant?
      tallied = true;
      inp.style.color = 'yellow';
    } else {
      inp.value="";
      tSF.innerHTML = total;
      //inp.style.color = 'white';
    }
  }
  
  function reset() {
    //if (!keep) inp.value=""; // If Keep then retain old value
    sSF.innerHTML = "";
    tSF.innerHTML = "";
    sumArr=[];
    tallied = false;
    inp.style.color='white';
  }
  
  var key = oKeyEvent.key;
  if (tallied) {
    if (numRE.test(key)) inp.value = ""; // Most calculators start afresh like this
    reset();
  }
  if (key == "c") {
    inp.value="";
  } else if (key == "a") {
    reset();
    inp.value="";
  } else if (key == "#" && inp.value != 0) {
    inp.value = -inp.value;
  } else if (numRE.test(key)) {
    inp.value += key;
  } else if (key == ".") {
    if (inp.value.indexOf('.') < 0) inp.value += key;
  } else if (numRE.test(inp.value)) { // If there's a number to operate on...  
    if (key == "+") soFar("+");
    else if (key == "-") soFar("−");
    else if (key == "*") soFar("×");
    else if (key == "/") soFar("÷");
    else if (key == "=" || key == "Enter") soFar("=");
  }
  // Always return false as user is not directly typing
  // into input box.
  return false;
}
  
function bPress(bttn) {
  takeInput({key: bttn});
  inp.focus();
}

// MOVEMENT FUNCTIONS--------------------
// Everything related to dragging the calculator
  
function drag_start(event) {
  var style = window.getComputedStyle(event.target, null);
  event.dataTransfer.setData("text/plain", (parseInt(style.getPropertyValue("left"),10) - event.clientX) + ',' + (parseInt(style.getPropertyValue("top"),10) - event.clientY));
}
function drag_over(event) {
  event.preventDefault();
  return false;
}
function drop(event) {
  var offset = event.dataTransfer.getData("text/plain").split(',');
  dm.style.left = (event.clientX + parseInt(offset[0],10)) + 'px';
  dm.style.top = (event.clientY + parseInt(offset[1], 10)) + 'px';
  event.preventDefault();
  inp.focus();
  return false;

}
  

            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console