CSS3实现苹果灵动岛效果

主要利用了CSS3-animation + JS实现效果,只是提供思路,具体细节可以参考

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

<!DOCTYPE html>

<html>

  <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></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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

执刀人的工具库

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

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

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

打赏作者

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

抵扣说明:

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

余额充值