目录
一、map
map:映射,由键值对构成的元素集合
let map = new Map();
set(key,value)向集合中添加一个元素:key已存在,则为改,不存在则为增
get(键):根据key去取值
delete(键):删除集合中某个数
has(键):判断集合中是否有某个值
clear():清空集合
遍历:for(let item of map){
console.log(item[0],item[1])
}
二、Dom运动
匀速运动
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
padding: 0;
margin: 0;
}
div{
width:100px;
height:100px;
background-color: aquamarine;
position: absolute;
top:200px;
}
</style>
</head>
<body>
<div class='box'></div>
<button class='but'>开始</button>
</body>
</html>
<script>
let mDiv=document.querySelector(".box")
let mBut=document.querySelector(".but")
let time=null;
function startmove(obj,target){
clearInterval(time);
time=setInterval(function(){
obj.style.left=obj.offsetLeft+5+'px';
if(obj.offsetLeft==target){
clearInterval(time)
}
},50)
}
mBut.onclick=function(){
startmove(mDiv,500)
}
</script>
往返匀速运动
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
padding: 0;
margin: 0;
}
div{
width:100px;
height:100px;
background-color: aquamarine;
position: absolute;
top:200px;
left: 200px;
}
</style>
</head>
<body>
<div class='box'></div>
<button class='but'>左</button>
<button class='but'>右</button>
</body>
</html>
<script>
let mDiv=document.querySelector(".box")
let mBut=document.querySelectorAll(".but")
let time=null;
function startmove(obj,target){
clearInterval(time);
time=setInterval(function(){
obj.style.left=obj.offsetLeft+(target>obj.offsetLeft?5:-5)+'px';
if(obj.offsetLeft+(target>obj.offsetLeft?5:-5)==target){
clearInterval(time);
}
},50)
}
mBut[0].onclick=function(){
startmove(mDiv,0)
}
mBut[1].onclick=function(){
startmove(mDiv,500)
}
</script>
匀速透明运动
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div{
width: 300px;
height: 300px;
background: url(./img/图片1.png);
background-size:100% ;
position: absolute;
opacity: 0.5;
}
</style>
</head>
<body>
<div>
</div>
</body>
</html>
<script>
let mBox=document.querySelector("div")
let time=null;
function startChange(obj,target){
clearInterval(time)
time=setInterval(function(){
let speed=getComputedStyle(obj,false)["opacity"]>target?-0.05:0.05;
obj.style.opacity=+getComputedStyle(obj,false)["opacity"]+speed;
if(getComputedStyle(obj,false)["opacity"]==target){
clearInterval(time)
}
},50)
}
mBox.onmouseover=function(){
startChange(mBox,1)
}
mBox.onmouseout=function(){
startChange(mBox,0)
}
</script>
缓冲运动
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
padding: 0;
margin: 0;
}
div{
width:100px;
height:100px;
background-color: aquamarine;
position: absolute;
top:200px;
}
</style>
</head>
<body>
<div class='box'></div>
<button class='but'>开始</button>
</body>
</html>
<script>
let mDiv=document.querySelector(".box")
let mBut=document.querySelector(".but")
let time=null;
function startMove(obj,target){
clearInterval(time)
time=setInterval(function(){
let speed=(target-obj.offsetLeft)/10;
speed>0?Math.ceil(speed):Math.floor(speed);
obj.style.left=obj.offsetLeft+speed+"px";
if(obj.offsetLeft==target){
clearInterval(time)
}
},50)
}
mBut.onclick=function(){
startMove(mDiv,800)
}
</script>
反弹运动
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
padding: 0;
margin: 0;
}
div{
width:80px;
height:80px;
background-color: aquamarine;
position: absolute;
top:200px;
border-radius:50% ;
}
</style>
</head>
<body>
<div class='box'></div>
<button class='but'>开始</button>
</body>
</html>
<script>
let mDiv=document.querySelector(".box")
let mBut=document.querySelector(".but")
function startMove(obj){
let speedX=10;
let speedY=10;
time=setInterval(function(){
if(obj.offsetLeft<0||obj.offsetLeft>innerWidth-obj.offsetWidth){
speedX *=-1
}
if(obj.offsetTop<0||obj.offsetTop>innerHeight-obj.offsetHeight){
speedY*=-1
}
obj.style.left=obj.offsetLeft+speedX+"px"
obj.style.top=obj.offsetTop+speedY+"px"
},50)
}
mBut.onclick = function(){
startMove(mDiv);
}
</script>