當(dāng)前位置:
首頁(yè) >
前端技术
> javascript
>内容正文
javascript
JavaScript 四. Math对象的属性和方法
生活随笔
收集整理的這篇文章主要介紹了
JavaScript 四. Math对象的属性和方法
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <!-- 定時(shí)刷新網(wǎng)頁(yè) -->
5 <!-- <meta http-equiv="refresh" content="1"> -->
6 <title></title>
7 <script type="text/javascript">
8 /*
9 Math對(duì)象
10 靜態(tài)對(duì)象, 不需要?jiǎng)?chuàng)建實(shí)例, 可以直接使用
11 */
12
13 // 圓周率
14 document.write(Math.PI);
15 document.write("<hr>");
16
17 // 絕對(duì)值
18 document.write(Math.abs(-9));
19 document.write("<hr>");
20
21 // 向上取整 去掉小數(shù), 整數(shù)+1
22 document.write(Math.ceil(3.2));
23 document.write("<hr>");
24
25 // 向下取整 去掉小數(shù)
26 document.write(Math.floor(3.9));
27 document.write("<hr>");
28
29 // 四舍五入
30 document.write(Math.round(3.5));
31 document.write("<hr>");
32
33 document.write(Math.round(3.1));
34 document.write("<hr>");
35
36 // x的y次方
37 document.write(Math.pow(2, 24));
38 document.write("<hr>");
39
40 // 開方
41 document.write(Math.sqrt(16));
42 document.write("<hr>");
43
44 // 0 - 1 的隨機(jī)數(shù)
45 document.write(Math.random());
46 document.write("<hr>");
47
48 // 實(shí)例1: 兩個(gè)數(shù)之間的隨機(jī)整數(shù)
49 function getRandom(min, max) {
50
51 // 隨機(jī)整數(shù)
52 random = Math.random() * (max - min) + min;
53
54 // 向下取整
55 random = Math.floor(random);
56
57 // 返回隨機(jī)整數(shù)
58 return random;
59 }
60
61 // 調(diào)用函數(shù)
62 document.write(getRandom(0, 10));
63 document.write("<hr>");
64
65 // 實(shí)例2: 隨機(jī)網(wǎng)頁(yè)背景色
66
67 // 得到隨機(jī)顏色值
68 // (1) 三個(gè)隨機(jī)數(shù)并轉(zhuǎn)換為16進(jìn)制
69 var red = getRandom(0, 256).toString(16);
70 var green = getRandom(0, 256).toString(16);
71 var blue = getRandom(0, 256).toString(16);
72 var color = "#" + red + green + blue;
73 // document.write(red + " ");
74 // document.write(green + " ");
75 // document.write(blue + " ");
76
77 // (2) 寫CSS樣式
78 var str = "";
79 str += '<style type="text/css">';
80 str += 'body {';
81 str += ' background-color:' + color + ';';
82 str += '}';
83 str += '</style>';
84
85 // 將CSS樣式寫入網(wǎng)頁(yè)
86 // document.write(str);
87
88 // 第二種方式
89 // (1) 得到隨機(jī)顏色值
90 var color = "#" + (getRandom(0, Math.pow(2, 24)).toString(16));
91
92 /* (2) 設(shè)置網(wǎng)頁(yè)背景色
93 document: 網(wǎng)頁(yè)對(duì)象
94 body: 子對(duì)象
95 bgColor: body的屬性
96 除了body以外, 其它標(biāo)記必須使用id來訪問
97 */
98 document.body.bgColor = color;
99 </script>
100 </head>
101 <body>
102
103 </body>
104 </html>
?
轉(zhuǎn)載于:https://www.cnblogs.com/ZeroHour/p/6364628.html
《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的JavaScript 四. Math对象的属性和方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CVE-2017-5521: Bypas
- 下一篇: 构建之法第三章