JS實(shí)現(xiàn)蘋果計(jì)算器
本文實(shí)例為大家分享了JS實(shí)現(xiàn)蘋果計(jì)算器的具體代碼,供大家參考,具體內(nèi)容如下
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>手機(jī)</title> <style type="text/css"> #phone{ position: relative; width: 380px; height: 700px; box-shadow: 1px 1px 10px #808080; margin: auto; border-radius: 30px; } .kj{ position: absolute; width: 8px; height: 60px; background-color: black; right: -8px ; top:100px; } .yl{ position: absolute; width: 8px; height: 80px; background-color: black; left: -8px; top: 85px; } .top{ width: 120px; height: 50px; /* box-shadow: 1px 1px 10px #808080; */ margin: auto; } .mkf{ float: left; width: 100px; height: 10px; border-radius: 5px; background-color: black; margin-right: 10px; margin-top: 20px; } .sxj{ float: left; width: 10px; height: 10px; border-radius: 5px; background-color: black; margin-top: 20px; } .screen{ width: 370px ; height: 600px; background-color: black; margin: auto; } .result-num{ height: 120px; /* padding-top: 30px; */ } .sp{ float: right; color: white; font-size: 50px; margin-top: 50px; } .num{ height: 480px; } .key{ width: 82.5px; height: 82.5px; border-radius: 50px; background-color: #808080; float: left; margin: 5px; text-align: center; line-height: 80px; font-size: 20px; color: white; } .first{ background-color: #B0B0B0; color: black; } .second{ background-color: orange; } .third{ width: 175px; } .home{ width: 45px; height: 45px; border-radius: 25px; background-color: #B0B0B0; margin: 3px auto 0px auto; } </style> </head> <body> <div id="phone"> <!--開機(jī)鍵--> <div class="kj"></div> <!--音量--> <div class="yl"></div> <!-- 手機(jī)頂部 --> <div class="top"> <!-- 麥克風(fēng) --> <div class="mkf"></div> <!-- 攝像頭 --> <div class="sxj"></div> </div> <!-- 屏幕 --> <div class="screen"> <div class="result-num"> <span class="sp">0</span> </div> <div class="num"> <div class="key first" onclick="clearCompute()">AC</div> <div class="key first" onclick="deleteLastNum()">←</div> <div class="key first">%</div> <div class="key second" onclick="fun('/')">/</div> <div class="key keynum" onclick="fun(7)">7</div> <div class="key keynum" onclick="fun(8)">8</div> <div class="key keynum" onclick="fun(9)">9</div> <div class="key second" onclick="fun('*')">*</div> <div class="key keynum" onclick="fun(4)">4</div> <div class="key keynum" onclick="fun(5)">5</div> <div class="key keynum" onclick="fun(6)">6</div> <div class="key second" onclick="fun('-')">-</div> <div class="key keynum" onclick="fun(1)">1</div> <div class="key keynum" onclick="fun(2)">2</div> <div class="key keynum" onclick="fun(3)">3</div> <div class="key second" onclick="fun('+')">+</div> <div class="key third keynum" onclick="fun(0)">0</div> <div class="key" onclick="fun('.')">.</div> <div class="key second" onclick="compute()">=</div> </div> </div> <!-- home鍵 --> <div class="home"> </div> </div> <script type="text/javascript"> var span = document.querySelector(".sp"); /** * @param {Object} num * 點(diǎn)擊數(shù)字鍵盤 將數(shù)字替換到我們的span標(biāo)簽內(nèi)部 */ function fun(num){ var value = span.innerText; if(value == 0){ span.innerText = num; }else{ span.innerText = span.innerText.concat(num); } } /** * 計(jì)算結(jié)果的 */ function compute(){ var value = span.innerText; var result= eval(value); span.innerText = result; } /** * 清空計(jì)算區(qū)域 重置為0 */ function clearCompute(){ span.innerText="0"; } /** * 刪除計(jì)算區(qū)域的最后一個(gè)字符 */ function deleteLastNum(){ var value = span.innerText; console.log(typeof(value)) if(value == 0){ } else{ /** * value是一個(gè)字符串 * zs123 * 字節(jié):文本在計(jì)算器底層存儲(chǔ)的時(shí)候都是字節(jié)存儲(chǔ)的 編碼集 將字符轉(zhuǎn)成特定的字節(jié)在計(jì)算機(jī)上存儲(chǔ)的 * 字符:人類能看懂的一個(gè)字母 或者一個(gè)中文漢字 * a b 中 國(guó) aj * * 字符串就是一個(gè)一個(gè)字符組成的一個(gè)集合體 字符串String提供了很多常用的方法 以便我們對(duì)這個(gè)字符數(shù)組進(jìn)行相關(guān)操作 */ if(value.length!=1){ span.innerText = value.substring(0,value.length-1) }else{ span.innerText="0"; } } } </script> </body> </html>
效果圖:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- js精準(zhǔn)的倒計(jì)時(shí)函數(shù)分享
- 超精準(zhǔn)的javascript驗(yàn)證身份證號(hào)的方法
- js拖拉表格實(shí)現(xiàn)內(nèi)容計(jì)算
- js實(shí)現(xiàn)一個(gè)簡(jiǎn)易的計(jì)算器
- javascript模擬實(shí)現(xiàn)計(jì)算器
- javascript實(shí)現(xiàn)簡(jiǎn)易計(jì)算器功能
- JavaScript 如何計(jì)算文本的行數(shù)的實(shí)現(xiàn)
- JavaScript經(jīng)典案例之簡(jiǎn)易計(jì)算器
- js精準(zhǔn)計(jì)算
相關(guān)文章
用JS寫的一個(gè)Ajax庫(kù)(實(shí)例代碼)
下面小編就為大家?guī)硪黄肑S寫的一個(gè)Ajax庫(kù)(實(shí)例代碼)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-08-08js實(shí)現(xiàn)點(diǎn)擊圖片將圖片地址復(fù)制到粘貼板的方法
這篇文章主要介紹了js實(shí)現(xiàn)點(diǎn)擊圖片將圖片地址復(fù)制到粘貼板的方法,涉及js操作節(jié)點(diǎn)的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-02-02網(wǎng)頁(yè)源代碼保護(hù)(禁止右鍵、復(fù)制、另存為、查看源文件)
網(wǎng)頁(yè)源代碼保護(hù)(禁止右鍵、復(fù)制、另存為、查看源文件),只能起一些簡(jiǎn)單的防護(hù)2012-05-05用javascript實(shí)現(xiàn)div可編輯的常見方法
用javascript實(shí)現(xiàn)div可編輯的常見方法...2007-10-10JS表格組件BootstrapTable行內(nèi)編輯解決方案x-editable
這篇文章主要介紹了JS組件系列BootstrapTable行內(nèi)編輯解決方案:x-editable,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09JavaScript高階函數(shù)_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了JavaScript高階函數(shù),詳細(xì)講解了什么是高階函數(shù)和高階函數(shù)的用法,有興趣的可以了解下2017-06-06