触发 CSS 动画和过渡
重要提示:此文档不适用于您当前选择的格式电子邮件!
CSS 动画使 Web 元素能够从一种 CSS 样式配置过渡到另一种。浏览器可以在加载时启动定义的动画,但是事件触发的 CSS 动画依赖于添加和删除类。AMP 支持这两种动画类型。
当您有一个不需要精确计时的较小、包含的动画时,请使用 CSS。
定义 CSS 和关键帧
您可以通过以下方式在 AMP 中定义 CSS
在样式和布局中阅读更多关于在 AMP 中使用 CSS 的信息。
<style amp-custom>
div {
width: 100px;
height: 100px;
background: red;
position: relative;
animation: mymove 5s infinite;
}
</style>
</head>
<body>
<div></div>
<style amp-keyframes>
@keyframes mymove {
0% {transform: translatey(0px);}
25% {transform: translatey(200px);}
75% {transform: translatey(50px);}
100% {transform: translatey(100px);}
}
</style>
</body>
添加、删除和切换类
AMP 操作 toggleClass
允许向定义的元素添加和删除类。
elementName.toggleClass(class="className")
您可以在您希望用户与之交互的同一元素上切换类,例如动画汉堡菜单。
<div id="hamburger" tabindex=1 role=button on="tap:hamburger.toggleClass(class='close')">
toggleClass
操作也可以应用于其他元素,并通过添加 force
属性在两个类之间切换。
<button on="tap:magicBox.toggleClass(class='invisible', force=true),magicBox.toggleClass(class='visible', force=false)">
Disappear
</button>
<button on="tap:magicBox.toggleClass(class='visible', force=true),magicBox.toggleClass(class='invisible', force=false)">
Reappear
</button>
如果您需要删除一个类并且不允许重新应用,请添加值为 false
的 force
属性。如果您需要添加一个类并且不允许删除,请添加值为 true
的 force
。
使用 CSS 和状态制作动画
您可以使用amp-bind
通过状态添加和删除任意数量的 CSS 类。
<head>
<script async custom-element="amp-bind" src="https://cdn.ampproject.org/v0/amp-bind-0.1.js"></script>
<style amp-custom>
div {
height: 100px;
width: 100px;
margin: 1em;
background-color: green;
margin-left: 100px;
transition: 2s;
}
.visible {
opacity: 1;
}
.invisible {
opacity: 0;
}
.left {
transform: translatex(-50px)
}
.right {
transform: translatex(50px)
}
button {
margin-top: 1rem;
margin-left: 1rem;
}
</style>
</head>
<body>
<amp-state id="magicBox">
<script type="application/json">
{
"visibleBox": {
"className": "visible"
},
"invisibleBox": {
"className": "invisible"
},
"moveLeft": {
"className": "left"
},
"moveRight": {
"className": "right"
}
}
</script>
</amp-state>
<div [class]="magicBox[animateBox].className"> </div>
<button on="tap:AMP.setState({animateBox: 'invisibleBox'})">
Disappear
</button>
<button on="tap:AMP.setState({animateBox: 'visibleBox'})">
Reappear
</button>
<button on="tap:AMP.setState({animateBox: 'moveLeft'})">
Move Left
</button>
<button on="tap:AMP.setState({animateBox: 'moveRight'})">
Move Right
</button>
</body>
首先在文档的 head
中的 <style amp-custom>
标记中添加 CSS 类列表,从而定义多个类动画
.visible {
opacity: 1;
}
.invisible {
opacity: 0;
}
.left {
transform: translatex(-50px)
}
.right {
transform: translatex(50px)
}
然后将每个类与一个状态配对
<amp-state id="magicBox">
<script type="application/json">
{
"visibleBox": {
"className": "visible"
},
"invisibleBox": {
"className": "invisible"
},
"moveLeft": {
"className": "left"
},
"moveRight": {
"className": "right"
}
}
</script>
</amp-state>
并将元素与类链接
<div [class]="magicBox[animateBox].className"> </div>
状态从链接的 AMP 操作或事件更改。以下示例从用户交互更改状态
<button on="tap:AMP.setState({animateBox: 'invisibleBox'})">
Disappear
</button>
<button on="tap:AMP.setState({animateBox: 'visibleBox'})">
Reappear
</button>
<button on="tap:AMP.setState({animateBox: 'moveLeft'})">
Move Left
</button>
<button on="tap:AMP.setState({animateBox: 'moveRight'})">
Move Right
</button>
以这种方式使用amp-bind
将类显式设置为定义的类。您不必告诉它删除其他类。