一区二区三区日韩精品-日韩经典一区二区三区-五月激情综合丁香婷婷-欧美精品中文字幕专区

分享

原生JavaScript實(shí)現(xiàn)網(wǎng)頁版計(jì)算器

 新用戶604881da 2021-11-11

本文實(shí)例為大家分享了JavaScript實(shí)現(xiàn)網(wǎng)頁版計(jì)算器的具體代碼,供大家參考,具體內(nèi)容如下

由于無聊看電腦上的系統(tǒng)軟件翻到了計(jì)算器這個(gè)功能,就簡單寫一下這個(gè)計(jì)算器的功能吧,這個(gè)網(wǎng)頁版計(jì)算器基本功能都有吧,但是不是很完全,僅供參考。

 

首先是網(wǎng)頁計(jì)算器的樣式部分不想手寫直接復(fù)制即可

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

<!DOCTYPE html>

<html lang="zh-CN">

<head>

    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Document</title>

    <style>

        * {

            margin: 0;

            padding: 0;

            box-sizing: border-box;

        }

        .cal {

            width: 420px;

            margin: 100px auto;

            background-color: #E6E6E6;

            padding: 2px;

            overflow: hidden;

        }

        .show {

            position: relative;

            width: 416px;

            height: 120px;

            font-size: 50px;

            line-height: 50px;

            font-weight: 700;

        }

        .show button {

            display: none;

            position: absolute;

            top: -2px;

            right: -2px;

            width: 60px;

            height: 40px;

            line-height: 40px;

            text-align: center;

            border: transparent;

            background-color: #E6E6E6;

            font-size: 30px;

            font-weight: 100;

            cursor: pointer;

        }

        .show button:hover {

            background-color: #e81123;

            color: #f0f0f0

        }

        .res,

        .left,

        .right {

            position: absolute;

            bottom: 0;

            height: 60px;

            line-height: 60px;

            padding: 0 3px;

        }

        .res {

            right: 0;

            /* width: 100%; */

            text-align: right;

        }

        .left {

            display: none;

            background-color: #E6E6E6;

        }

        .right {

            display: none;

            right: 0;

            background-color: #E6E6E6;

        }

        .left:hover,

        .right:hover {

            color: #2e8eda;

        }

        .keyboard {

            display: flex;

            flex-wrap: wrap;

            justify-content: center;

        }

        .btn {

            display: flex;

            justify-content: center;

            width: 100px;

            height: 60px;

            line-height: 60px;

            margin: 2px;

            background-color: #f0f0f0;

            border: transparent;

            font-size: large;

        }

        .btn:hover {

            background-color: #d6d6d6;

        }

        .digital {

            background-color: #fafafa;

            font-weight: 700;

        }

        .equal {

            background-color: #8abae0;

        }

        .equal:hover {

            background-color: #4599db;

        }

    </style>

</head>

<body>

    <div class="cal">

        <div class="show">

            <button class="close">×</button>

            <div class="res">0</div>

            <div class="left"><</div>

            <div class="right">></div>

        </div>

        <div class="keyboard">

            <!-- 0 -->

            <button class="btn percent">%</button>

            <!-- 1 -->

            <button class="btn clearOne">CE</button>

            <!-- 2 -->

            <button class="btn clearAll">C</button>

            <!-- 3 -->

            <button class="btn back">del</button>

            <!-- 4 -->

            <button class="btn div1">1/x</button>

            <!-- 5 -->

            <button class="btn square">x2</button>

            <!-- 6 -->

            <button class="btn sqrt">2√x</button>

            <!-- 7 -->

            <button class="btn div">÷</button>

            <!-- 8 -->

            <button class="btn digital">7</button>

            <!-- 9 -->

            <button class="btn digital">8</button>

            <!-- 10 -->

            <button class="btn digital">9</button>

            <!-- 11 -->

            <button class="btn mul">x</button>

            <!-- 12 -->

            <button class="btn digital">4</button>

            <!-- 13 -->

            <button class="btn digital">5</button>

            <!-- 14 -->

            <button class="btn digital">6</button>

            <!-- 15 -->

            <button class="btn sub">-</button>

            <!-- 16 -->

            <button class="btn digital">1</button>

            <!-- 17 -->

            <button class="btn digital">2</button>

            <!-- 18 -->

            <button class="btn digital">3</button>

            <!-- 19 -->

            <button class="btn add">+</button>

            <!-- 20 -->

            <button class="btn neg">+/-</button>

            <!-- 21 -->

            <button class="btn digital">0</button>

            <!-- 22 -->

            <button class="btn digital">.</button>

            <!-- 23 -->

            <button class="btn equal">=</button>

        </div>

    </div>

    <script src="./計(jì)算機(jī).js"></script>

</body>

</html>

js部分:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

const bt = document.querySelectorAll('.keyboard button')

const close = document.querySelector('.close')

const res = document.querySelector('.res')

//當(dāng)點(diǎn)擊的數(shù)字的時(shí)候

let k = 0

let one

let two

function arr(num) {

    bt[num].onclick = function () {

        res.innerText += bt[num].innerText

        res.innerText = parseFloat(res.innerText)

        // console.log(one)

    }

}

//小數(shù)點(diǎn)

//保留結(jié)果小數(shù)

function fn() {

    if (res.innerText.length > 8) {

        res.innerText = res.innerText.slice(0, 10)

    }

    if (res.innerText == 'NaN') {

        res.innerText = 0

    }

}

//當(dāng)點(diǎn)擊的是運(yùn)算符號的時(shí)候

function symbol(str, fu) {

    bt[str].onclick = function () {

        k++

        if (k > 1) {

            return

        }

        one = parseFloat(res.innerText)

        // switch (fu) {

        //     case '+':

        //         one += one

        //         break;

        //     case '-':

        //         one -= one

        //         break;

        //     case '*':

        //         one *= one

        //         break;

        //     case '/':

        //         one /= one

        //         break;

        // }

        res.innerText = ''

        close.style.display = 'block'

        close.innerText = bt[str].innerText

        console.log(one)

    }

}

arr(21)

arr(18)

arr(17)

arr(16)

arr(14)

arr(13)

arr(12)

arr(10)

arr(9)

arr(8)

arr(22)

//運(yùn)算符號

symbol(0)

symbol(7, '/')

symbol(11, '*')

symbol(15, '-')

symbol(19, '+')

console.log(bt[22].innerText)

bt[22].onclick = function () {

    res.innerText += bt[22].innerText

    console.log(565)

}

bt[23].onclick = function () {

    two = parseFloat(res.innerText)

    switch (close.innerText) {

        case '%':

            //toFixed(11)保留11位小數(shù)

            res.innerText = one % two

            k = 0

            break;

        case '+':

            res.innerText = one + two

            k = 0

            break;

        case '-':

            res.innerText = one - two

            k = 0

            break;

        case 'x':

            res.innerText = one * two

            k = 0

            break;

        case '÷':

            res.innerText = one / two

            k = 0

            break;

    }

    // console.log(res.innerText.length)

    fn()

}

bt[1].onclick = function () {

    res.innerText = ''

}

bt[2].onclick = function () {

    res.innerText = '0'

    close.innerText = 'x'

    close.style.display = ''

    one = 0

    two = 0

}

bt[3].onclick = function () {

    res.innerText = res.innerText.slice(0, res.innerText.length - 1)

    if (res.innerText.length === 0) {

        res.innerText = '0'

        return

    }

}

bt[4].onclick = function () {

    res.innerText = 1 / parseFloat(res.innerText)

    fn()

}

bt[5].onclick = function () {

    res.innerText = parseFloat(res.innerText) * parseFloat(res.innerText)

    fn()

}

bt[6].onclick = function () {

    res.innerText = Math.sqrt(parseFloat(res.innerText))

    fn()

}

bt[20].onclick = function () {

    res.innerText = 0 - parseFloat(res.innerText)

    fn()

}

    本站是提供個(gè)人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    亚洲中文字幕亲近伦片| 黑丝国产精品一区二区| 免费精品国产日韩热久久| 国产日本欧美特黄在线观看| 午夜福利激情性生活免费视频| 日韩亚洲激情在线观看| 久久福利视频视频一区二区 | 国产精品视频一级香蕉| 成人精品欧美一级乱黄| 国产日韩精品欧美综合区| 国产精品夜色一区二区三区不卡| 久热人妻中文字幕一区二区| 精品伊人久久大香线蕉综合| 精品午夜福利无人区乱码| 国产精品不卡高清在线观看| 国产亚洲成av人在线观看| 亚洲第一区欧美日韩在线| 九九热在线视频精品免费| 亚洲黄片在线免费小视频| 欧美成人精品国产成人综合| 午夜精品黄片在线播放| 欧美不卡午夜中文字幕| 亚洲二区欧美一区二区| 日本三区不卡高清更新二区| 成人午夜在线视频观看| 色婷婷国产精品视频一区二区保健 | 亚洲熟女精品一区二区成人| 亚洲熟女精品一区二区成人| 欧美一区二区三区不卡高清视| 国产亚洲视频香蕉一区| 亚洲男女性生活免费视频| 激情内射亚洲一区二区三区| 亚洲精品福利入口在线| 亚洲精品黄色片中文字幕| 国产精品午夜福利免费阅读 | 亚洲欧美日韩中文字幕二欧美| 99热在线播放免费观看| 国产99久久精品果冻传媒| 日韩精品日韩激情日韩综合| 欧美视频在线观看一区| 国产精品乱子伦一区二区三区|