一组用于您页面中的流畅动画。
将 .uk-animation-*
类之一添加到任何元素。当添加类时会显示动画,通常是在页面加载时立即显示。要在其他时间点显示动画,例如当元素进入视口时,您需要使用 JavaScript 添加该类 — 例如,使用 滚动监听组件。这是 UIkit 中许多使用动画的组件中所发生的情况。所有动画本身都是使用 CSS 实现的,因此它们不需要 JavaScript 即可播放。
类 | 描述 |
---|---|
.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>
淡入
放大
缩小
抖动
<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>
淡入
放大
缩小
抖动
<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>
淡入
<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>
右下
顶部中心
底部中心
<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>
要添加简单的肯·伯恩斯效果,请将 .uk-animation-kenburns
类添加到任何图像。您还可以应用 .uk-animation-reverse
或 实用工具组件 中的 .uk-transform-origin-*
类之一来修改效果。
<img class="uk-animation-kenburns" src="" width="" height="" alt="">
默认情况下,动画在页面加载时开始。 在此示例中,我们使用了滚动监听 组件,以在图像进入视图时切换效果。
<div class="uk-child-width-1-2@s uk-grid-small" uk-grid>
<div>
<div class="uk-overflow-hidden">
<img src="images/dark.jpg" width="1800" height="1200" alt="Example image" uk-scrollspy="cls: uk-animation-kenburns; repeat: true">
</div>
</div>
<div>
<div class="uk-overflow-hidden">
<img src="images/dark.jpg" width="1800" height="1200" 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>
更简单的方法是使用 SVG 组件,方法是将 uk-svg="stroke-animation: true"
添加到图像元素。 它将计算笔划长度并自动添加 --uk-animation-stroke
自定义属性。
<img src="" width="" height="" alt="" 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" src="images/strokes.svg" width="400" height="400" alt="" uk-svg="stroke-animation: true">
</div>
<div class="uk-animation-toggle" tabindex="0">
<img class="uk-animation-stroke uk-animation-reverse" src="images/strokes.svg" width="400" height="400" alt="" uk-svg="stroke-animation: true">
</div>
</div>