移动动画(简易版)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>动画演示</title>
</head>
<style>
    *{
        margin: 0;
        padding: 0;
    }
    .begin{
        width:100vw;
        height: 100vh;
        background-color: pink;
    }
    .box{
        width:200px;
        height:200px;
        background-color: blue;
        color:white;
        margin-top: 30px;
        margin-left: 100px;
    }
    /* 开始定义动画 */
    @keyframes begin{
        0%{
            transform: translate(0,0);
        }
        100%{
            transform: translate(400px,400px);
        }
    }
    @keyframes reverseBegin{
        0%{
            transform: translate(400px,400px);
        }
        100%{
            transform: translate(0,0);
        }
    }
    /* 定义完动画就该使用动画了,使用动画也是使用类名 */
    .action{
        animation: begin 5s linear forwards;
    }
    .backAction{
        animation: reverseBegin 5s linear forwards;
    }
</style>
<body>
    <div class="begin">
        <span>兄弟们,这是一个css动画演示 我们使用到的属性是@keyframes,transform,translate,scale();</span>
        <button class="Yaction"style="margin-right: 30px;">点击开始动画</button>
        <button class="Baction">点击回来动画</button>

        <div class="box">我是一个要移动的盒子</div>
    </div>

    <script>
        var Yaction=document.querySelector('.Yaction');
        var Baction=document.querySelector('.Baction');
        var box=document.querySelector('.box');
        
        Yaction.addEventListener('click',function(){
            box.classList.remove('backAction');
            box.classList.add('action');
        })
        
        Baction.addEventListener('click',function(){
            box.classList.remove('action');
            box.classList.add('backAction');
        })
    </script>
</body>
</html>

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值