【web】动画 (用法+例子)

动画

属性:

  • animation-name:动画属性名
  • animation-duration:动画持续时间
  • animation-delay:动画延迟时间
  • linear:匀速运动
  • animation-timing-function:动画速度曲线
    • 值可以为:cubic-bezier(.17,.67,.7,.88)
    • https://cubic-bezier.com/#.17,.67,.7,.88可以打开网址生成
    • 可将速度变为慢-快-慢等过程
  • animation-iteration-counton:定义循环次数,infinite为无限循环
  • animation-direction:alternate:动画轮流反向播放
  • animation-play-statepaused:动画状态,值paused,running
  • animation-fill-mode:forwards保留最后一帧动画
    用法:
 <style>
        /*定义关键帧*/
        @keyframes changediv {
            from {/*动画起始状态*/
                width: 200px;
                height: 200px;
                background-color: tomato;
            }
            to {/*动画结束状态*/
                width: 300px;
                height: 300px;
                background-color: gold;
            }
        }
        div{
            width: 100px;
            height: 100px;
            background-color: tomato;
            animation: changediv 2s;/*动画名,执行时间*/
        }
    </style>
<body>
    <div></div>
</body>
轮播图案例(使用动画做)
  • 创建动画

    • left从0移动到-1200
     @keyframes move {
                from {
                    left: 0;
                }
                to {
                    left: -1200px;
                }
            }
    
    • 移动盒子引用动画

      animation: move 5s linear infinite;/*动画名,5s时间,匀速运动,无限循环*/
      
  • 从左上角为基线开始移动

    • 左上角为0,显示第一张图片
    • 左上角为400,第二张图片
    • 左上角800,显示第三张图片
    • 左上角1200,显示第四章图片
    <style>
            * {
                margin: 0;
                padding: 0;
                list-style: none;
            }
            @keyframes move {
                from {
                    left: 0;
                }
                to {
                    left: -1200px;
                }
            }
            .window {
                width: 400px;
                height: 300px;
                overflow: hidden;
                position: relative;
                box-shadow: 2px 2px 5px gray;
                margin:0 auto;
            }
            .move {
                width: 1600px;
                height: 300px;
                position: absolute;
                animation: move 5s linear infinite;
            }
            li {
                width: 400px;
                height: 300px;
                float: left;
            }
            img {
                width: 400px;
                height: 300px;
            }
        </style>
    <body>
        <div class="window">
            <ul class="move">
                <li><img src="./03.jpg" alt=""></li>
                <li><img src="./04.jpg" alt=""></li>
                <li><img src="./img1.jpg" alt=""></li>
                <li><img src="./img2.jpg" alt=""></li>
                </ul>>
        </div>
    </body>
    
  • 动画不衔接的问题

    • 把第一个图片复制一份放最后面
    • 同样,滚动距离、盒子宽度都需要修改
<style>
        * {
            margin: 0;
            padding: 0;
            list-style: none;
        }
        @keyframes move {
            from {
                left: 0;
            }
            to {
                left: -1600px;
            }
        }
        .window {
            width: 400px;
            height: 300px;
            overflow: hidden;
            position: relative;
            box-shadow: 2px 2px 5px gray;
            margin:0 auto;
        }
        .move {
            width: 2000px;
            height: 300px;
            position: absolute;
            animation: move 5s linear infinite;
        }
        li {
            width: 400px;
            height: 300px;
            float: left;
        }
        img {
            width: 400px;
            height: 300px;
        }
    </style>
<body>
    <div class="window">
        <ul class="move">
            <li><img src="./03.jpg" alt=""></li>
            <li><img src="./04.jpg" alt=""></li>
            <li><img src="./img1.jpg" alt=""></li>
            <li><img src="./img2.jpg" alt=""></li>
            <li><img src="./03.jpg" alt=""></li>
            </ul>
    </div>
  • 图片不停留问题
    • 在各时间段设置位置,这样图片就会有停留
 @keyframes move {
            0% {
                left: 0;
            }

            24% {
                left: 0;
            }

            26% {
                left: -400px;
            }

            49% {
                left: -400px;
            }

            51% {
                left: -800px;
            }

            74% {
                left: -800px;
            }

            76% {
                left: -1200px;
            }

            98% {
                left: -1200px;
            }

            100% {
                left: -1600px;
            }
        }
再加入小点

完整版

<style>
* {
            margin: 0;
            padding: 0;
            list-style: none;
        }

        @keyframes move {
            0% {
                left: 0;
            }

            24% {
                left: 0;
            }

            26% {
                left: -400px;
            }

            49% {
                left: -400px;
            }

            51% {
                left: -800px;
            }

            74% {
                left: -800px;
            }

            76% {
                left: -1200px;
            }

            98% {
                left: -1200px;
            }

            100% {
                left: -1600px;
            }
        }
        @keyframes changeColor{
            0%{background-color: cyan;}
            25% {background-color: cyan;}
            25.1% {background-color: white;}
            100% {background-color: white;}
        }
        .window {
            width: 400px;
            height: 300px;
            overflow: hidden;
            position: relative;
            box-shadow: 2px 2px 5px gray;
            margin: 0 auto;
        }

        .move {
            width: 2000px;
            height: 300px;
            position: absolute;
            animation: move 10s linear infinite;
        }

        li {
            width: 400px;
            height: 300px;
            float: left;
        }

        img {
            width: 400px;
            height: 300px;
        }
        .dots {
            width: 200px;
            height: 6px;
            /* background-color: pink; */
            position: absolute;
            left: 100px;
            bottom: 20px;
            display: flex;
            justify-content: space-evenly;
        }
        .dot {
            width: 6px;
            height: 6px;
            background-color: white;
            border-radius: 50%;
            float: left;
            animation: changeColor 8s linear infinite;
        }
    </style>
<body>
    <div class="window">
        <ul class="move">
            <li><img src="./03.jpg" alt=""></li>
            <li><img src="./04.jpg" alt=""></li>
            <li><img src="./img1.jpg" alt=""></li>
            <li><img src="./img2.jpg" alt=""></li>
            <li><img src="./03.jpg" alt=""></li>
        </ul>
        <div class="dots">
            <div class="dot" ></div>
            <div class="dot" style="animation-delay: 2s;"></div>
            <div class="dot" style="animation-delay: 4s;"></div>
            <div class="dot" style="animation-delay: 6s;"></div>
        </div>
    </div>
</body>

效果:自动切换图片,点跟着图片的切换而切换
效果

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值