特效介绍
这是一款非常好看的基于vue.js的计算器,支持普通计算器和科学计算器,点击计算器上面的Show Advanced Mode和Show Basic Mode即可进行切换。经过改造可以放在自己的网站上。
因为比较简单,所以就不提供下载地址了,您可以按照下面的方法一步一步实现。
使用方法
1、加入css
03 | background : linear-gradient(to right , #85D8CE , #085078 ); |
06 | font-family : 'Avenir' , Helvetica , Arial , sans-serif ; |
07 | -webkit-font-smoothing: antialiased; |
08 | -moz-osx-font-smoothing: grayscale; |
13 | justify- content : center ; |
22 | background-color : #333333 ; |
28 | border : 1px solid black ; |
29 | background-color : #333333 ; |
32 | margin : 0 0px 10px 0px ; |
37 | border-color : #03a9f4 ; |
38 | box-shadow: 0 0 4px #03A9F4 ; |
44 | border : 1px solid #0d0d0d ; |
48 | background-color : #1a1a1a ; |
55 | justify- content : space-evenly; |
58 | background-color : green ; |
63 | background-color : #333333 ; |
68 | text-shadow : -1px -1px 0 rgba( 0 , 0 , 0 , 0.35 ); |
73 | button::-moz-focus-inner { |
74 | border-color : transparent ; |
注意:不要和自己的css重复了
2、引入vue.js
3、在需要使用该计算器的地方加入html
02 | < div class = "calculator" > |
03 | < button @ click = "changeModeEvent" class = "toggle-button" > |
04 | < p v-if = "changeMode" >Show Advanced Mode ⚈</ p > |
05 | < p v-else>Show Basic Mode ⚆</ p > |
08 | < input class = "input" v-model = "current" /> |
10 | < div class = "mode" v-if = "changeMode" > |
11 | < button class = "button" @ click = "press" >7</ button > |
12 | < button class = "button" @ click = "press" >8</ button > |
13 | < button class = "button" @ click = "press" >9</ button > |
14 | < button class = "button" @ click = "press" >*</ button > |
15 | < button class = "button" @ click = "press" ><=</ button > |
16 | < button class = "button" @ click = "press" >C</ button > |
17 | < button class = "button" @ click = "press" >4</ button > |
18 | < button class = "button" @ click = "press($event)" >5</ button > |
19 | < button class = "button" @ click = "press" >6</ button > |
20 | < button class = "button" @ click = "press" >/</ button > |
21 | < button class = "button" @ click = "press" >(</ button > |
22 | < button class = "button" @ click = "press" >)</ button > |
23 | < button class = "button" @ click = "press" >1</ button > |
24 | < button class = "button" @ click = "press" >2</ button > |
25 | < button class = "button" @ click = "press" >3</ button > |
26 | < button class = "button" @ click = "press" >-</ button > |
27 | < button class = "button" @ click = "press" >x 2</ button > |
28 | < button class = "button" @ click = "press" >±</ button > |
29 | < button class = "button" @ click = "press" >0</ button > |
30 | < button class = "button" @ click = "press" >.</ button > |
31 | < button class = "button" @ click = "press" >%</ button > |
32 | < button class = "button" @ click = "press" >+</ button > |
33 | < button class = "button equal-sign" @ click = "press" >=</ button > |
35 | < div class = "mode" v-else> |
36 | < button class = "button" @ click = "press" >sin</ button > |
37 | < button class = "button" @ click = "press" >cos</ button > |
38 | < button class = "button" @ click = "press" >tan</ button > |
39 | < button class = "button" @ click = "press" >x^</ button > |
40 | < button class = "button" @ click = "press" ><=</ button > |
41 | < button class = "button" @ click = "press" >C</ button > |
42 | < button class = "button" @ click = "press" >log</ button > |
43 | < button class = "button" @ click = "press" >ln</ button > |
44 | < button class = "button" @ click = "press" >e</ button > |
45 | < button class = "button" @ click = "press" >°</ button > |
46 | < button class = "button" @ click = "press" >rad</ button > |
47 | < button class = "button" @ click = "press" >√</ button > |
48 | < button class = "button" @ click = "press" >7</ button > |
49 | < button class = "button" @ click = "press" >8 </ button > |
50 | < button class = "button" @ click = "press" >9</ button > |
51 | < button class = "button" @ click = "press" >/</ button > |
52 | < button class = "button" @ click = "press" >x 2</ button > |
53 | < button class = "button" @ click = "press" >x !</ button > |
54 | < button class = "button" @ click = "press" >4</ button > |
55 | < button class = "button" @ click = "press" >5</ button > |
56 | < button class = "button" @ click = "press" >6</ button > |
57 | < button class = "button" @ click = "press" >*</ button > |
58 | < button class = "button" @ click = "press" >(</ button > |
59 | < button class = "button" @ click = "press" >)</ button > |
60 | < button class = "button" @ click = "press" >1</ button > |
61 | < button class = "button" @ click = "press" >2</ button > |
62 | < button class = "button" @ click = "press" >3</ button > |
63 | < button class = "button" @ click = "press" >-</ button > |
64 | < button class = "button" @ click = "press" >%</ button > |
65 | < button class = "button" @ click = "press" >±</ button > |
66 | < button class = "button" @ click = "press" >0</ button > |
67 | < button class = "button" @ click = "press" >.</ button > |
68 | < button class = "button" @ click = "press" >π</ button > |
69 | < button class = "button" @ click = "press" >+</ button > |
70 | < button class = "button equal-sign" @ click = "press" >=</ button > |
4、初始化js
010 | press: function (event) { |
012 | let key = event.target.textContent |
036 | } else if (key === '=' ) { |
037 | if ((me.current).indexOf( '^' ) > -1) { |
038 | let base = (me.current).slice(0, (me.current).indexOf( '^' )) |
039 | let exponent = (me.current).slice((me.current).indexOf( '^' ) + 1) |
040 | me.current = eval( 'Math.pow(' + base + ',' + exponent + ')' ) |
042 | me.current = eval(me.current) |
044 | } else if (key === 'C' ) { |
046 | } else if (key === '*' ) { |
048 | } else if (key === '/' ) { |
050 | } else if (key === '+' ) { |
052 | } else if (key === '-' ) { |
054 | } else if (key === '±' ) { |
055 | if ((me.current).charAt(0) === '-' ) { |
056 | me.current = (me.current).slice(1) |
058 | me.current = '-' + me.current |
060 | } else if (key === '<=' ) { |
061 | me.current = me.current.substring(0, me.current.length - 1) |
062 | } else if (key === '%' ) { |
063 | me.current = me.current / 100 |
064 | } else if (key === 'π' ) { |
065 | me.current = me.current * Math.PI |
066 | } else if (key === 'x 2' ) { |
067 | me.current = eval(me.current * me.current) |
068 | } else if (key === '√' ) { |
069 | me.current = Math.sqrt(me.current) |
070 | } else if (key === 'sin' ) { |
071 | me.current = Math.sin(me.current) |
072 | } else if (key === 'cos' ) { |
073 | me.current = Math.cos(me.current) |
074 | } else if (key === 'tan' ) { |
075 | me.current = Math.tan(me.current) |
076 | } else if (key === 'log' ) { |
077 | me.current = Math.log10(me.current) |
078 | } else if (key === 'ln' ) { |
079 | me.current = Math.log(me.current) |
080 | } else if (key === 'x^' ) { |
082 | } else if (key === 'x !' ) { |
084 | if (me.current === 0) { |
086 | } else if (me.current < 0) { |
090 | for (let i = me.current; i > 0; i--) { |
095 | } else if (key === 'e' ) { |
096 | me.current = Math.exp(me.current) |
097 | } else if (key === 'rad' ) { |
098 | me.current = me.current * (Math.PI / 180) |
099 | } else if (key === '°' ) { |
100 | me.current = me.current * (180 / Math.PI) |
103 | changeModeEvent: function () { |
105 | me.changeMode = !me.changeMode |