Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                
<div id="app" class="calculator">
  <div class="calc-body">
    <div class="calc-body-screen">
      <div class="q"><span id="inputtile">{{caltext}}</span></div>
      <div class="a"><span id="answertile">{{ans}}</span></div>
    </div>
    <div class="calc-body-btn"><span>MC</span></div>
    <div class="calc-body-btn"><span>MR</span></div>
    <div class="calc-body-btn"><span>MS</span></div>
    <div class="calc-body-btn"><span>M+</span></div>
    <div class="calc-body-btn"><span>M-</span></div>
    <div id="btn_back" class="calc-body-btn"><span>←</span></div>
    <div id="btn_clear_all" class="calc-body-btn"><span>CE</span></div>
    <div id="btn_clear" class="calc-body-btn"><span>C</span></div>
    <div id="btn_rvnag" class="calc-body-btn"><span>±</span></div>
    <div id="btn_sqrt" class="calc-body-btn"><span>√</span></div>
    <div class="calc-body-btn btnnum"><span>7</span></div>
    <div class="calc-body-btn btnnum"><span>8</span></div>
    <div class="calc-body-btn btnnum"><span>9</span></div>
    <div class="calc-body-btn btnnum"><span>/</span></div>
    <div class="calc-body-btn btnnum"><span>%</span></div>
    <div class="calc-body-btn btnnum"><span>4</span></div>
    <div class="calc-body-btn btnnum"><span>5</span></div>
    <div class="calc-body-btn btnnum"><span>6</span></div>
    <div class="calc-body-btn btnnum"><span>*</span></div>
    <div id="btn_reverse" class="calc-body-btn"><span>1/x</span></div>
    <div class="left">
      <div class="calc-body-btn btnnum"><span>1</span></div>
      <div class="calc-body-btn btnnum"><span>2</span></div>
      <div class="calc-body-btn btnnum"><span>3</span></div>
      <div class="calc-body-btn btnnum"><span>-</span></div>
      <div class="calc-body-btn zero btnnum"><span>0</span></div>
      <div class="calc-body-btn btnnum"><span>.</span></div>
      <div class="calc-body-btn btnnum"><span>+</span></div>
    </div>
    <div id="btn_cal_all" class="calc-body-btn qu"><span>=</span></div>
  </div>
</div>
              
            
!

CSS

              
                	html,body {
		width: 100%;
		height: 100%;
		padding: 0;
		margin: 0;
	}
	.calculator {
		width: 100%;
		height: 100%;
		min-height: 600px;
		background-image: linear-gradient(to bottom, #038FA1, #023E6E);
		display: flex;
		justify-content: center;
		align-items: center;
	}
	.calc-body {
	  width: 85%;
	  max-width: 250px;
	  height: 375px;
	  background-image: linear-gradient(to bottom, #004D59, #012024);
	  padding: 10px;
	  border-radius: 10px;
	  box-shadow: 0 25px 35px -15px rgba(3, 28, 31, .85);
	  /*flex*/
	  display: flex;
	  flex-wrap: wrap;
	  justify-content: space-between;
	  align-items: stretch;
	  align-content: space-between;
	}
	.calc-body-screen {
		flex: 1 0 100%;
		height: 24%;
		color: #A8EEFF;
		box-shadow: inset 0 0 0 1px #225861;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		text-align: right;
	}
	.calc-body-screen .q {
		width: 100%;
		line-height: 40px;
		padding: 0 5%;
		box-sizing: border-box;
		font-size: 15px;
	}
	.calc-body-screen .a {
		line-height: 50px;
		width: 100%;
		padding: 0 5%;
		box-sizing: border-box;
		font-size: 38px;
	}
	.calc-body-btn {
		cursor: pointer;
		opacity: .6;
		font-size: 12px;
		flex: 0 1 18%;
		height: 11%;
		color: #A8EEFF;
		box-shadow: inset 0 0 0 1px #225861;
		text-align: center;
		border-radius: 5px;
		display: flex;
		justify-content: center;
		align-items: center;
	}
	.calc-body-btn:hover {
		background-color: #225861;
	}
	.calc-body-btn.qu {
		height: 23%;
	}
	.left {
		display: flex;
		flex: 0 0 79.5%;
		height: 23%;
		align-content: space-between;
		justify-content: space-between;
		flex-wrap: wrap;
	}
	.left .calc-body-btn {
		height: 46%;
		flex: 0 0 23%;
	}
	.left .calc-body-btn.zero {
		flex: 0 1 48.5%;
	}

              
            
!

JS

              
                var global_cal={};
//計算式
global_cal.caltext="";
//答案
global_cal.ans=0;
var lastcal=false;


//將計算式 答案資料與介面同步
var vm = new Vue({
  el: "#app",
  data: global_cal
});

$(".btnnum").click(function(){
  //當有這個class的時候,計算式直接加上span裡面的文字
  global_cal.caltext+=$(this).children("span").text();
  lastcal=false;
});

//取倒數
$("#btn_reverse").click(function(){
  //計算 並把計算式設為空字串
  global_cal.ans=(""+(1/parseInt(global_cal.caltext))).substr(0,9);
  global_cal.caltext="";
});

//開根號
$("#btn_sqrt").click(function(){
  //如果剛剛做完運算(lastcal=true)就再做一次
  if (lastcal)
    global_cal.ans=(""+(Math.sqrt(parseInt(global_cal.ans)))).substr(0,9);   
  else
    global_cal.ans=(""+(Math.sqrt(parseInt(global_cal.caltext)))).substr(0,9);
  
  global_cal.caltext="";
  lastcal=true;
 
});

$("#btn_rvnag").click(function(){
  //取前九個
  global_cal.ans=(""+(-(parseInt(global_cal.caltext)))).substr(0,9);
  global_cal.caltext="";
});

$("#btn_clear_all").click(function(){
  global_cal.ans=0;
  global_cal.caltext="";
});
$("#btn_clear").click(function(){
  global_cal.caltext="";
});
$("#btn_cal_all").click(function(){
  //丟eval計算取前九個,加""之後資料會變文字,取前九個字
  global_cal.ans=(""+eval(global_cal.caltext.split("*").join("*"))).substr(0,9);
  global_cal.caltext="";
  lastcal=true;
});

$("#btn_back").click(function(){
  //從0開始取(長度-1)個字,所以看起來像退格
  global_cal.caltext=global_cal.caltext.substr(0,global_cal.caltext.length-1);
});

              
            
!
999px

Console