html-动画效果

目录

一 旋转方块:

二 旋转缩放

三:样式变化动画化(鼠标触发)

四 简单的导航实现

导航效果一:

导航效果2:


一 旋转方块:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    .box{
        width: 200px;
        height: 200px;
        background-color: red;
        border-radius: 50px;
        animation: run 5s linear infinite;
    }
    @keyframes run {
        0%{
            transform: translateX(0) rotate(0);
        }
        50%{
            transform: translateX(200px) rotate(360deg);
        }
        100%{
            transform: translateX(200px) rotate(0);
        }
        
    }
</style>
<body>
    <div class="box"></div>
</body>
</html>

效果图:

可以通过修改translateX来改变旋转的方向 translateY则绕着Y轴旋转

linear线性

infinite无限循环

不同的百分号比例对旋转速度所占比有影响

———————————————————————————————————————————

二 旋转缩放

//旋转缩放
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    .box{
        width: 200px;
        height: 200px;
        background-color: red;
        border-radius: 50px;
        animation: run 5s linear infinite;
    }
    @keyframes run {
        0%{
            transform: translate(0) rotate(0) scale(1);
        }
        50%{
            transform: translate(200px,200px) rotate(360deg) scale(2);
        }
        100%{
            transform: translate(0,0) rotate(0) scale(1);
        }
        
    }
</style>
<body>
    <div class="box"></div>
</body>
</html>

效果图:

———————————————————————————————————————————

三 样式变化动画化(鼠标触发)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    .box{
        width: 200px;
        height: 200px;
        background-color: red;
        /* 样式变化时,会以动画形式过渡 */
        transition: all 2s;
    }
    /* 鼠标悬停在上面触发动画 */
    .box:hover{
        width: 400px;
        height: 300px;
        background-color: yellow;

    }
    
</style>
<body>
    <div class="box"></div>
</body>
</html>

变化前:

变化后:———————————————————————————————————————————

四 简单的导航实现

导航效果一:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    .box{
        overflow: hidden;
        margin-top: 50px;
    }
    .title{
        border: 1px solid;
        float: left;
           /* margin 是针对div外部的四个方位 */
        margin-left: 30px;
        /* padding 是针对div内部的四个方位 */
        padding-left: 20px;
        padding-right: 20px;
        padding-top: 10px;
        padding-bottom: 10px;
        transition: all 0.6s;
    }
    .title:hover{
        background-color: red;
        color: white;
        border-radius: 20px;
        /* 箭头变手指 */
        cursor: pointer;
    }
    
    
    
</style>
<body>
    <div class="box">
        <div class="title">图片</div>
        <div class="title">视频</div>
        <div class="title">翻译</div>
        <div class="title">语文</div>
        <div class="title">数学</div>
    </div>
</body>
</html>

页面效果:

简单交互:

鼠标点到对于按钮会改变颜色(字体和背景)以及按钮的形状,鼠标的样式

导航效果2:

修改一部分代码即可(去掉title函数里面的方框)

 .title:hover{
        /* 箭头变手指 */
        cursor: pointer;
        border-bottom: 6px solid gray;
    }
    

 底部变灰

五 脚本

小球运动:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box {
            width: 400px;
            height: 400px;
            border: 1px solid;
        }
        .ball {
            width: 80px;
            height: 80px;
            background-color: red;
            border-radius: 50%;
        }
    </style>
</head>
<body>
    <div class="box" id="box">
        <div class="ball" id="ball"></div>
    </div>

    <script>
        // 拿到要处理的对象
        let box = document.getElementById("box")
        let ball = document.getElementById("ball")
        
        // 获取对象的宽高
        let box_w = box.clientWidth
        let box_h = box.clientHeight
        let ball_w = ball.clientWidth
        let ball_h = ball.clientHeight

        // 对样式的修改
        box.style.position = "relative"
        ball.style.position = "absolute"

        // 时间循环方法  1秒=1000毫秒
        let left_pos = 0, top_pos = 0
        let x_speed = 5, y_speed = 4

        let colors = ["red", "blue", "yellow", "green"]

        setInterval(function() {
            ball.style.left = left_pos + "px"
            ball.style.top = top_pos + "px"

            let color = Math.floor(Math.random()*4)

            if (left_pos >= box_w - ball_w || left_pos < 0) {
                x_speed *= -1
                ball.style.backgroundColor = colors[color]
            }
            if (top_pos >= box_h - ball_h || top_pos < 0) {
                y_speed *= -1
                ball.style.backgroundColor = colors[color]
            }

            left_pos += x_speed
            top_pos += y_speed
        }, 30)
    </script>

</body>
</html>

效果图:

能在设置的框内运动且会随着运动切换颜色

  • 1
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

绝迹刻本

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值