react实现时钟翻牌效果

需求:随着数字的变动要求有时钟翻动动效

问题:只在加载时有动效

解决方案:通过判断数字改变(这里通过新旧数值变动来判断,不贴代码啦),每次变动的时候手动把animationIterationCount设置为infinite(持续动画),半秒后改为1(动画只执行一次),

特效参考代码

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>数字翻牌效果</title>
    <style>
        body {
            margin: 0 auto;
            padding: 0;
            text-align: center;
        }
        .container {
            position: absolute;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);
            width: 908px;
            height: 150px;
            background-color: #cc3131;
            border-bottom: 5px solid #ff8b8b;
            border-radius: 12px;
        }
        .item {
            position: relative;
            margin: 8px 5px 0;
            display: inline-block;
            width: 72px;
            height: 130px;
            /*background-color: #fe4e4e;*/
            background: -webkit-linear-gradient(#a42121, #fe4e4e, #bd2a2a); /* Safari 5.1 - 6.0 */
            background: -o-linear-gradient(#a42121, #fe4e4e, #bd2a2a); /* Opera 11.1 - 12.0 */
            background: -moz-linear-gradient(#a42121, #fe4e4e, #bd2a2a); /* Firefox 3.6 - 15 */
            background: linear-gradient(#a42121, #fe4e4e, #bd2a2a); /* 标准的语法 */
            border-radius: 12px;
        }
        .under, .on {
            position: absolute;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            line-height: 130px;
            color: #FACA38;
            font-size: 80px;
            border-radius: 12px;
            overflow: hidden;
        }
        .bottom {
            position: absolute;
            left: 0;
            top: 50%;
            width: 100%;
            height: 50%;
            line-height: 0;
            overflow: hidden;    /*不加这行就不会隐藏掉上面那半个数字部分*/
            background: -webkit-linear-gradient(#fe4e4e, #bd2a2a); /* Safari 5.1 - 6.0 */
            background: -o-linear-gradient(#fe4e4e, #bd2a2a); /* Opera 11.1 - 12.0 */
            background: -moz-linear-gradient( #fe4e4e, #bd2a2a); /* Firefox 3.6 - 15 */
            background: linear-gradient(#fe4e4e, #bd2a2a); /* 标准的语法 */
        }
        .animate {
            animation-name: flip;
            animation-duration: 1s;
            animation-direction: normal;
            animation-fill-mode: forwards;
        }
        .animate1 {
            animation-delay: .3s;
            -webkit-animation-delay: .3s;
        }
        .animate2 {
            animation-delay: .6s;
        }
        .animate3 {
            animation-delay: .9s;
        }
        .animate4 {
            animation-delay: 1.2s;
        }
        .animate5 {
            animation-delay: 1.5s;
        }
        .animate6 {
            animation-delay: 1.8s;
        }
        .animate7 {
            animation-delay: 2.1s;
        }
        .animate8 {
             animation-delay: 2.4s;
         }
        .animate9 {
            animation-delay: 2.7s;
        }
        @keyframes flip{
            from {
                transform: rotateX(180deg);
                width: 100%;
            }
            to {
                transform: rotateX(0);
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="item">
            <!--底下那层-->
            <div class="under">9</div>
            <!--上面会翻动的那层-->
            <div class="on animate animate0">
                <!--只需要下面那一半-->
                <div class="bottom">9</div>
            </div>
        </div>
        <div class="item">
            <div class="under">8</div>
            <div class="on animate animate1">
                <div class="bottom">
                    <div class="num">8</div>
                </div>
            </div>
        </div>
        <div class="item">
            <div class="under">7</div>
            <div class="on animate animate2">
                <div class="bottom">
                    <div class="num">7</div>
                </div>
            </div>
        </div>
        <div class="item">
            <div class="under">6</div>
            <div class="on animate animate3">
                <div class="bottom">
                    <div class="num">6</div>
                </div>
            </div>
        </div>
        <div class="item">
            <div class="under">5</div>
            <div class="on animate animate4">
                <div class="bottom">
                    <div class="num">5</div>
                </div>
            </div>
        </div>
        <div class="item">
            <div class="under">4</div>
            <div class="on animate animate5">
                <div class="bottom">
                    <div class="num">4</div>
                </div>
            </div>
        </div>
        <div class="item">
            <div class="under">3</div>
            <div class="on animate animate6">
                <div class="bottom">
                    <div class="num">3</div>
                </div>
            </div>
        </div>
        <div class="item">
            <div class="under">2</div>
            <div class="on animate animate7">
                <div class="bottom">
                    <div class="num">2</div>
                </div>
            </div>
        </div>
        <div class="item">
            <div class="under">1</div>
            <div class="on animate animate8">
                <div class="bottom">
                    <div class="num">1</div>
                </div>
            </div>
        </div>
        <div class="item">
            <div class="under">0</div>
            <div class="on animate animate9">
                <div class="bottom">
                    <div class="num">0</div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>
相关推荐
September_ning12 分钟前
React.lazy() 懒加载
前端·react.js·前端框架
web行路人22 分钟前
React中类组件和函数组件的理解和区别
前端·javascript·react.js·前端框架
番茄小酱00123 分钟前
Expo|ReactNative 中实现扫描二维码功能
javascript·react native·react.js
小马哥编程1 小时前
【前端基础】CSS基础
前端·css
Justinc.2 小时前
CSS3新增边框属性(五)
前端·css·css3
fruge2 小时前
纯css制作声波扩散动画、js+css3波纹催眠动画特效、【css3动画】圆波扩散效果、雷达光波效果完整代码
javascript·css·css3
As977_2 小时前
前端学习Day12 CSS盒子的定位(相对定位篇“附练习”)
前端·css·学习
susu10830189112 小时前
vue3 css的样式如果background没有,如何覆盖有background的样式
前端·css
Rattenking2 小时前
React 源码学习01 ---- React.Children.map 的实现与应用
javascript·学习·react.js
熊的猫4 小时前
JS 中的类型 & 类型判断 & 类型转换
前端·javascript·vue.js·chrome·react.js·前端框架·node.js