发表于: 2023-02-16 19:07:32

0 133




今天的js知识点:

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <title>Title</title>
   <style>
       div {
width: 200px;
           height: 200px;
       }
</style>
</head>
<body>
   <div></div>

   <script>
       // 数学内置对象
       // console.log(Math.PI)
       // //方法 Math.ceil() 向上取整
       // console.log(Math.ceil(1.1)) // 2 向上取整
       // console.log(Math.ceil(-1.1)) // -1 向上取整
       // console.log('---------------------------------')
       // //Math.floor() 向下取整
       // console.log(Math.floor(1.1)) //// 1 向下取整
       // console.log(Math.floor(-1.1)) //// -2 向下取整
       // console.log('---------------------------------')
       // // Math.round() 四舍五入
       // console.log(Math.round(3.5)) // 4  四舍五入
       // console.log(Math.round(3.4)) // 3  四舍五入
       // console.log(Math.round(-20.5))  //-20
       // console.log(Math.round(-20.51))  //-21
       // console.log('---------------------------------')
       // //Math.sqrt()开平方
       // console.log(Math.sqrt(9))  //3
       // // Math.random() 返回一个0-1之间的数 左闭右开 能取到0 取不到1
       // console.log(Math.random())
       // console.log(Math.random() * (10 + 1))
       // console.log(Math.floor(Math.random() * (10 + 1)))

       // 数组中随机取数 小案例
       // let arr = ['red', 'green', 'blue']
       // let random = Math.floor(Math.random() * arr.length)
       // console.log(arr[random])
       //
       // //如何生成 N-M之间的随机数
       // // Math.random() * (M - N + 1) + N
       // function getRandom(N, M) {
       //     return Math.floor(Math.random() * (M - N + 1) + N)
       // }
       // console.log(getRandom(4,8))

       // 得到随机数
       // let arr = ['赵云', '刘备', '张飞', '马超', '关羽', '黄忠', '曹操']
       // let random = Math.floor(Math.random() * arr.length)
       // // 获取随机数
       // document.write(arr[random])
       //
       // // splice(起始的位置, 删除几个元素)
       // arr.splice(random, 1)
       // console.log(arr)

       //生成随机数案例 猜数字大小
       //随机生成一个  1-10 的数字
       // let random = Math.floor(Math.random() * (10 + 1))
       // function getRandom(N, M) {
       //     return Math.floor(Math.random() * (M - N + 1) + N)
       // }
       // let random = getRandom(0, 10)

       // while (true) {
       //     let a = +prompt('请输入数字:')
       //     if (a > random) {
       //         alert('猜大了')
       //     }else if(a < random) {
       //         alert('猜小了')
       //     }else {
       //         alert('猜对了')
       //         break
       //     }
       // }

       // 设定最多只能猜三次
       // flag = true //开关变量
       // for(let i = 1; i <= 3; i++) {
       //     let a = +prompt('请输入1-10之间的数字,您有三次机会:')
       //     if (a > random) {
       //         alert('猜大了')
       //     } else if(a < random) {
       //         alert('猜小了')
       //     } else {
       //         flag = false
       //         alert('猜对了')
       //         break
       //     }
       // }
       // if(flag) {
       //     alert('对不起,次数用完了')
       // }
       // console.log(random)

       //随机颜色案例
       //自定义一个随机颜色
       function getRandomColor(flag = true) {
if(flag) {
// 如果是true则返回 #ffffff
               let str = '#'
               let arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                   'a', 'b', 'c', 'd', 'e', 'f']
for(let i = 1; i <= 6; i++) {
let random = Math.floor(Math.random() * arr.length)
//每次都要随机从数组里抽一个
                   str += arr[random]
}
return str
}else {
// 否则是返回 false 返回rgb(255,255,255)
               let r = Math.floor(Math.random() * 256)
let g = Math.floor(Math.random() * 256)
let b = Math.floor(Math.random() * 256)
return `rgb(${r},${g},${b})`
           }
}
// 调用函数 getRandomColor(布尔值)
       getRandomColor(false)
console.log(getRandomColor(false))
console.log(getRandomColor(true))
console.log(getRandomColor())

const div = document.querySelector('div')
div.style.backgroundColor = getRandomColor()

//渲染学成在线案例

   </script>

</body>
</html>



返回列表 返回列表
评论

    分享到