day 15 运动(map、dom运动)

这篇博客介绍了JavaScript中实现DOM元素的各种动画效果,包括匀速运动、往返匀速运动、匀速透明运动、缓冲运动和反弹运动。通过设置定时器改变元素的位置和透明度,实现了平滑的动画过渡。这些基础的动画技巧对于前端开发者来说是必备的知识点。
摘要由CSDN通过智能技术生成

目录

一、map

二、Dom运动

匀速运动

往返匀速运动

匀速透明运动

缓冲运动

反弹运动


一、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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值