LOADING...

Preview

Pen ID
Unlock Campus Themeforest adv

 

Code
C
7 8 9 + 4 5 6 - 1 2 3 ÷ 0 . = x
CSS
* {
	margin: 0;
	padding: 0;
	box-sizing: border-box;
	font: normal 25px Arial, sans-serif;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
}

html {
	height: 100%;
	background: url(https://33.media.tumblr.com/d4a884274dff05760e01bd3bdcc6defb/tumblr_nb1ulnMaP91st5lhmo1_1280.jpg);
	background-size: cover;
}


#calculator {
	width: 325px;
	height: auto;
	overflow:hidden;
	margin: 100px auto;
	padding: 20px 20px 9px;
	position:relative;
	background: rgba(0,0,0,0.3);
	border-radius: 3px;
	box-shadow:  0px 10px 15px rgba(0, 0, 0, 0.2);
}


.top span.clear {
	position:absolute;
  z-index:6;
 
}


.top .screen {
	height: 80px;
	width: 100%;
	position:absolute;
  top:0; left:0;
	padding: 20px 10px;
	background: rgba(0, 0, 0, 0.2);
	font-size: 27px;
  font-weight:normal;
	line-height: 40px;
	color: white;
	text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.2);
	text-align: right;
	letter-spacing: 1px;
}

.keys, .top {overflow: hidden;}


.keys span, .top span.clear {
	float: left;
	position: relative;
	top: 0;
	
	cursor: pointer;
	
	width: 66px;
	height: 56px;
	
	background: rgba(0,0,0,0.5);
	border-radius: 3px;
	box-shadow: 0px 4px rgba(0, 0, 0, 0.2);
	
	margin: 0 7px 11px 0;
	
	color: #ccc;
	line-height: 56px;
	text-align: center;
	
	user-select: none;
	

	transition: all 0.2s ease;
}


.keys span.operator {
	background: rgba(250,100,0,0.3);
	margin-right: 0;
}

.keys span.eval {

	color: #888e5f;
}

.top span.clear {
	background: transparent;
	box-shadow: none;
	color: #aaa;
  width:56px;
  left:5px
}


.keys span:hover {
	background: rgba(255,255,255,0.8);
	color: #333;
}

.keys span.eval:hover {
	background: #abb850;
	color: #ffffff;
}

.top span.clear:hover {
  border-radius:190px;
	background: rgba(0,0,0,0.1);
	color: white;
}

.keys span:active {
	box-shadow: 0px 0px rgba(0, 0, 0, 0.2);
	top: 4px;
}

.keys span.eval:active {
	box-shadow: 0px 0px #717a33;
	top: 4px;
}

.top span.clear:active {
	top: 4px;
	box-shadow: 0px 0px #d3545d;
}
JS
var keys = document.querySelectorAll('#calculator span');
var operators = ['+', '-', 'x', '÷'];
var decimalAdded = false;


for(var i = 0; i < keys.length; i++) {
	keys[i].onclick = function(e) {
		
		var input = document.querySelector('.screen');
		var inputVal = input.innerHTML;
		var btnVal = this.innerHTML;
		var total;
		if(btnVal == 'C') {
			input.innerHTML = '';
			decimalAdded = false;
		}
		
		else if(btnVal == '=') {
			var equation = inputVal;
			var lastChar = equation[equation.length - 1];
			
			equation = equation.replace(/x/g, '*').replace(/÷/g, '/');
			
			if(operators.indexOf(lastChar) > -1 || lastChar == '.')
				equation = equation.replace(/.$/, '');
			
			if(equation)
				{
             total = eval(equation);
              if(total.toString().indexOf('.') != -1)
                total= total.toFixed(2);
          
          input.innerHTML = total;
        }
				
			decimalAdded = false;
		}
		
		
		else if(operators.indexOf(btnVal) > -1) {
			
			var lastChar = inputVal[inputVal.length - 1];
			
			if(inputVal != '' && operators.indexOf(lastChar) == -1) 
				input.innerHTML += btnVal;
			
			else if(inputVal == '' && btnVal == '-') 
				input.innerHTML += btnVal;
			
			if(operators.indexOf(lastChar) > -1 && inputVal.length > 1) {
				input.innerHTML = inputVal.replace(/.$/, btnVal);
			}
			
			decimalAdded =false;
		}
		
		else if(btnVal == '.') {
			if(!decimalAdded) {
				input.innerHTML += btnVal;
				decimalAdded = true;
			}
		}
		
		else {
			input.innerHTML += btnVal;
		}
		
		e.preventDefault();
	} 
}
Host Instantly Drag and Drop Website Builder

 

Description

iOS8 inspired
Term
Mon, 11/27/2017 - 21:50

Related Codes

Pen ID
Pen ID
Pen ID