吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 4217|回复: 17
收起左侧

[其他转载] HTML CSS3+JS实现【灵动岛】效果

[复制链接]
全能小太阳 发表于 2022-9-14 14:00
主要利用CSS3-animation + JS实现效果,只是提供思路,具体细节可以参考

实现效果:


QQ录屏20220914135737.gif

css3-animation属性,可实现动画效果,如下:
[CSS] 纯文本查看 复制代码
.demo{
  animation: 动画1,动画2;
}


上面可以实现动画效果,但是不可控制,只能自动播放,于是乎看下面:


[JavaScript] 纯文本查看 复制代码
 // 灵动岛对应dom
    const box = document.querySelector(".demo");
   // 以类名定义所有动画类型,以类名切换,实现动画切换
    const animationList = ["longer", "divide", "fusion", "bigger"];
    box.addEventListener("click", () => {
      box.classList.add(animationList[index]);
    });
    let index = 0;
    // 每一个动画结束都会触发此事件(包括子元素及不同属性动画结束时)
    box.addEventListener("animationend", (e) => {
      if (
        e.animationName === "divide-right" ||
        e.animationName === "fusion-right"
      ) {
        return;
      }
      index++;
      setTimeout(() => {
        if (index <= animationList.length - 1) {
          box.classList.add(animationList[index]);
        } else {
          index = 0;
        }
      }, 800);
    });


JS控制动画的三种方式:
1.直接覆盖 animation
[CSS] 纯文本查看 复制代码
box.style.animation = `longer 800ms ease-in-out`;

如果动画效果较多,那就写的比较长了,建议封装CSS类,通过切换CSS类名实现动画效果


2.通过切换CSS类名,上面说过了,把不同动画封装在不同类里面,直接切换
[CSS] 纯文本查看 复制代码
box.classList.toggle('longer');


3.animationPlayState属性
[CSS] 纯文本查看 复制代码
box.style.animationPlayState="paused" 
// runing播放,paused暂停。

但是只支持单动画播放


动画结束如何让界面停留在结束时的状态呢?
[CSS] 纯文本查看 复制代码
animation-fill-mode:forwards;


完整代码:
[HTML] 纯文本查看 复制代码
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <title>灵动岛</title>
    <style>
      * {
        margin: 0;
        padding: 0;
      }
      #iphone14pro {
        position: relative;
        margin: auto;
        width: 974px;
        height: 876px;
        overflow: hidden;
        background-image: url([img]https://www.qcodes.cn/wp-content/uploads/2022/09/2022091405393859.png[/img]);
      }
      .demo {
        width: 320px;
        margin-top: 72px;
        margin: 72px auto 0;
        background-color: red;
        height: 80px;
        border-radius: 40px;
        background-color: #272729;
        position: relative;
      }
      .demo::after {
        position: absolute;
        content: " ";
        right: 0;
        width: 80px;
        height: 100%;
        border-radius: 80px;
        background-color: #272729;
      }
      /* 变长 */
      .longer {
        animation: longer 800ms ease-in-out forwards;
      }
      @keyframes longer {
        0% {
        }
        60% {
          width: 50vw;
        }
        80% {
          transform: scaleX(1.04);
        }
        100% {
          transform: scaleX(1);
          width: 50vw;
        }
      }
      /* 分离 */
      .divide {
        animation: divide-left 800ms ease-in-out forwards;
      }
      @keyframes divide-left {
        0% {
        }
        40% {
          transform: scaleX(1.1);
        }

        100% {
          transform: scaleX(1);
        }
      }
      .divide::after {
        animation: divide-right 800ms ease-in-out forwards;
      }
      @keyframes divide-right {
        0% {
        }
        40% {
          transform: scaleX(1.1);
        }

        100% {
          transform: scaleX(1);
          right: -100px;
        }
      }
      /* 融合 */
      .fusion {
        animation: fusion-left 800ms ease-in-out forwards;
      }
      @keyframes fusion-left {
        0% {
        }
        40% {
          transform: scaleX(1.1);
        }

        100% {
          transform: scaleX(1);
        }
      }
      .fusion::after {
        animation: fusion-right 800ms ease-in-out forwards;
      }
      @keyframes fusion-right {
        0% {
          right: -100px;
        }
        40% {
          transform: scaleX(1.1);
        }

        100% {
          transform: scaleX(1);
          right: 0;
        }
      }
      /* 变大 */
      .bigger {
        animation: bigger 800ms ease-in-out forwards;
      }
      @keyframes bigger {
        0% {
        }
        60% {
          width: 81vw;
          height: 400px;
          border-radius: 100px;
        }
        80% {
          transform: scaleX(1.04);
        }
        100% {
          width: 81vw;
          height: 400px;
          border-radius: 100px;
          transform: scaleX(1);
        }
      }
      .bigger::after {
        display: none;
      }
    </style>
  </head>
  <body>
    <div id="iphone14pro">
      <div class="demo"></div>
    </div>
    <script>
      // 灵动岛对应dom
      const box = document.querySelector(".demo");

      const animationList = ["longer", "divide", "fusion", "bigger"];
      box.addEventListener("click", () => {
        box.classList.add(animationList[index]);
      });
      let index = 0;
      // 每一个动画结束都会触发此事件(包括子元素及不同种类属性动画)
      box.addEventListener("animationend", (e) => {
        if (
          e.animationName === "divide-right" ||
          e.animationName === "fusion-right"
        ) {
          return;
        }
        index++;
        setTimeout(() => {
          if (index <= animationList.length - 1) {
            box.classList.add(animationList[index]);
          } else {
            index = 0;
          }
        }, 800);
      });
    </script>
  </body>
</html>

免费评分

参与人数 4吾爱币 +4 热心值 +3 收起 理由
我是一个外星人 + 1 + 1 谢谢@Thanks!
Fullmoonbaka + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
_paopao + 1 + 1 我很赞同!
zfc0621 + 1 + 1 我很赞同!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

Th52520 发表于 2023-2-27 14:30
[Asm] 纯文本查看 复制代码
// 灵动岛对应dom
const box = document.querySelector(".demo");

const animationList = ["longer", "divide", "fusion", "bigger"];
let index = 0;

box.addEventListener("click", () => {
    // Remove any existing animations from the class. 
    box.className = '';

    // Add the animation at the current index of the list.
    box.classList.add(animationList[index]);
});

// 每一个动画结束都会触发此事件(包括子元素及不同种类属性动画)
box.addEventListener('animationend', (e) => {
    if (e.animationName === 'divide-right' || e.animationName === 'fusion-right') {
        return;
    }
    
    // Increment the index and check if it is out of range
    index++;
    if (index > animationList.length - 1) {
        index = 0;
    }

    // Add the next animation after 800 milliseconds
    setTimeout(() => {
        box.classList.add(animationList[index]);
    }, 800);
});
Th52520 发表于 2023-2-27 14:29
楼主 你看这样行不

// 灵动岛对应dom
const box = document.querySelector(".demo");

const animationList = ["longer", "divide", "fusion", "bigger"];
let index = 0;

box.addEventListener("click", () => {
    // Remove any existing animations from the class.
    box.className = '';

    // Add the animation at the current index of the list.
    box.classList.add(animationList[index]);
});

// 每一个动画结束都会触发此事件(包括子元素及不同种类属性动画)
box.addEventListener('animationend', (e) => {
    if (e.animationName === 'divide-right' || e.animationName === 'fusion-right') {
        return;
    }
   
    // Increment the index and check if it is out of range
    index++;
    if (index > animationList.length - 1) {
        index = 0;
    }

    // Add the next animation after 800 milliseconds
    setTimeout(() => {
        box.classList.add(animationList[index]);
    }, 800);
});
cyh1119 发表于 2022-9-14 16:17
_paopao 发表于 2022-9-14 17:11
厉害  支持下
vicky526356 发表于 2022-9-14 18:09
感谢分享,必须支持一波
iwhy 发表于 2022-9-14 20:16
紧跟潮流呀
bdcpc 发表于 2022-9-15 06:12
苹果的效果,牛&#128046;
chenfy08 发表于 2022-9-15 13:58
看这个挺不错的
jiangxiaoshuai 发表于 2022-9-16 08:43
感谢分享!
WuJi001 发表于 2022-9-16 18:15
试了一下,确实不错,挺好玩的
tuqi 发表于 2022-12-17 20:28
果断试一试。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-9 21:03

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表