Html5代码实现动态三角形

以下是一个简单的动态圣诞树的HTML5代码示例:



<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>快速下降的六边形</title>
    <style>
        body {
      
      
            background-color: #1d1d1d;
            color: #fff;
            font-family: Arial, sans-serif;
            text-align: center;
        }
        .tree {
      
      
            width: 400px;
            margin: 0 auto;
            height: 400px;
            position: relative;
            overflow: hidden;
            background-size: cover;
        }
        .star {
      
      
            position: absolute;
            top: 50px;
            left: 50%;
            margin-left: -20px;
            width: 0;
            height: 0;
            border-left: 20px solid transparent;
            border-right: 20px solid transparent;
            border-bottom: 30px solid #f1c40f;
            transform: rotate(45deg);
            animation: blink .5s infinite alternate;
        }
        @keyframes blink {
      
      
            from {
      
      
                opacity: 1;
            }
            to {
      
      
                opacity: 0.5;
            }
        }
        .tree > div {
      
      
            position: absolute;
            bottom: 0;
            width: 100%;
            height: 0;
            transform-origin: bottom center;
            animation: grow 1s forwards;
        }
        .tree > div:nth-child(1) {
      
      
            animation-delay: 0s;
            background-color: #f00;
            transform: scaleY(0);
        }
        .tree > div:nth-child(2) {
      
      
            animation-delay: 0.1s;
            background-color: #0f0;
            transform: scaleY(0);
        }
        .tree > div:nth-child(3) {
      
      
            animation-delay: 0.2s;
            background-color: #00f;
            transform: scaleY(0);
        }
        .tree > div:nth-child(4) {
      
      
            animation-delay: 0.3s;
            background-color: #f1c40f;
            transform: scaleY(0);
        }
        .tree > div:nth-child(5) {
      
      
            animation-delay: 0.4s;
            background-color: #fff;
            transform: scaleY(0);
        }
        .tree > div:nth-child(6) {
      
      
            animation-delay: 0.5s;
            background-color: #f00;
            transform: scaleY(0);
        }
        .tree > div:nth-child(7) {
      
      
            animation-delay: 0.6s;
            background-color: #0f0;
            transform: scaleY(0);
        }
        .tree > div:nth-child(8) {
      
      
            animation-delay: 0.7s;
            background-color: #00f;
            transform: scaleY(0);
        }
        .tree > div:nth-child(9) {
      
      
            animation-delay: 0.8s;
            background-color: #f1c40f;
            transform: scaleY(0);
        }
        .tree > div:nth-child(10) {
      
      
            animation-delay: 0.9s;
            background-color: #fff;
            transform: scaleY(0);
        }
        @keyframes grow {
      
      
            to {
      
      
                height: 100%;
            }
        }
        @keyframes shake {
      
      
            0%, 100% {
      
      
                transform: rotateZ(0deg);
            }
            10%, 30%, 50%, 70%, 90% {
      
      
                transform: rotateZ(5deg);
            }
            20%, 40%, 60%, 80% {
      
      
                transform: rotateZ(-5deg);
            }
        }
        .tree:hover > div:nth-child(4) {
      
      
            animation: shake .5s infinite;
        }
    </style>
</head>
<body>
    <div class="tree">
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div class="star"></div>
    </div>
</body>
</html>

这个动态三角形主要由一个包含10个绿色三角形的div组成,每个三角形的高度逐渐增加,从而形成了一个三角形的形状。同时,在三角形顶部添加一个闪烁的黄色五角星,让整个三角形更加生动。
在CSS中,使用了animation和keyframes属性来实现动态效果。通过设置animation-delay属性,可以让每个三角形依次生长,从而形成一个逐渐增高的三角形。同时,通过:hover属性和animation属性,可以让三角形在鼠标悬停时摇晃,增加趣味性和互动性。
总的来说,这个动态三角形的设计方法和特点是:使用CSS的animation和keyframes属性来实现动态效果,同时通过:hover属性和animation属性增加互动性。此外,使用了背景图片和颜色来增加视觉效果,让整个三角形更加立体和生动。

猜你喜欢

转载自blog.csdn.net/dica54dica/article/details/129775383