一些可以流畅运行在你的页面中的简单动画。.
添加一个 .uk-animation-*
类之一添加到任何元素。 class被添加时就会显示动画,所以在页面加载之后就会立即生效。
要使动画在另一个时间点才显示,比如在元素进入视口时,你需要添加使用了 JavaScript 的class,例如 滚动监听组件
这是 UIkit 的多数组件使用动画的方式。所有动画本身都是用 CSS 实现的,不需要使用 JavaScript来播放。
Class | Description |
---|---|
.uk-animation-fade |
元素淡入 |
.uk-animation-scale-up .uk-animation-scale-down
|
元素逐渐淡入并按比例放大或缩小。 |
.uk-animation-slide-top .uk-animation-slide-bottom
.uk-animation-slide-left .uk-animation-slide-right
|
元素以其自身的高度或宽度从顶部,底部,左侧或右侧淡入并滑入。 |
.uk-animation-slide-top-small .uk-animation-slide-bottom-small
.uk-animation-slide-left-small .uk-animation-slide-right-small
|
元素从顶部、底部、左侧或右侧淡入并滑入,并带有固定像素值的较小距离。 |
.uk-animation-slide-top-medium .uk-animation-slide-bottom-medium
.uk-animation-slide-left-medium .uk-animation-slide-right-medium
|
元素从顶部、底部、左侧或右侧淡入并滑入,并带有固定像素值的中等距离。 |
.uk-animation-kenburns |
元素会非常缓慢地向上缩放,并且不会消失。 |
.uk-animation-shake |
元素抖动 |
.uk-animation-stroke |
绘制SVG元素笔划。 |
要在鼠标悬停或处于焦点时切换动画,请将 .uk-animation-toggle
类添加到父元素。还可以添加
tabindex="0"
以使动画可通过键盘导航和触摸设备使动画聚焦。
<div class="uk-animation-toggle" tabindex="0">
<div class="uk-animation-fade"></div>
</div>
Fade
Scale Up
Scale Down
Shake
<div class="uk-child-width-1-2 uk-child-width-1-4@s uk-grid-match" uk-grid>
<div class="uk-animation-toggle" tabindex="0">
<div class="uk-card uk-card-default uk-card-body uk-animation-fade">
<p class="uk-text-center">Fade</p>
</div>
</div>
<div class="uk-animation-toggle" tabindex="0">
<div class="uk-card uk-card-default uk-card-body uk-animation-scale-up">
<p class="uk-text-center">Scale Up</p>
</div>
</div>
<div class="uk-animation-toggle" tabindex="0">
<div class="uk-card uk-card-default uk-card-body uk-animation-scale-down">
<p class="uk-text-center">Scale Down</p>
</div>
</div>
<div class="uk-animation-toggle" tabindex="0">
<div class="uk-card uk-card-default uk-card-body uk-animation-shake">
<p class="uk-text-center">Shake</p>
</div>
</div>
<div class="uk-animation-toggle" tabindex="0">
<div class="uk-card uk-card-default uk-card-body uk-animation-slide-left">
<p class="uk-text-center">Left</p>
</div>
</div>
<div class="uk-animation-toggle" tabindex="0">
<div class="uk-card uk-card-default uk-card-body uk-animation-slide-top">
<p class="uk-text-center">Top</p>
</div>
</div>
<div class="uk-animation-toggle" tabindex="0">
<div class="uk-card uk-card-default uk-card-body uk-animation-slide-bottom">
<p class="uk-text-center">Bottom</p>
</div>
</div>
<div class="uk-animation-toggle" tabindex="0">
<div class="uk-card uk-card-default uk-card-body uk-animation-slide-right">
<p class="uk-text-center">Right</p>
</div>
</div>
<div class="uk-animation-toggle" tabindex="0">
<div class="uk-card uk-card-default uk-card-body uk-animation-slide-left-small">
<p class="uk-text-center">Left Small</p>
</div>
</div>
<div class="uk-animation-toggle" tabindex="0">
<div class="uk-card uk-card-default uk-card-body uk-animation-slide-top-small">
<p class="uk-text-center">Top Small</p>
</div>
</div>
<div class="uk-animation-toggle" tabindex="0">
<div class="uk-card uk-card-default uk-card-body uk-animation-slide-bottom-small">
<p class="uk-text-center">Bottom Small</p>
</div>
</div>
<div class="uk-animation-toggle" tabindex="0">
<div class="uk-card uk-card-default uk-card-body uk-animation-slide-right-small">
<p class="uk-text-center">Right Small</p>
</div>
</div>
<div class="uk-animation-toggle" tabindex="0">
<div class="uk-card uk-card-default uk-card-body uk-animation-slide-left-medium">
<p class="uk-text-center">Left Medium</p>
</div>
</div>
<div class="uk-animation-toggle" tabindex="0">
<div class="uk-card uk-card-default uk-card-body uk-animation-slide-top-medium">
<p class="uk-text-center">Top Medium</p>
</div>
</div>
<div class="uk-animation-toggle" tabindex="0">
<div class="uk-card uk-card-default uk-card-body uk-animation-slide-bottom-medium">
<p class="uk-text-center">Bottom Medium</p>
</div>
</div>
<div class="uk-animation-toggle" tabindex="0">
<div class="uk-card uk-card-default uk-card-body uk-animation-slide-right-medium">
<p class="uk-text-center">Right Medium</p>
</div>
</div>
</div>
默认地,所有动画都是元素出现的效果。要把动画反向,请添加 .uk-animation-reverse
类.
<div class="uk-animation-fade uk-animation-reverse"></div>
Fade
Scale Up
Scale Down
Shake
<div class="uk-child-width-1-2 uk-child-width-1-4@s uk-grid-match" uk-grid>
<div class="uk-animation-toggle" tabindex="0">
<div class="uk-card uk-card-default uk-card-body uk-animation-fade uk-animation-reverse">
<p class="uk-text-center">Fade</p>
</div>
</div>
<div class="uk-animation-toggle" tabindex="0">
<div class="uk-card uk-card-default uk-card-body uk-animation-scale-up uk-animation-reverse">
<p class="uk-text-center">Scale Up</p>
</div>
</div>
<div class="uk-animation-toggle" tabindex="0">
<div class="uk-card uk-card-default uk-card-body uk-animation-scale-down uk-animation-reverse">
<p class="uk-text-center">Scale Down</p>
</div>
</div>
<div class="uk-animation-toggle" tabindex="0">
<div class="uk-card uk-card-default uk-card-body uk-animation-shake uk-animation-reverse">
<p class="uk-text-center">Shake</p>
</div>
</div>
<div class="uk-animation-toggle" tabindex="0">
<div class="uk-card uk-card-default uk-card-body uk-animation-slide-left uk-animation-reverse">
<p class="uk-text-center">Left</p>
</div>
</div>
<div class="uk-animation-toggle" tabindex="0">
<div class="uk-card uk-card-default uk-card-body uk-animation-slide-top uk-animation-reverse">
<p class="uk-text-center">Top</p>
</div>
</div>
<div class="uk-animation-toggle" tabindex="0">
<div class="uk-card uk-card-default uk-card-body uk-animation-slide-bottom uk-animation-reverse">
<p class="uk-text-center">Bottom</p>
</div>
</div>
<div class="uk-animation-toggle" tabindex="0">
<div class="uk-card uk-card-default uk-card-body uk-animation-slide-right uk-animation-reverse">
<p class="uk-text-center">Right</p>
</div>
</div>
<div class="uk-animation-toggle" tabindex="0">
<div class="uk-card uk-card-default uk-card-body uk-animation-slide-left-small uk-animation-reverse">
<p class="uk-text-center">Left Small</p>
</div>
</div>
<div class="uk-animation-toggle" tabindex="0">
<div class="uk-card uk-card-default uk-card-body uk-animation-slide-top-small uk-animation-reverse">
<p class="uk-text-center">Top Small</p>
</div>
</div>
<div class="uk-animation-toggle" tabindex="0">
<div class="uk-card uk-card-default uk-card-body uk-animation-slide-bottom-small uk-animation-reverse">
<p class="uk-text-center">Bottom Small</p>
</div>
</div>
<div class="uk-animation-toggle" tabindex="0">
<div class="uk-card uk-card-default uk-card-body uk-animation-slide-right-small uk-animation-reverse">
<p class="uk-text-center">Right Small</p>
</div>
</div>
<div class="uk-animation-toggle" tabindex="0">
<div class="uk-card uk-card-default uk-card-body uk-animation-slide-left-medium uk-animation-reverse">
<p class="uk-text-center">Left Medium</p>
</div>
</div>
<div class="uk-animation-toggle" tabindex="0">
<div class="uk-card uk-card-default uk-card-body uk-animation-slide-top-medium uk-animation-reverse">
<p class="uk-text-center">Top Medium</p>
</div>
</div>
<div class="uk-animation-toggle" tabindex="0">
<div class="uk-card uk-card-default uk-card-body uk-animation-slide-bottom-medium uk-animation-reverse">
<p class="uk-text-center">Bottom Medium</p>
</div>
</div>
<div class="uk-animation-toggle" tabindex="0">
<div class="uk-card uk-card-default uk-card-body uk-animation-slide-right-medium uk-animation-reverse">
<p class="uk-text-center">Right Medium</p>
</div>
</div>
</div>
要以更快的速度播放动画,请将 .uk-animation-fast
类添加到元素。
<div class="uk-animation-fade uk-animation-fast"></div>
Fade
<div class="uk-width-1-3@s">
<div class="uk-animation-toggle" tabindex="0">
<div class="uk-card uk-card-default uk-card-body uk-animation-fast uk-animation-fade">
<p class="uk-text-center">Fade</p>
</div>
</div>
</div>
默认情况下,缩放动画从中心开始。 若要修改此行为,请从实用效果组件中添加
.uk-transform-origin-*
类之一。
<div class="uk-animation-scale-up uk-transform-origin-bottom-right"></div>
Bottom Right
Top Center
Bottom Center
<div class="uk-child-width-1-3@s" uk-grid>
<div class="uk-animation-toggle" tabindex="0">
<div class="uk-card uk-card-default uk-card-body uk-animation-scale-up uk-transform-origin-bottom-right">
<p class="uk-text-center">Bottom Right</p>
</div>
</div>
<div class="uk-animation-toggle" tabindex="0">
<div class="uk-card uk-card-default uk-card-body uk-animation-scale-up uk-transform-origin-top-center">
<p class="uk-text-center">Top Center</p>
</div>
</div>
<div class="uk-animation-toggle" tabindex="0">
<div class="uk-card uk-card-default uk-card-body uk-animation-scale-up uk-transform-origin-bottom-center">
<p class="uk-text-center">Bottom Center</p>
</div>
</div>
</div>
要添加简单的Ken Burns效果,请将 .uk-animation-kenburns
类添加到图像。 您还可以使用 实用效果组件中的 .uk-animation-reverse
或
.uk-transform-origin-*
其中之一来修改效果。
<img class="uk-animation-kenburns" src="" alt="">
默认情况下,动画在页面加载后就开始播放。 在此示例中,我们使用滚动监听 组件,这样图片在进入视口时才触发动画效果。
<div class="uk-child-width-1-2@s uk-grid-small" uk-grid>
<div>
<div class="uk-overflow-hidden">
<img src="/skin/ukv3/images/dark.jpg" alt="Example image" uk-scrollspy="cls: uk-animation-kenburns; repeat: true">
</div>
</div>
<div>
<div class="uk-overflow-hidden">
<img src="/skin/ukv3/images/dark.jpg" alt="Example image" class="uk-animation-reverse uk-transform-origin-top-right" uk-scrollspy="cls: uk-animation-kenburns; repeat: true">
</div>
</div>
</div>
动画组件可用于为SVG笔画设置动画。 效果看起来像SVG在您的眼前绘制笔划。 SVG图像必须作为嵌入式SVG注入到标记中。 这可以手动完成,也可以使用 SVG 组件完成。
以下示例显示了如何手动添加嵌入式SVG。由于您必须知道笔触的确切长度,因此UIkit要求您在自定义属性 --uk-animation-stroke
中设置长度。
在本例中,笔划长度为46
。
<svg class="uk-animation-stroke" style="--uk-animation-stroke: 46;" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg">
<path fill="none" stroke="#000" stroke-width="1" d=""/>
</svg>
还有一种更为简单的方法是将uk-svg="stroke-animation: true"
添加到图像元素来使用 SVG 组件。 它将计算笔划长度并自动添加 --uk-animation-stroke
自定义属性。
<img src="" uk-svg="stroke-animation: true">
<div class="uk-child-width-1-2@m uk-text-center" uk-grid>
<div class="uk-animation-toggle" tabindex="0">
<img class="uk-animation-stroke" width="400" height="400" src="images/strokes.svg" alt="" uk-svg="stroke-animation: true">
</div>
<div class="uk-animation-toggle" tabindex="0">
<img class="uk-animation-stroke uk-animation-reverse" width="400" height="400" src="images/strokes.svg" alt="" uk-svg="stroke-animation: true">
</div>
</div>