在css3中,我们使用animation与keyframes结合,可以给元素添加各种各样的动画效果。这篇文章主要介绍了css3实现多个元素依次显示效果,对css3感兴趣的小伙伴和需要的朋友可以参考下

如上图所示,在许多的活动宣传html5中会经常需要用到这样的一个动画效果。特别是快到年底了,也许有同学正在为了公司的活动页面而忙碌,get到这样一个小技能说不定刚好对你有帮助哦。
在css3中,我们使用animation与keyframes结合,可以给元素添加各种各样的动画效果。具体的动画,在keyframes中定义,在animation中使用。例如可以定义一个从上飞入的动画效果。
@keyframes topIn {
from { transform: translateY(-50px) }
to { transform: translateY(0px) }
}
并在目标元素中通过animation来使用动画。
<p></p>
.topIn {
animation: topIn 1s ease;
}
这样,当元素第一次渲染进入DOM时,就会有一个从上到下的位移动画效果。当然,这种效果并不是我们想要的。往往我们还在在动画上加上一个透明度从0到1的渐变。
@keyframes topIn {
from {
transform: translateY(-50px);
opacity: 0;
}
to {
transform: translateY(0px);
opacity: 1;
}
}
我们还希望能够控制元素的显示时机应该怎么办?简单一点的办法就是在需要动画效果展示时,才给目标元素添加控制动画的class样式。
btn.addEventListener('click', function() {
document.querySelector('.target').classList.add('topIn');
}, !1);
但是这样做有一个问题。我相信实践过的朋友都已经发现过的。我们期望元素在入场之前,是处于看不见的状态。但是仅仅只是上面的做法,动画开始前元素是能够被看见的。那么应该怎么办?
我们可以很简单的想到,给元素添加 display: none 或者 visibility: hidden 。但是由于 display: none 之后,元素是不占位的。因此如果这样的话,会导致页面布局出现混乱。所以我们在开始之前,给元素添加一个新的class。
.aninode {
visibility: hidden;
}
并且添加一个新的class让元素显示出来。
.animated .aninode {
visibility: visible;
}
控制动画效果的class也在css上进行一些调整。
.animated .topIn {
animation: topIn 1s ease;
}
这样做的好处是,我们只需要在class中添加一个 animated ,就能够达到我们的效果。实例demo完整代码如下:
<p>
</p><p></p>
<button>show</button>
<button>hide</button>
.container {
width: 100px;
margin: 0 auto;
}
.aninode {
visibility: hidden;
}
.animated .aninode {
visibility: visible;
}
.target {
width: 100px;
height: 100px;
background: orange;
border-radius: 4px;
margin: 20px 0;
}
.animated .topIn {
animation: topIn 1s ease;
}
.animated .leftIn {
animation: leftIn 1s ease;
}
.btn {
width: 100px;
height: 30px;
border: 1px solid #ccc;
outline: none;
transition: 0.1s;
}
.btn:active {
border: none;
background: orange;
color: #fff;
}
@keyframes topIn {
from {
transform: translateY(-50px);
opacity: 0;
}
to {
transform: translateY(0px);
opacity: 1;
}
}
@keyframes leftIn {
from {
transform: translateX(-50px);
opacity: 0;
}
to {
transform: translateX(0px);
opacity: 1;
}
}
var show = document.querySelector('.show');
var hide = document.querySelector('.hide');
var container = document.querySelector('.container');
show.addEventListener('click', function() {
container.classList.add('animated');
}, !1);
hide.addEventListener('click', function() {
container.classList.remove('animated');
}, !1);
Demo显示如下:
See the Pen <a>NXKrPg</a> by Ormie (<a>@yangbo5207</a>) on <a>CodePen</a>.
codepen demo 地址
但是这样离我们想要的效果好像还差一点点。继续思考。首先想要后面的元素比前一个元素晚一点出现,那么肯定是要控制延迟时间,我们就必须有许多设置延迟时间的class。
.delay200 {
animation-delay: 200ms;
animation-fill-mode: backwards!important;
}
.delay400 {
animation-delay: 400ms;
animation-fill-mode: backwards!important;
}
.delay600 {
animation-delay: 600ms;
animation-fill-mode: backwards!important;
}
.delay800 {
animation-delay: 800ms;
animation-fill-mode: backwards!important;
}
animation-fill-mode: backwards!important; 的目的是为了元素在出现之前,保持透明度为0的状态。防止当添加 animated 之后元素直接出现了。
加 !important 是为了防止在新的class中使用animation简写时对 animation-fill-mode 的属性进行覆盖改写。如果此处不写 !important 的话,那么在 topIn 这样的动画class中就不能使用简写形式。
这样之后,我们只需要在css中添加上上述代码,并对html做一些改动,就能够实现我们想要的效果了。
See the Pen <a>mpbEEE</a> by Ormie (<a>@yangbo5207</a>) on <a>CodePen</a>.
codepen demo 地址
完整代码如下:
<p>
</p><p>
</p><p>春晓</p>
<p>春眠不觉晓</p>
<p>处处蚊子咬</p>
<p>夜来风雨声</p>
<p></p>
<button>show</button>
<button>hide</button>
.container {
width: 200px;
margin: 0 auto;
}
.aninode {
visibility: hidden;
}
.animated .aninode {
visibility: visible;
}
.targets {
margin: 20px 0;
}
.targets .item {
border: 1px solid #ccc;
margin: 10px 0;
line-height: 2;
padding: 2px 6px;
border-radius: 4px;
}
.animated .topIn {
animation: topIn 1s ease;
}
.animated .leftIn {
animation-name: leftIn;
animation-duration: 1s;
}
.btn {
width: 100px;
height: 30px;
border: 1px solid #ccc;
outline: none;
transition: 0.1s;
}
.btn:active {
border: none;
background: orange;
color: #fff;
}
@keyframes topIn {
from { transform: translateY(-50px) }
to { transform: translateY(0px) }
}
@keyframes leftIn {
from {
transform: translateX(-50px);
opacity: 0;
}
to {
transform: translateX(0px);
opacity: 1;
}
}
.delay200 {
animation-delay: 200ms;
animation-fill-mode: backwards!important;
}
.delay400 {
animation-delay: 400ms;
animation-fill-mode: backwards!important;
}
.delay600 {
animation-delay: 600ms;
animation-fill-mode: backwards!important;
}
.delay800 {
animation-delay: 800ms;
animation-fill-mode: backwards!important;
}
var show = document.querySelector('.show');
var hide = document.querySelector('.hide');
var container = document.querySelector('.container');
show.addEventListener('click', function() {
container.classList.add('animated');
}, !1);
hide.addEventListener('click', function() {
container.classList.remove('animated');
}, !1);
我们发现js的逻辑并没有发生任何改变。仍然仅仅只是在合适的位置添加/删除animated。
彩蛋:
在实践中我们还会遇到一个比较麻烦的事儿。就是延迟class的编写。我们可能并不知道会使用到那些时差,有多少个元素会使用到,如果都用手来写的话,重复工作确实太过麻烦。因此我们可以使用js动态插入。代码如下:
const styleSheet = getSheet(); var delay = 100; while (delay <p><span style="color: #ff0000"><strong>总结</strong></span></p><p>以上所述是小编给大家介绍的css3实现多个元素依次显示效果,希望对大家有所帮助!!</p><p><span style="font-size: 16px;"><strong>相关推荐:</strong></span></p><p><span style="font-size: 14px;"><a href="http://www.php.cn/js-tutorial-382478.html" target="_blank">CSS3中使用视窗单位来布局的方法</a><strong><a href="http://www.php.cn/js-tutorial-382478.html" target="_blank"></a><br></strong></span></p><p><a href="http://www.php.cn/css-tutorial-382470.html" target="_blank">CSS3实现逐渐发光的方格边框实例</a><br></p><p><a href="http://www.php.cn/css-tutorial-382281.html" target="_blank">纯 CSS3 效果资源收集整理</a></p>
前端入门到VUE实战笔记:立即使用
在学习笔记中,你将探索 前端 的入门与实战技巧!











