成都创新互联网站制作重庆分公司

css3如何实现多个元素依次显示效果

这篇文章主要介绍“css3如何实现多个元素依次显示效果”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“css3如何实现多个元素依次显示效果”文章能帮助大家解决问题。

创新互联建站主要从事成都做网站、成都网站制作、网页设计、企业做网站、公司建网站等业务。立足成都服务中阳,10多年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18982081108

css3如何实现多个元素依次显示效果

如上图所示,在许多的活动宣传html5中会经常需要用到这样的一个动画效果。特别是快到年底了,也许有同学正在为了公司的活动页面而忙碌,get到这样一个小技能说不定刚好对你有帮助哦。

在css3中,我们使用animation与keyframes结合,可以给元素添加各种各样的动画效果。具体的动画,在keyframes中定义,在animation中使用。例如可以定义一个从上飞入的动画效果。

@keyframes topIn {
  from { transform: translateY(-50px) }
  to { transform: translateY(0px) }
}

并在目标元素中通过animation来使用动画。

.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完整代码如下:


  
  show   hide
.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 NXKrPg by Ormie (@yangbo5207) on CodePen.

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 mpbEEE by Ormie (@yangbo5207) on CodePen.

codepen demo 地址

完整代码如下:


  
      春晓
      春眠不觉晓
      处处蚊子咬
      夜来风雨声
      <此处请留下你们的才华>
  
  show
  hide

.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 < 10000) {
    styleSheet.insertRule(`.animated .delay${delay}{ animation-delay: ${delay}ms; animation-fill-mode: backwards; }`, styleSheet.cssRules.length);
    delay += delay < 3000 ? 100 : 1000;
}
function getSheet() {
    var sheets = document.styleSheets;
    var len = sheets.length;
    for(var i = 0; i <= len; i++) {
        var sheet = sheets.item(i);
        try {
            if (sheet.cssRules) {
                return sheet;
            }
        } catch(e) {} 
    }
    var style = document.createElement('style');
    style.type = "text/css";
    document.getElementsByTagName('head')[0].appendChild(style);
    return style.sheet;
}

关于“css3如何实现多个元素依次显示效果”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注创新互联行业资讯频道,小编每天都会为大家更新不同的知识点。


分享文章:css3如何实现多个元素依次显示效果
分享路径:http://cxhlcq.com/article/pdgjgi.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部