cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmlicon-personicon-teamoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

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.

            
              <section class="calculator-wrap">
  <form method="GET" name="form" action="#" id="calculator" data-calc-mode="initial" autocomplete="off">
    <h1 class="calc-title">Casio 9000</h1>
      <input type="text" id="calc-display" value="0">
      <div class="buttons-wrap">
        <div class="button-numbers">  
          <button id="btn-1" value="1" data-btn-val="1">1</button>
          <button id="btn-2" value="2" data-btn-val="2">2</button>
          <button id="btn-3" value="3" data-btn-val="3">3</button>
          <button id="btn-4" value="4" data-btn-val="4">4</button>
          <button id="btn-5" value="5" data-btn-val="5">5</button>
          <button id="btn-6" value="6" data-btn-val="6">6</button>
          <button id="btn-7" value="7" data-btn-val="7">7</button>
          <button id="btn-8" value="8" data-btn-val="8">8</button>
          <button id="btn-9" value="9" data-btn-val="9">9</button>
          <button id="btn-0" value="0" data-btn-val="0">0</button>
          <button id="percentage-btn" data-operator="percentage">%</button>
          <button id="decimal-btn" data-operator="decimal">.</button>
        </div>
        <div class="buttons-operators"> 
          <button id="add-btn" data-operator="add">+</button>
          <button id="subtract-btn" data-operator="subtract">-</button>
          <button id="divide-btn" data-operator="divide">/</button>
          <button id="multiply-btn" data-operator="multiply">*</button>
        </div>
      </div>
      <div class="buttons-methods">
        <button id="clear-btn" data-operator="clear">C</button>
        <button id="calc-btn" data-operator="equals">=</button>
      </div>
    </form>
</section>

<p class="colophon">
  Another CodePen by <a href="https://richardhuf.com.au">Richard Huf</a>
</p>
            
          
!
            
              // @import url('https://fonts.googleapis.com/css?family=Source+Code+Pro');

$font-google: 'Source Code Pro', monospace;
$font-google-2: 'Orbitron', monospace;

// VARS

// Color vars
// $color1: #F4DF42;
$color1: #42F4D1;
$color2: #000;

// MIXINS

// Global box shadow mixin
// Set some default values that can be altered
@mixin global-box-shadow($x: 3px, $y: 3px) {
  box-shadow: #{$x} #{$y} 0 lighten($color2, 20%);
}

html {
  height: 100%;
}

* {
  box-sizing: border-box;
}

body {
  // background-color: $color1;
  background: linear-gradient(to bottom, #42f4d1 0%,#42f4d1 51%,#000000 51%,#42f4d1 51%,#000000 51%,#000000 51%,#000000 100%);
  background-size: cover;
  font-family: $font-google;
  height: 100%;
  padding: 16px;
}

a:link,
a:visited {
  color: $color2;
}

a:hover,
a:active {
   text-decoration: none; 
}

.calculator-wrap {
  // @include global-box-shadow(6px, 6px);
  background-color: $color1;
  border: 3px solid $color2;
  cursor: grab;
  display: block;
  margin: 0 auto;
  min-width: 300px;
  padding: 20px;
  position: relative;
  width: 40vmax;
  z-index: 2;
  
  &.ui-draggable-dragging,
  &:active {
    cursor: grabbing;
  }
}

.calc-title {
  // background: mix($color1, $color2, 95%);
  color: $color2;
  margin: -20px -20px 0;
  padding: 20px;
  text-transform: uppercase;
  text-align: center;
  transform: translateZ(30deg);
  // transform: perspective(17px);
}

#calc-display {
  @include global-box-shadow;
  border: 2px solid $color2;
  background-color: transparent;
  font-family: $font-google-2;
  border-radius: 2px;
  display: block;
  margin-bottom: 10px;
  padding: 12px 12px;
  width: 100%;
}

.buttons-wrap {
  display: flex;  
}

.buttons-operators {
  align-items: flex-start;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-around;
}

.button-numbers {
  width: 72%;
  margin-right: 3%;
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  justify-content: space-between;
  
  button {
    width: 30%;
    
    &:nth-child(3n) {
      margin-right: 0;
    }
  }
}

.buttons-operators {
  display: flex;
  width: 25%;
  
  button {
    background: mix($color1, $color2, 80%);
    font-size: 1.25em;
    flex-basis: 100%;
  }
}

.buttons-methods {
  display: flex;
  flex-direction: row;
  align-items: stretch;
  flex-wrap: nowrap;
  justify-content: space-between;
  
  button {
    background: mix($color1, $color2, 80%);
    font-size: 1.25em;
    width: 48.5%;
  }
}

button {
  @include global-box-shadow;
  appearance: none;
  background-color: transparent;
  border: 2px solid $color2;
  font-weight: 900;
  font-family: $font-google-2;
  font-size: 16px;
  margin-bottom: 8px;
  outline: none;
  padding: 8px;

  &:active {
    box-shadow: none;
    transform: translateX(3px) translateY(3px);
    outline: none;
  }
}


[id="calculator"]:before {
  color: $color2;
  content: 'DEBUG: Mode = ' attr(data-calc-mode);
  font-family: $font-google;
  font-size: 12px;
  text-transform: uppercase;
  position: absolute;
  bottom: 7px;
  left: 25px;
  display: block;
}

// Turn off debug msg via js 
.is-debug-mode:before {
  content: '';
}


.colophon {
  color: $color1;
  font-size: 13px;
  text-align: center;
  margin: 40px auto;
  position: relative;
  z-index: 1;
  
  a {
    color: $color1;
  }
}
            
          
!
            
              'use strict';

let calculator            = document.getElementById('calculator');
let calcDisplayEl         = document.getElementById('calc-display');
let calcDisplayValue      = calcDisplayEl.value;
let currentCalcMode       = calculator.dataset.calcMode;
let calcBtn               = document.getElementById('calc-btn');  
let clearBtn              = document.getElementById('clear-btn');
let decimalBtn            = document.getElementById('decimal-btn');  
let percentageBtn         = document.getElementById('percentage-btn');
let memory                = [];
let debug                 = false;

// If in debug mode, we output console logs and show debug info 
if(!debug) {
  // Turn off all console logs conveniently
  console.log = function() {}
  
  // Turn off debug data displaying calc mode 
  calculator.classList.add('is-debug-mode');
}

// Make calculator draggable 
$('.calculator-wrap').draggable({
  addClasses: true,
});

// Array of number button elements
let numbers = [
  document.getElementById('btn-1'),
  document.getElementById('btn-2'),
  document.getElementById('btn-3'),
  document.getElementById('btn-4'),
  document.getElementById('btn-5'),
  document.getElementById('btn-6'),
  document.getElementById('btn-7'),
  document.getElementById('btn-8'),
  document.getElementById('btn-9'),
  document.getElementById('btn-0')
];

// Array of operator button elements
let operators = [
  document.getElementById('add-btn'),
  document.getElementById('subtract-btn'),
  document.getElementById('divide-btn'),
  document.getElementById('multiply-btn'),
];

numbers.forEach(function(element) {
  // console.log(element);
  element.addEventListener('click', function(event) {
    event.preventDefault();
    
    // Use 'data-cal-mode' attr on form to set mode
    // Mode can be 'number' if number last pressed,
    // or 'operator' if operator last pressed'.
    currentCalcMode =  calculator.dataset.calcMode;
    
    //////////////////////////////////////////////////////// 
    // CALC MODE: INITIAL
    ////////////////////////////////////////////////////////
    if(currentCalcMode === 'initial') {

      // Calc is reset to initial. Update display with button value pressed
      calcDisplayEl.value = this.value;
      
      // Place this value into memory
      memory.push(calcDisplayEl.value);
      
    } 
    
    //////////////////////////////////////////////////////// 
    // CALC MODE: NUMBER
    ////////////////////////////////////////////////////////
    if(currentCalcMode === 'number') {
    
      // append the last number pressed to the value
      calcDisplayEl.value = calcDisplayEl.value + this.value;
      
       // We remove the last item (a number) from the array, and then update the array with the currect value
      memory.pop();
      
      // Update memory
      memory.push(calcDisplayEl.value);
    }
    
    //////////////////////////////////////////////////////// 
    // CALC MODE: OPERATOR
    ////////////////////////////////////////////////////////
    if(currentCalcMode === 'operator') {

      calcDisplayEl.value = this.value;
      
      memory.push(calcDisplayEl.value);
      
      console.log(memory);

    }
    
    // Since number has been presed, set data-cal-mode attr to 'number'
    // Get updated value of calc-display
    let currentDisplayEl  = document.getElementById('calc-display');
    
    // Set calc mode to number
    calculator.dataset.calcMode = 'number';

    // Update calc mode var
    currentCalcMode =  calculator.dataset.calcMode;
    
    let currentDisplayVal = parseInt(currentDisplayEl.value);
    
    // Place current display value into memory
    // memory.push(currentDisplayVal);
    console.log(memory);    

  }, false);  
});

// Bind click event to each operator button
operators.forEach(function(element) {
  element.addEventListener('click', function(event) {
    event.preventDefault();
    
    // Gets value from buttons data-operator attr.
    let buttonOperator = this.getAttribute('data-operator');
    
    memory.push({
      'operator': buttonOperator
    });
    
    // Set calc mode to operator
    calculator.dataset.calcMode = 'operator';
    
    console.log(memory);
    
  })
});

////////////////////////////////////////////////////////
// CLEAR BUTTON CLICK
////////////////////////////////////////////////////////
clearBtn.addEventListener('click', (event) => {
  event.preventDefault();
  
  // Reset some stuff
  calcDisplayEl.value = "0";
  memory = [];
  
  // Set calc mode to initial
  calculator.dataset.calcMode = 'initial';
  
  // console.log(memory);
  console.clear();
  
}, false);

////////////////////////////////////////////////////////
// DECIMAL BUTTON CLICK
////////////////////////////////////////////////////////
decimalBtn.addEventListener('click', (event) => {
  event.preventDefault();
  
  // If displayValue !== 0, append decimal point
  if(calcDisplayEl.value !== '0') {
    // append the last number pressed to the value
    calcDisplayEl.value = calcDisplayEl.value + '.';       
  }
    
}, false);

////////////////////////////////////////////////////////
// PERCENTAGE BUTTON CLICK
////////////////////////////////////////////////////////
percentageBtn.addEventListener('click', (event) => {
  event.preventDefault();
  
  // If displayValue !== 0, append decimal point
  if(calcDisplayEl.value !== '0') {
    // append the last number pressed to the value
    calcDisplayEl.value = parseInt(calcDisplayEl.value) / 100;       
  }
  
  memory = [calcDisplayEl.value];
  console.log(memory);
  
}, false);

////////////////////////////////////////////////////////
// CALC BUTTON CLICK
////////////////////////////////////////////////////////
calcBtn.addEventListener('click', (event) => {
  event.preventDefault();
  
  console.log(memory);
  
  // Lets calculate a result.
  let previousVal  = memory[0];
  let operator     = memory[1];
  let currentVal   = memory[2];
            
  // Operator returns a function name,
  // either 'add', 'subtract', 'multiple' or 'divide'
  // We us this funtion to calculate a result

  let calcMethodToUse = operator.operator;
  let result = '';            

  switch(calcMethodToUse) {
    case 'add':
      result = add(previousVal, currentVal);
      break;
    case 'subtract':
      result = subtract(previousVal, currentVal);
      break;
    case 'multiply':
      result = multiply(previousVal, currentVal);
      break;
    case 'divide':
      result = divide(previousVal, currentVal);
      break;
  }
    
  calcDisplayEl.value = result;
      
  memory = [result];

  console.log(memory);
    
}, false);


/////////////////////////////////////////////////////////
// OPERATOR FUNCTIONS
/////////////////////////////////////////////////////////

// Addition
const add = (a,b) => {
  return(+a + +b)
}

// Subtraction
const subtract = (a,b) => {
  return(a - b)
}

// Multiply
const multiply = (a,b) => {
  return(a * b)
}

// Divide
const divide = (a,b) => {
  return(a / b)
}

/////////////////////////////////////////////////////////
// KEY BINDINGS
/////////////////////////////////////////////////////////

window.addEventListener("keyup", checkKeyPressed, false);

function checkKeyPressed(e) {
  console.log('click');
  // If displayfield is in focus, we can type numbers in directly instead of triggering a click
  if(calcDisplayEl.is(':focus')) {
    return;
  }

  // Numbers 0-9
  for(let i = 48; i < 58; i++) {
    console.log(i);
    
    if (e.keyCode == [i]) {
      $('[data-btn-val="' + ([i] - 48) + '"]').trigger('click');
    }
  }

  // Plus key
  if (e.which == 107) {
    $('[data-operator="add"]').trigger('click');
  }

  // Minus key
  if (e.which == 109) {
    $('[data-operator="subtract"]').trigger('click');
  }

  // Enter (or equals) key
  if (e.which == 13 || e.keyCode == 187) {
    $('[data-operator="equals"]').trigger('click');
  }

  // Clear key (cmd + c)
  if (e.keyCode == 67) {
    console.log('cmd + c');
    $('[data-operator="clear"]').trigger('click');
  }
}

            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console