gggogoing 2016-02-23
1、数学函数详解
<!doctype html> <html> <head> <!--声明当前页面的编码集:charset=gbk,gb2312(中文编码),utf-8国际编码--> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <!--当前页面的三要素--> <title>数学函数</title> <meta name="Keywords" content="关键词,关键词"> <meta name="description" content=""> <script type="text/javascript" src="sgutil.js"></script> </head> <body> <h1 id="colorCard">色卡一:李飘水</h1> <h1 id="colorCard2">色卡二:李飘水</h1> <script type="text/javascript"> //Math的常用方法 //1.Math.abs(data):求data的绝对值 //2.Math.sqrt(data):求data的平方根 //3.Math.pow(x,y):求x的y次方 //4.Math.max(x1,x2,x3,...,xn):求x元素中的最大值 //5.Math.min(x1,x2,x3,...,xn):求x元素中的最小值 //6.Math.floor(data):求小于等于data的最大整数 //7.Math.ceil(data):求大于等于data的最小整数 //8.Math.round(data):求四舍五入后的整数 //9.Math.random():求大于0,且小于1的随机数 console.log("Math.abs(-12)="+Math.abs(-12)); //12 console.log("Math.sqrt(2)="+Math.sqrt(2)); //1.414 console.log("Math.pow(2,3)="+Math.pow(2,3)); //8 console.log("Math.max(100,3,20)="+Math.max(100,3,20)); //100 console.log("Math.min(100,3,20)="+Math.min(100,3,20)); //3 console.log("Math.floor(12.85)="+Math.floor(12.85)); //12 console.log("Math.ceil(12.35)="+Math.ceil(12.35)); //13 console.log("Math.random()="+Math.random()); console.log("Math.round(12.47)="+Math.round(12.47)); //12 console.log("Math.round(12.57)="+Math.round(12.57)); //13 //返回start至end的随机数 function rangeRandom(start, end) { return Math.floor(Math.random()*(end-start+1))+start; } /* setInterval(function() { console.log(rangeRandom(0, 5)); },1000); */ //方案一色卡 function randomColor() { var r = Math.floor(Math.random()*256); var g = Math.floor(Math.random()*256); var b = Math.floor(Math.random()*256); return "rgb("+r+","+g+","+b+")"; //IE7不支持rgb } //调用方案一色卡 setInterval(function() { var colorCardDom = document.getElementById("colorCard"); colorCardDom.style.color = randomColor(); },1000); //方案二色卡 function randomColor2() { //将0-255的随机整数转换成十六进制数 var r = Math.floor(Math.random()*256).toString(16); var g = Math.floor(Math.random()*256).toString(16); var b = Math.floor(Math.random()*256).toString(16); //如果转换后的十六进制数不足两位,则在前面补0 if(r.length < 2) { r = "0" + r; } if(g.length < 2) { g = "0" + g; } if(b.length < 2) { b = "0" + b; } return "#"+r+g+b; } //调用方案二的色卡 setInterval(function() { var colorCardDom = document.getElementById("colorCard2"); colorCardDom.style.color = randomColor2(); },1000); </script> </body> </html>