HTML&CSS:必学!跳跃的动态标题

91 阅读3分钟

这段代码实现了一个动态的标题效果,其中每个字母依次跳动,营造出一种活泼的视觉效果。


大家复制代码时,可能会因格式转换出现错乱,导致样式失效。建议先少量复制代码进行测试,若未能解决问题,私信回复源码两字,我会发送完整的压缩包给你。

演示效果

HTML&CSS


<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>公众号关注:前端Hardy</title>
    <style>
        html,
        body {
            width: 100%;
            height: 100%;
            background: #FF7777;
            -webkit-font-smoothing: antialiased;
            display: flex;
            justify-content: center;
            align-items: center;
        }

        h1 {
            height: 100px;
        }

        h1 span {
            position: relative;
            top: 20px;
            display: inline-block;
            animation: bounce .3s ease infinite alternate;
            font-family: none;
            font-size: 80px;
            color: #FFF;
            text-shadow: 0 1px 0 #CCC,
                0 2px 0 #CCC,
                0 3px 0 #CCC,
                0 4px 0 #CCC,
                0 5px 0 #CCC,
                0 6px 0 transparent,
                0 7px 0 transparent,
                0 8px 0 transparent,
                0 9px 0 transparent,
                0 10px 10px rgba(0, 0, 0, .4);
        }

        h1 span:nth-child(2) {
            animation-delay: .1s;
        }

        h1 span:nth-child(3) {
            animation-delay: .2s;
        }

        h1 span:nth-child(4) {
            animation-delay: .3s;
        }

        h1 span:nth-child(5) {
            animation-delay: .4s;
        }

        h1 span:nth-child(6) {
            animation-delay: .5s;
        }

        h1 span:nth-child(7) {
            animation-delay: .6s;
        }

        h1 span:nth-child(8) {
            animation-delay: .7s;
        }

        @keyframes bounce {
            100% {
                top: -20px;
                text-shadow: 0 1px 0 #CCC,
                    0 2px 0 #CCC,
                    0 3px 0 #CCC,
                    0 4px 0 #CCC,
                    0 5px 0 #CCC,
                    0 6px 0 #CCC,
                    0 7px 0 #CCC,
                    0 8px 0 #CCC,
                    0 9px 0 #CCC,
                    0 50px 25px rgba(0, 0, 0, .2);
            }
        }
    </style>
</head>

<body>
    <h1>
        <span>I</span>
        <span>L</span>
        <span>O</span>
        <span>V</span>
        <span>E</span>
        <span>Y</span>
        <span>O</span>
        <span>U</span>
    </h1>
</body>

</html>

HTML 结构

  • h1:定义标题,包含多个span元素,每个span包含一个字母。
  • span:定义标题中的每个字母,通过CSS实现动态效果。

CSS 样式

  • html, body:设置页面的整体布局,背景颜色为#FF7777,并使用flex布局居中对齐内容。
  • h1:定义标题的样式,设置高度为100px。
  • h1 span:定义标题中每个字母的样式,包括相对定位、内联块显示、动画效果、字体大小、颜色和文本阴影。
  • h1 span:nth-child(2):定义第二个字母的动画延迟为.1s。
  • h1 span:nth-child(3):定义第三个字母的动画延迟为.2s。
  • h1 span:nth-child(4):定义第四个字母的动画延迟为.3s。
  • h1 span:nth-child(5):定义第五个字母的动画延迟为.4s。
  • h1 span:nth-child(6):定义第六个字母的动画延迟为.5s。
  • h1 span:nth-child(7):定义第七个字母的动画延迟为.6s。
  • h1 span:nth-child(8):定义第八个字母的动画延迟为.7s。
  • @keyframes bounce:定义跳动动画,通过改变top和text-shadow属性实现字母的上下跳动效果。

各位互联网搭子,要是这篇文章成功引起了你的注意,别犹豫,关注、点赞、评论、分享走一波,让我们把这份默契延续下去,一起在知识的海洋里乘风破浪!