[前端] js实现酷炫倒计时动画

2033 0
Honkers 2022-10-21 15:37:21 | 显示全部楼层 |阅读模式
本文实例为大家分享了js实现酷炫倒计时动画的具体代码,供大家参考,具体内容如下
前段时间和朋友去音乐餐厅吃饭,中间有个活动,然后看到他们软件公众号H5有个活动开始的倒计时的动画效果,于是想了下实现思路。

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.   <meta charset="utf-8" />
  5.   <title>js实现酷炫倒计时动画效果</title>
  6.   <style>
  7.    *{margin:0;padding:0;}
  8.    body{width:100%;height:100%;overflow:hidden;}
  9.    .box{width:1000px;height:700px;margin:100px auto;}
  10.    .btn{width:100px;height:100px;margin:50px auto 0;font-size:16px;color:#fff;text-align:center;line-height:100px;border-radius:100px;background:#3385ff;}
  11.    .btn:hover{box-shadow: 0 0 10px #77aeff;cursor:pointer;}
  12.    h1{font-size:300px;color:red;text-align:center;}
  13.    h1.active{animation:count .5s;}
  14.    @keyframes count {
  15.     from {
  16.      transform: scale(.1);
  17.      opacity: 1;
  18.     }
  19.     to {
  20.      transform: scale(3.5);
  21.      opacity: 0;
  22.      display:none;
  23.     }
  24.    }
  25.   </style>
  26. </head>
  27. <body>
  28. <div class="btn">倒计时</div>
  29. <div class="box">
  30.   <h1 style="display:none;">10</h1>
  31. </div>
  32. </body>
  33. <script>
  34. let NUMBER = 1;
  35. let COUNT = 10;
  36. let COLORS = ['#8c00ff', '#006bff', '#4fff00', '#ffb800', '#ff0000'];
  37. let timer = null;
  38. function $(str) {
  39.   return document.querySelector(str);
  40. }
  41. function actionNum () {
  42.   let h1 = $('h1');
  43.   $('h1').style.dsplay = 'block';
  44.   timer = setInterval(() => {
  45.    COUNT--;
  46.    NUMBER++;
  47.    if (COUNT >= 0) {
  48.     h1.classList.remove('active');
  49.     setTimeout(() => {
  50.      let num = Math.floor(Math.random()*5);
  51.      h1.innerText = COUNT;
  52.      h1.style.color = COLORS[num];
  53.      h1.classList.add('active');
  54.     }, 100);
  55.    } else {
  56.     clearInterval(timer);
  57.    }
  58.   }, 1000);
  59. }
  60. $('.btn').onclick = function () {
  61.   if (COUNT < 0) {
  62.    COUNT = 11;
  63.   }
  64.   actionNum();
  65. };
  66. </script>
  67. </html>
复制代码
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持中国红客联盟。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Honkers

特级红客

关注
  • 3159
    主题
  • 36
    粉丝
  • 0
    关注
这家伙很懒,什么都没留下!

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行