【问题标题】:CSS hide element, after 5 seconds show it [duplicate]CSS隐藏元素,5秒后显示它[重复]
【发布时间】:2018-03-29 15:34:51
【问题描述】:

我发现了这个问题CSS Auto hide elements after 5 seconds

我需要知道如何做相反的事情。 基本上:

-隐藏元素的页面加载

-等待 5 秒

-显示元素

我不太擅长 CSS,所以任何帮助都会很好!

#thingtohide {
    -moz-animation: cssAnimation 0s ease-in 5s forwards;
    /* Firefox */
    -webkit-animation: cssAnimation 0s ease-in 5s forwards;
    /* Safari and Chrome */
    -o-animation: cssAnimation 0s ease-in 5s forwards;
    /* Opera */
    animation: cssAnimation 0s ease-in 5s forwards;
    -webkit-animation-fill-mode: forwards;
    animation-fill-mode: forwards;
}

@keyframes cssAnimation {
    to {
        width:0;
        height:0;
        overflow:hidden;
    }
}
@-webkit-keyframes cssAnimation {
    to {
        width:0;
        height:0;
        visibility:hidden;
    }
}

【问题讨论】:

  • 这实际上是从您链接的目标问题中交换结束状态和开始状态之间的属性的情况。
  • @MattWeber 引入一个依赖于另一种编程语言的库并不比使用 CSS 在 5 秒后显示隐藏元素更简单。
  • @TylerH 根据答案,有一种更好的方法来做相反的事情,此外,我不知道需要改变什么,因为我已经玩过它但没有成功。因此,为什么我提出了一个新问题而不是删除那个旧线程。
  • 永远不要担心发布到旧的问答页面。这不是一个论坛,所以只要是相关的,就老问题或答案要求澄清是没有问题的。

标签: css


【解决方案1】:

你可以运行这样的东西。这会将opacity 设置为 0,并在几秒钟后将其斜升至 100%。此选项允许您微调希望它出现的速度,让您控制元素的不透明度以及在时间范围内的时间。

#showMe {
    opacity: 0;
    -moz-animation: cssAnimation 5s;
    /* Firefox */
    -webkit-animation: cssAnimation 5s;
    /* Safari and Chrome */
    -o-animation: cssAnimation 5s;
    /* Opera */
    animation: cssAnimation 5s;
    -webkit-animation-fill-mode: forwards;
    animation-fill-mode: forwards;
}
@keyframes cssAnimation {
    99% {
        opacity: 0;
    }
    100% {
        opacity: 1;
    }
}
@-webkit-keyframes cssAnimation {
    99% {
        opacity: 0;
    }
    100% {
        opacity: 1;
    }
}
<div id='showMe'>Wait for it...</div>

【讨论】:

  • 你可以用cssAnimation 0s 5s forwards;去掉99%的关键帧,会造成5s的延迟,没有动画时间
  • 好电话,我设置答案的方式是让提问者看到他们可以调整和设置不透明度和相关的时间范围。通过这种方式,他们可以将不透明度设置为在 2 秒内缓慢达到 50%,然后在 3 日快速达到 100……以此类推。
【解决方案2】:

试试这个简单的解决方案。

#showMe {
  animation: cssAnimation 0s 5s forwards;
  visibility: hidden;
}

@keyframes cssAnimation {
  to   { visibility: visible; }
}
<div id='showMe'>Wait for it...</div>

您还可以使用不透明度设置为可见性

#showMe {
  animation: cssAnimation 0s 5s forwards;
  opacity: 0; 
}

@keyframes cssAnimation {
  to   { opacity: 1; }
}
<div id='showMe'>Wait for it...</div>

【讨论】:

猜你喜欢
  • 2014-03-26
  • 1970-01-01
  • 2011-03-26
  • 2010-10-15
  • 1970-01-01
  • 2018-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多