You need to enable JavaScript to run this app. CSS动画中attr()绑定JS传入变量失效,元素无法执行弹跳动画
优惠活动
大模型
产品
解决方案
定价
更多

CSS动画中attr()绑定JS传入变量失效,元素无法执行弹跳动画

故障根因

CSS 原生attr()函数目前仅支持在content属性中读取DOM自定义属性值,完全不支持在@keyframes规则、calc()计算逻辑中读取DOM属性值并参与尺寸计算,所有正式版浏览器都未落地该实验特性,这是导致白色方块动画失效的核心原因。
之前的实现存在两个逻辑偏差:

  • JS端给元素设置的实体width/height属性,本身就不在CSS可读取的属性范畴内,就算attr()支持动态传值,也无法从这类属性中获取有效尺寸值
  • 错误依赖未被浏览器支持的实验语法写动画规则,会导致整个@keyframes块被CSS解析器丢弃,连带方块的基础定位样式也会失效
修复实现方案
  • 调整JS传值逻辑:将实时获取的窗口尺寸、文本区块位置尺寸,写入CSS自定义属性(以--开头的变量),供CSS渲染层读取
  • 替换动画的定位计算方式:完全去掉attr()调用,改用CSS自定义属性计算方块的固定位置,弹跳动画仅通过transform: translateY实现纵向位移,避免触发重排
  • 保留题目要求的核心逻辑:使用setInterval每秒执行一次尺寸同步回调
完整可运行代码

HTML 文件

<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <title>自定义CSS动画测试</title>
  <link rel="stylesheet" href="./style.css">
</head>
<body>
  <!-- 文本展示区块 -->
  <div class="main-content">
    <h1>主内容标题</h1>
    <p>这是一段占位段落文本,白色动画方块会固定在该文本区块的右侧,循环做上下弹跳效果,窗口尺寸变化时方块位置会自动同步调整。</p>
  </div>
  <!-- 待动画白色方块 -->
  <div id="bounceLeft"></div>

  <script src="./main.js"></script>
</body>
</html>

JS 文件(main.js)

const mainContent = document.querySelector('.main-content');

// 实时同步尺寸到CSS自定义属性
function syncSize() {
  // 获取窗口实时尺寸
  const winWidth = window.innerWidth;
  const winHeight = window.innerHeight;
  // 写入根节点CSS变量,全局可读取
  document.documentElement.style.setProperty('--win-width', `${winWidth}px`);
  document.documentElement.style.setProperty('--win-height', `${winHeight}px`);
  // 获取文本区块的实际位置尺寸
  const contentRect = mainContent.getBoundingClientRect();
  document.documentElement.style.setProperty('--content-left', `${contentRect.left}px`);
  document.documentElement.style.setProperty('--content-top', `${contentRect.top}px`);
  document.documentElement.style.setProperty('--content-width', `${contentRect.width}px`);
}

// 初始执行一次
syncSize();
// 按要求每秒执行一次回调同步尺寸
setInterval(syncSize, 1000);

CSS 文件(style.css)

/* 全局样式重置 */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

/* 文本区块布局 */
.main-content {
  width: 70%;
  margin: 3rem auto;
  padding: 2rem;
  line-height: 1.8;
}

.main-content h1 {
  margin-bottom: 1.5rem;
}

/* 白色动画方块基础样式 */
#bounceLeft {
  position: fixed;
  /* 用CSS变量计算固定位置:文本区块右侧留2rem间距 */
  left: calc(var(--content-left, 100px) + var(--content-width, 600px) + 2rem);
  top: calc(var(--content-top, 100px) + 1rem);
  width: 40px;
  height: 40px;
  background: #ffffff;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  /* 绑定弹跳动画 */
  animation: bounceLeft 1s ease-in-out infinite alternate;
}

/* 背景色循环渐变动画 */
@keyframes colChange {
  0% { background-color: #eef2ff; }
  50% { background-color: #fdf2f8; }
  100% { background-color: #ecfdf5; }
}
body {
  min-height: 100vh;
  animation: colChange 8s linear infinite;
}

/* 方块上下弹跳动画 */
@keyframes bounceLeft {
  from {
    transform: translateY(0);
  }
  to {
    transform: translateY(90px);
  }
}

注意:如果后续要优化性能,可以把每秒轮询的setInterval替换为window.addEventListener('resize', syncSize)事件监听,减少不必要的循环执行,上述代码保留了题目要求的每秒执行的逻辑。

内容的提问来源于stack exchange,提问作者cabbage dude

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.03 16:54:03