自定义CSS属性(@property)解决自定义CSS变量无法实现过渡效果的问题

且看下面的代码:

html 复制代码
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>demot</title>
  </head>
  <body>
    <div class="demoBox"></div>
  </body>
  <style>
    :root {
      --height: 100px;
    }
    @property --width {
      syntax: "<length>";
      inherits: false;
      initial-value: 100px;
    }
    .demoBox {
      width: var(--width);
      height: var(--height);
      background-image: linear-gradient(45deg, red, blue);
      animation: run 2s infinite alternate;
      transition: all 1s;
    }
    @keyframes run {
      from {
        --width: 100px;
        --height: 100px;
      }
      to {
        --width: 200px;
        --height: 200px;
      }
    }
  </style>
</html>

在上面的代码中,通过自定义属性的--width能够在100px到200px中实现过渡效果;自定义变量--height只能在100px与200px中进行切换,如下所示

相关推荐
墨渊君3 分钟前
React Native 跨平台组件库实践: GlueStack UI 上手指南
前端
晓得迷路了10 分钟前
栗子前端技术周刊第 84 期 - Vite v7.0 beta、Vitest 3.2、Astro 5.9...
前端·javascript·vite
独立开阀者_FwtCoder13 分钟前
最全301/302重定向指南:从SEO到实战,一篇就够了
前端·javascript·vue.js
Moment22 分钟前
给大家推荐一个超好用的 Marsview 低代码平台 🤩🤩🤩
前端·javascript·github
小满zs26 分钟前
Zustand 第三章(状态简化)
前端·react.js
普宁彭于晏28 分钟前
元素水平垂直居中的方法
前端·css·笔记·css3
恋猫de小郭39 分钟前
为什么跨平台框架可以适配鸿蒙,它们的技术原理是什么?
android·前端·flutter
云浪43 分钟前
元素变形记:CSS 缩放函数全指南
前端·css
明似水1 小时前
用 Melos 解决 Flutter Monorepo 的依赖冲突:一个真实案例
前端·javascript·flutter
独立开阀者_FwtCoder1 小时前
stagewise:让AI与代码编辑器无缝连接
前端·javascript·github