自定义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中进行切换,如下所示

相关推荐
paopaokaka_luck9 分钟前
基于springboot3+vue3的车间生产管理系统(Echarts图形化分析、BI报表)
java·前端·spring boot·学习·echarts
程序员鱼皮14 分钟前
3 大 DeepSeek Harness 进阶玩法,招多个大肥鱼帮我干活!
前端·后端·ai编程
KoPa18 分钟前
HeySmart:大模型开源网关基座-请求生命周期与钩子引擎
前端·后端
七牛开发者38 分钟前
Coding Agent 如何跑稳长任务?从上下文管理到运行时状态
前端·javascript·后端
半个落月1 小时前
从 CSR 到 Server Component:吃透 Next.js 16 App Router 路由、布局与 SEO
前端·next.js
七牛开发者1 小时前
拆解 DeepSeek Harness:Profile 与 Bundle 如何装配运行时
前端·javascript·后端
葡萄城技术团队1 小时前
表格智能体系列 · 1:一句话怎么变成一次表格操作
前端
梦曦i1 小时前
Vue-Router 2.4.0 新增可控重定向功能
前端·uni-app
deli0070071 小时前
Markdown 实时预览器:左边写右边看,排版效率翻倍
前端·ai编程
郑州光合科技余经理2 小时前
海外版外卖系统架构:订单怎么流转、权限怎么分
java·开发语言·前端·系统架构·uni-app·php·ai编程