Blogger Information
Blog 25
fans 0
comment 0
visits 19636
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
Math对象+随机色3-28
moonheart的博客
Original
674 people have browsed it

实例

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>javascript-Math对象(随机色)</title>
	<style type="text/css">

	</style>
</head>
<body>
<button onclick="roundColor();">随机颜色背景色</button>
<script type="text/javascript">
	//Math 对象的作用
	//round()方法可以把一个数字四舍五入为最接近的整数
	//	语法:Math.round(x)
	var a=Math.round(2.8);
	document.write(a+'round()<hr>');
	//random()方法返回一个介于0~1之间的一个随机数
	document.write('随机数:Math.random()<hr>'+Math.random());
	// floor()方法返回小于等于x的最大整数;(如果传递的参数是一个整数,该值不变)
	var b=Math.floor(3.8);
	document.write('<br>floor()方法返回小于等于x的最大整数<hr>'+b);
	//取1~10的随机数*10+1
	var c=Math.floor(Math.random()*10+1);
	document.write('<br>取1~10的随机数<hr>'+c);
	//取1~100的随机数:*100+1
	var c=Math.floor(Math.random()*100+1);
	document.write('<br>取1~100的随机数<hr>'+c);
	//[案例]随机选择颜色,十六进制颜色
	function roundColor(){
		var bg='#'//背景色
		var r=Math.floor(Math.random()*10).toString()+Math.floor(Math.random()*10);
		var g=Math.floor(Math.random()*10).toString()+Math.floor(Math.random()*10);
		var b=Math.floor(Math.random()*10).toString()+Math.floor(Math.random()*10);
		bg+=r+g+b;
		document.getElementsByTagName('body')[0].style.background=bg;

	}
</script>

</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例

微信截图_20190329150523.png微信截图_20190329150540.png

Correction status:Uncorrected

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post