函数
1.函数实际上就是可以作为一个逻辑单元对待的代码块
2.作用:使代码更加简洁,提高重用性
3.地位:每一个项目中的代码,大约95%在函数里
4.函数的定义(重点)
function 函数名([x,y,z]){return 表达式}
函数名:在同一个页面中函数名必须唯一,参数部分选填,参数用逗号隔开,最多可以添加255个,return选填
关于return
没有return 函数只代表一些列的逻辑运算
有return的 函数返回一个特定值,此时函数可以作为一个值参与到运算中
return后则结束函数
5.函数的调用(重点) :函数定义后不会自动执行,需要在特定的位置调用
6.函数的参数
实参:在调用函数时小括号里的内容
形参:形式参数,就是函数名后小括号里的内容
传参:将实参传递给形参,用形参表示实参
agruments:函数的参数就是作为一个数组返回的
练习
函数实现小球变色移动
代码如下所示:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=/, initial-scale=1.0">
<title>Document</title>
<style>
@keyframes roll {
from {
transform: rotate(0);
filter: hue-rotate(0deg);
}
to {
transform: rotate(360deg);
filter: hue-rotate(360deg);
}
}
* {
margin: 0;
padding: 0;
}
.wrap {
width: 100vw;
height: 100vh;
position: relative;
overflow: hidden;
background-color: #111;
}
.ball {
width: 40px;
height: 40px;
/* background-color: pink; */
border-radius: 50%;
position: absolute;
top: 130px;
left: 0;
animation: roll 2s linear infinite;
}
.ball div {
width: 96%;
height: 96%;
background-color: #111;
border-radius: 50%;
filter: blur(5px);
}
</style>
</head>
<body>
<div class="wrap">
<div class="ball">
<div></div>
</div>
<div class="ball">
<div></div>
</div>
<div class="ball">
<div></div>
</div>
<div class="ball">
<div></div>
</div>
<div class="ball">
<div></div>
</div>
<div class="ball">
<div></div>
</div>
<div class="ball">
<div></div>
</div>
<div class="ball">
<div></div>
</div>
<div class="ball">
<div></div>
</div>
<div class="ball">
<div></div>
</div>
<div class="ball">
<div></div>
</div>
<div class="ball">
<div></div>
</div>
<div class="ball">
<div></div>
</div>
<div class="ball">
<div></div>
</div>
<div class="ball">
<div></div>
</div>
<div class="ball">
<div></div>
</div>
<div class="ball">
<div></div>
</div>
<div class="ball">
<div></div>
</div>
<div class="ball">
<div></div>
</div>
<div class="ball">
<div></div>
</div>
</div>
</body>
<script>
var dBalls = document.querySelectorAll('.ball');
var timer = null;
// 获取浏览器窗口的宽度和高度
var sw = window.innerWidth;
var sh = window.innerHeight;
var vx = 0;
var vy = 0;
// 自执行函数
(function() {
for (var i = 0; i < dBalls.length; i++) {
var ball = dBalls[i];
// 为小球添加随机速度
ball.vx = Math.random() > 0.5 ? rand(1, 3) : -rand(1, 3);
ball.vy = Math.random() > 0.5 ? rand(1, 3) : -rand(1, 3);
// 设置随机起点
ball.style.left = rand(0, sw - ball.offsetWidth) + 'px';
ball.style.top = rand(0, sh - ball.offsetHeight) + 'px';
// 随机大小
var w = rand(60, 100);
ball.style.width = w + 'px';
ball.style.height = w + 'px';
// 设置随机颜色
ball.style.backgroundColor = randColor();
}
})()
// 小球移动
function move(dBall) {
// 每一次时间间隔,让dBall进行一次单位移动
// dBall的新位置 = dBall的当前位置 + 速度
// offsetLeft 距离定位父级的左侧偏移量(只读)
// offsetTop 距离定位父级的顶部偏移量(只读)
// 边界处理
var l = dBall.offsetLeft + dBall.vx;
var t = dBall.offsetTop + dBall.vy;
// 边界处理
if (l > sw - dBall.offsetWidth) {
l = sw - dBall.offsetWidth;
dBall.vx = -dBall.vx;
} else if (l < 0) {
l = 0;
dBall.vx = -dBall.vx;
}
if (t > sh - dBall.offsetHeight) {
t = sh - dBall.offsetHeight;
dBall.vy = -dBall.vy;
} else if (t < 0) {
t = 0;
dBall.vy = -dBall.vy;
}
dBall.style.left = l + 'px';
dBall.style.top = t + 'px';
}
//开启定时器
timer = setInterval(function() {
for (var i = 0; i < dBalls.length; i++) {
var ball = dBalls[i];
move(ball);
}
}, 30)
function rand(min, max) {
return Math.round(Math.random() * (max - min) + min);
}
function randColor() {
return `rgb(${rand(0,255)},${rand(0,255)},${rand(0,255)})`;
}
// function colorRotate() {
// return randColor().rotate(360 + 'deg');
// }
</script>
</html>