关于Math常用的方法

1. 常用的Math用法

Math.random() //0-1 的随机数

Math.round() //四舍五入取整

Math.ceil() //向上取整

Math.floor() //向下取整

Math.abs() //绝对值

Math.max(num1,num2....) //比较最大值

Math.min(num1,num2....) //比较最小值

Math.PI //π 3.1415926 - 3.1415927

Math.pow(x,y) //x的y次方

Math.sqrt() //开平方

Math.sin

Math.cos

Math.tan

注:

常用弧度来表示角度

eg: b=c*sin(x); //x表示一个度数。用弧度表示角度 (2*Math.PI/360)*x;

2.案例

a.做一个六边形

 var z = 150 / Math.tan((2 * Math.PI / 360) * 30);
        for (var i = 0; i < 6; i++) {
            box.innerHTML += '<div style="transform-origin: center center '+(-z)+'px;transform: translateZ('+z+'px) rotateY('+(i*60)+'deg);background:'+randomColor()+';"></div>';
        }

        //随机色(用16进制颜色值表示)
        function randomColor() {
            var str = "0123456789abcdef"
            var color = '#';
            for (var i = 0; i < 6; i++) {
                //每一次都随机一个0-15下标
                color += str.charAt((Math.random() * 15));
            }
           return color;
        }

    </script>
效果:
 

 

 

b.抛物线案例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0
        }
        div:nth-of-type(1) {
            width: 600px;
            height: 2px;
            background: red;
            position: absolute;
            top: 300px;
        }
        div:nth-of-type(2) {
            width: 2px;
            height: 600px;
            background: blue;
            position: absolute;
            left: 300px;
        }
        span {
            display: block;
            width: 2px;
            height: 2px;
            position: absolute;
            border-radius: 1px;
            background: red;
        }
    </style>
</head>
<body>
    <div></div>
    <div></div>
    <script>
        for(var x= -300; x<=300;x++) {
            var y = 0.01 * Math.pow(x,2);
            document.write('<span style="left:' +(x+300)+ 'px;top:' +(y+300)+'px;"></span>');
        }
    </script>
</body>
</html>
显示效果:

 

 

 

 

转载于:https://www.cnblogs.com/lxz123/p/11489705.html

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值