利用animation.css库快速实现CSS3旋转动画效果

[复制链接]
发表于 2025-7-9 01:52:26 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

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

×
CSS3旋转动画效果实现(利用Animate.css)

下面我将展示如何利用Animate.css库快速实现各种CSS3旋转动画效果,同时提供一个直观的演示界面。
思绪分析


  • 引入Animate.css库
  • 创建不同旋转动画的展示区域
  • 添加控制面板自定义动画效果
  • 实现及时预览功能
终极实当代码

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.     <title>Animate.css旋转动画效果</title>
  7.     <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css">
  8.     <style>
  9.         * {
  10.             margin: 0;
  11.             padding: 0;
  12.             box-sizing: border-box;
  13.         }
  14.         
  15.         body {
  16.             font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
  17.             background: linear-gradient(135deg, #1a2a6c, #b21f1f, #1a2a6c);
  18.             color: #fff;
  19.             min-height: 100vh;
  20.             padding: 20px;
  21.         }
  22.         
  23.         .container {
  24.             max-width: 1200px;
  25.             margin: 0 auto;
  26.         }
  27.         
  28.         header {
  29.             text-align: center;
  30.             padding: 40px 0;
  31.             margin-bottom: 30px;
  32.         }
  33.         
  34.         h1 {
  35.             font-size: 3.5rem;
  36.             text-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
  37.             margin-bottom: 10px;
  38.             background: linear-gradient(45deg, #ff9a9e, #fad0c4, #fad0c4, #a18cd1);
  39.             -webkit-background-clip: text;
  40.             -webkit-text-fill-color: transparent;
  41.         }
  42.         
  43.         .subtitle {
  44.             font-size: 1.2rem;
  45.             opacity: 0.9;
  46.             max-width: 800px;
  47.             margin: 0 auto;
  48.             line-height: 1.6;
  49.         }
  50.         
  51.         .grid {
  52.             display: grid;
  53.             grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  54.             gap: 30px;
  55.             margin-bottom: 50px;
  56.         }
  57.         
  58.         .card {
  59.             background: rgba(255, 255, 255, 0.1);
  60.             backdrop-filter: blur(10px);
  61.             border-radius: 15px;
  62.             padding: 25px;
  63.             box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
  64.             transition: transform 0.3s ease;
  65.         }
  66.         
  67.         .card:hover {
  68.             transform: translateY(-5px);
  69.         }
  70.         
  71.         .card h2 {
  72.             font-size: 1.8rem;
  73.             margin-bottom: 20px;
  74.             color: #ffcc00;
  75.             text-align: center;
  76.         }
  77.         
  78.         .preview-area {
  79.             height: 200px;
  80.             display: flex;
  81.             justify-content: center;
  82.             align-items: center;
  83.             margin: 20px 0;
  84.         }
  85.         
  86.         .animated-element {
  87.             width: 100px;
  88.             height: 100px;
  89.             background: linear-gradient(45deg, #ff9a9e, #fad0c4, #fad0c4, #a18cd1);
  90.             border-radius: 10px;
  91.             display: flex;
  92.             justify-content: center;
  93.             align-items: center;
  94.             font-size: 3rem;
  95.             color: white;
  96.             box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
  97.         }
  98.         
  99.         .controls {
  100.             background: rgba(255, 255, 255, 0.1);
  101.             backdrop-filter: blur(10px);
  102.             border-radius: 15px;
  103.             padding: 30px;
  104.             box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
  105.             margin-bottom: 40px;
  106.         }
  107.         
  108.         .controls h2 {
  109.             font-size: 2rem;
  110.             margin-bottom: 25px;
  111.             color: #ffcc00;
  112.             text-align: center;
  113.         }
  114.         
  115.         .control-group {
  116.             display: flex;
  117.             flex-wrap: wrap;
  118.             gap: 20px;
  119.             margin-bottom: 20px;
  120.         }
  121.         
  122.         .control-item {
  123.             flex: 1;
  124.             min-width: 250px;
  125.         }
  126.         
  127.         label {
  128.             display: block;
  129.             margin-bottom: 8px;
  130.             font-weight: 500;
  131.         }
  132.         
  133.         select, input {
  134.             width: 100%;
  135.             padding: 12px;
  136.             border-radius: 8px;
  137.             border: none;
  138.             background: rgba(255, 255, 255, 0.15);
  139.             color: white;
  140.             font-size: 1rem;
  141.         }
  142.         
  143.         button {
  144.             background: linear-gradient(45deg, #ff9a9e, #a18cd1);
  145.             color: white;
  146.             border: none;
  147.             padding: 15px 30px;
  148.             font-size: 1.1rem;
  149.             border-radius: 8px;
  150.             cursor: pointer;
  151.             transition: all 0.3s ease;
  152.             display: block;
  153.             margin: 30px auto 0;
  154.             font-weight: bold;
  155.             box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
  156.         }
  157.         
  158.         button:hover {
  159.             transform: scale(1.05);
  160.             box-shadow: 0 8px 20px rgba(0, 0, 0, 0.3);
  161.         }
  162.         
  163.         .code-container {
  164.             background: rgba(0, 0, 0, 0.2);
  165.             border-radius: 10px;
  166.             padding: 20px;
  167.             font-family: monospace;
  168.             font-size: 1.1rem;
  169.             overflow-x: auto;
  170.             margin-top: 20px;
  171.         }
  172.         
  173.         .code-highlight {
  174.             color: #ffcc00;
  175.         }
  176.         
  177.         footer {
  178.             text-align: center;
  179.             padding: 30px 0;
  180.             font-size: 1rem;
  181.             opacity: 0.8;
  182.         }
  183.         
  184.         .info {
  185.             margin-top: 20px;
  186.             padding: 15px;
  187.             background: rgba(255, 204, 0, 0.15);
  188.             border-radius: 8px;
  189.             border-left: 4px solid #ffcc00;
  190.         }
  191.         
  192.         @media (max-width: 768px) {
  193.             h1 {
  194.                 font-size: 2.5rem;
  195.             }
  196.             
  197.             .grid {
  198.                 grid-template-columns: 1fr;
  199.             }
  200.         }
  201.     </style>
  202. </head>
  203. <body>
  204.     <div class="container">
  205.         <header>
  206.             <h1>Animate.css 旋转动画效果</h1>
  207.             <p class="subtitle">Animate.css 是一个现成的跨浏览器动画库,可用于您的 Web 项目。通过这个演示页面,您可以快速预览各种旋转动画效果并轻松集成到您的项目中。</p>
  208.         </header>
  209.         
  210.         <div class="controls">
  211.             <h2>自定义动画效果</h2>
  212.             <div class="control-group">
  213.                 <div class="control-item">
  214.                     <label for="animationType">选择动画效果:</label>
  215.                     <select id="animationType">
  216.                         <option value="rotateIn">旋转进入 (rotateIn)</option>
  217.                         <option value="rotateInDownLeft">左下旋转进入 (rotateInDownLeft)</option>
  218.                         <option value="rotateInDownRight">右下旋转进入 (rotateInDownRight)</option>
  219.                         <option value="rotateInUpLeft">左上旋转进入 (rotateInUpLeft)</option>
  220.                         <option value="rotateInUpRight">右上旋转进入 (rotateInUpRight)</option>
  221.                         <option value="rotateOut">旋转退出 (rotateOut)</option>
  222.                         <option value="rotateOutDownLeft">左下旋转退出 (rotateOutDownLeft)</option>
  223.                         <option value="rotateOutDownRight">右下旋转退出 (rotateOutDownRight)</option>
  224.                         <option value="rotateOutUpLeft">左上旋转退出 (rotateOutUpLeft)</option>
  225.                         <option value="rotateOutUpRight">右上旋转退出 (rotateOutUpRight)</option>
  226.                     </select>
  227.                 </div>
  228.                
  229.                 <div class="control-item">
  230.                     <label for="animationDuration">动画时长 (秒):</label>
  231.                     <input type="range" id="animationDuration" min="0.5" max="5" step="0.5" value="1">
  232.                     <span id="durationValue">1.0 秒</span>
  233.                 </div>
  234.                
  235.                 <div class="control-item">
  236.                     <label for="animationDelay">动画延迟 (秒):</label>
  237.                     <input type="range" id="animationDelay" min="0" max="3" step="0.5" value="0">
  238.                     <span id="delayValue">0.0 秒</span>
  239.                 </div>
  240.             </div>
  241.             
  242.             <div class="preview-area">
  243.                 <div id="customElement" class="animated-element">✨</div>
  244.             </div>
  245.             
  246.             <button id="applyAnimation">应用动画效果</button>
  247.             
  248.             <div class="code-container">
  249.                 <p>HTML代码:</p>
  250.                 <p>&lt;div class="animate__animated <span class="code-highlight" id="codeClass">animate__rotateIn</span>"&gt;元素内容&lt;/div&gt;</p>
  251.                 <p>CSS代码:</p>
  252.                 <p>.animate__animated {<br>
  253.                 &nbsp;&nbsp;--animate-duration: <span class="code-highlight" id="codeDuration">1s</span>;<br>
  254.                 &nbsp;&nbsp;--animate-delay: <span class="code-highlight" id="codeDelay">0s</span>;<br>
  255.                 }</p>
  256.             </div>
  257.         </div>
  258.         
  259.         <div class="grid">
  260.             <div class="card">
  261.                 <h2>旋转进入</h2>
  262.                 <div class="preview-area">
  263.                     <div class="animated-element animate__animated animate__rotateIn">➰</div>
  264.                 </div>
  265.                 <div class="info">
  266.                     <p><strong>类名:</strong> animate__rotateIn</p>
  267.                     <p>元素从透明状态旋转出现,适合元素进入场景时的动画效果。</p>
  268.                 </div>
  269.             </div>
  270.             
  271.             <div class="card">
  272.                 <h2>左下旋转进入</h2>
  273.                 <div class="preview-area">
  274.                     <div class="animated-element animate__animated animate__rotateInDownLeft">↙️</div>
  275.                 </div>
  276.                 <div class="info">
  277.                     <p><strong>类名:</strong> animate__rotateInDownLeft</p>
  278.                     <p>元素从屏幕左下方旋转进入,创造从角落进入的视觉效果。</p>
  279.                 </div>
  280.             </div>
  281.             
  282.             <div class="card">
  283.                 <h2>旋转退出</h2>
  284.                 <div class="preview-area">
  285.                     <div class="animated-element animate__animated animate__rotateOut">➿</div>
  286.                 </div>
  287.                 <div class="info">
  288.                     <p><strong>类名:</strong> animate__rotateOut</p>
  289.                     <p>元素旋转并逐渐消失,适合元素离开场景时的动画效果。</p>
  290.                 </div>
  291.             </div>
  292.             
  293.             <div class="card">
  294.                 <h2>右上旋转退出</h2>
  295.                 <div class="preview-area">
  296.                     <div class="animated-element animate__animated animate__rotateOutUpRight">↗️</div>
  297.                 </div>
  298.                 <div class="info">
  299.                     <p><strong>类名:</strong> animate__rotateOutUpRight</p>
  300.                     <p>元素向右上方旋转并消失,创造飞向屏幕右上角的效果。</p>
  301.                 </div>
  302.             </div>
  303.         </div>
  304.         
  305.         <footer>
  306.             <p>© 2023 CSS3旋转动画效果演示 | 使用Animate.css v4.1.1</p>
  307.             <p>提示:在实际项目中,您只需添加相应的类名即可应用这些动画效果</p>
  308.         </footer>
  309.     </div>
  310.     <script>
  311.         document.addEventListener('DOMContentLoaded', function() {
  312.             // 获取DOM元素
  313.             const animationType = document.getElementById('animationType');
  314.             const animationDuration = document.getElementById('animationDuration');
  315.             const animationDelay = document.getElementById('animationDelay');
  316.             const durationValue = document.getElementById('durationValue');
  317.             const delayValue = document.getElementById('delayValue');
  318.             const applyButton = document.getElementById('applyButton');
  319.             const customElement = document.getElementById('customElement');
  320.             const codeClass = document.getElementById('codeClass');
  321.             const codeDuration = document.getElementById('codeDuration');
  322.             const codeDelay = document.getElementById('codeDelay');
  323.             
  324.             // 更新时长显示
  325.             animationDuration.addEventListener('input', function() {
  326.                 durationValue.textContent = this.value + ' 秒';
  327.                 codeDuration.textContent = this.value + 's';
  328.             });
  329.             
  330.             // 更新延迟显示
  331.             animationDelay.addEventListener('input', function() {
  332.                 delayValue.textContent = this.value + ' 秒';
  333.                 codeDelay.textContent = this.value + 's';
  334.             });
  335.             
  336.             // 应用动画按钮
  337.             document.getElementById('applyAnimation').addEventListener('click', function() {
  338.                 // 移除之前的动画类
  339.                 customElement.className = 'animated-element';
  340.                
  341.                 // 添加新的动画类
  342.                 const selectedAnimation = 'animate__' + animationType.value;
  343.                 codeClass.textContent = selectedAnimation;
  344.                
  345.                 // 添加动画类
  346.                 customElement.classList.add('animate__animated', selectedAnimation);
  347.                
  348.                 // 设置自定义属性
  349.                 customElement.style.setProperty('--animate-duration', animationDuration.value + 's');
  350.                 customElement.style.setProperty('--animate-delay', animationDelay.value + 's');
  351.                
  352.                 // 重新触发动画
  353.                 void customElement.offsetWidth;
  354.             });
  355.             
  356.             // 为每个卡片添加点击预览功能
  357.             document.querySelectorAll('.card .animated-element').forEach(element => {
  358.                 element.addEventListener('click', function() {
  359.                     // 获取动画类名
  360.                     const classes = this.className.split(' ');
  361.                     const animationClass = classes.find(cls => cls.startsWith('animate__') && cls !== 'animate__animated');
  362.                     
  363.                     if (animationClass) {
  364.                         // 在控制面板中设置对应的值
  365.                         animationType.value = animationClass.replace('animate__', '');
  366.                         codeClass.textContent = animationClass;
  367.                         
  368.                         // 应用动画到自定义元素
  369.                         customElement.className = 'animated-element';
  370.                         customElement.classList.add('animate__animated', animationClass);
  371.                         
  372.                         // 显示消息
  373.                         alert(`已应用动画: ${animationClass}`);
  374.                     }
  375.                 });
  376.             });
  377.         });
  378.     </script>
  379. </body>
  380. </html>
复制代码
功能说明

这个页面展示了如何利用Animate.css实现各种CSS3旋转动画效果:

  • 预设动画展示区

    • 展示了4种不同的旋转动画效果(rotateIn, rotateInDownLeft, rotateOut, rotateOutUpRight)
    • 每个卡片包含动画预览和说明信息

  • 自定义控制面板

    • 可选择10种不同的旋转动画效果
    • 可调解动画时长(0.5秒至5秒)
    • 可设置动画延长时间(0秒至3秒)
    • 及时预览自定义动画效果

  • 代码天生

    • 表现应用动画所需的HTML和CSS代码
    • 代码会根据用户选择及时更新

  • 交互功能

    • 点击预设卡片可直接在自定义区域预览效果
    • 应用按钮可应用自定义设置

利用说明


  • 在自定义控制面板中选择动画效果
  • 调解动画时长和延长时间
  • 点击"应用动画效果"按钮检察结果
  • 复制天生的代码到您的项目中
这个页面完全响应式,可在各种设备上正常表现,并利用了当代化的UI设计,包括毛玻璃效果、渐变背景和卡片式布局。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
继续阅读请点击广告
回复

使用道具 举报

© 2001-2025 Discuz! Team. Powered by Discuz! X3.5

GMT+8, 2025-7-25 08:23 , Processed in 0.082582 second(s), 29 queries 手机版|qidao123.com技术社区-IT企服评测▪应用市场 ( 浙ICP备20004199 )|网站地图

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