---
title: "CSS动画"
language: "zh"
canonical: "https://x6.antv.antgroup.com/zh/examples/animation/usage/css/"
version: "3.x"
---

# CSS动画

[运行示例](https://x6.antv.antgroup.com/zh/examples/animation/usage/css/)

[资料索引](/llms.txt)

页面需要一个 `id="container"` 的 DOM 元素。以下为原始示例源码。

## 源码

```ts
import { Graph } from '@antv/x6'
import insertCss from 'insert-css'

const graph = new Graph({
  container: document.getElementById('container'),
  grid: true,
})

graph.addNode({
  x: 40,
  y: 40,
  width: 100,
  height: 60,
  attrs: {
    body: {
      fill: '#efdbff',
      stroke: 'none',
      rx: 10,
      style: {
        // 也可以使用CSS动画，具体如何选择可参考文档中的详细说明
        animation: 'trans 1s infinite linear',
      },
    },
  },
})

graph.addNode({
  x: 200,
  y: 40,
  width: 100,
  height: 60,
  attrs: {
    body: {
      fill: '#85C054',
      stroke: 'none',
      rx: 10,
      // 也可以通过添加class来添加动画
      class: 'css-animation-node',
    },
  },
})

// 我们用 insert-css 协助demo演示
// 实际项目中只要将下面样式添加到样式文件中
insertCss(`
  @keyframes trans {
    to {
        transform: translateX(50px);
    }
  }

  .css-animation-node {
    animation: trans 1s infinite linear;
  }
`)
```
