使图标逐渐出现

huangapple go评论67阅读模式
英文:

Make icons appear gradually

问题

我有一个图标容器,并且我想制作一个动画,逐个延迟出现图标。以下是大致展示容器的示例。

.container{
  display:flex;
  gap: 8px;
  flex-flow: row wrap;
}
.container > img{
  width: auto;
  height: auto;
  max-width: 40px;
  max-height: 40px;
}
<div class="container">
  <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/e/e1/Google_Chrome_icon_%28February_2022%29.svg/1024px-Google_Chrome_icon_%28February_2022%29.svg.png">
  <!-- 这里包括更多图标的代码... -->
</div>

是否有人知道如何使用关键帧、JavaScript或它们的组合来实现这个效果?

英文:

I have an icon container, and I would like to make an animation that gradually delays the appearance of icons one at a time. Below is a small example of more or less how the box is

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.container{
  display:flex;
  gap: 8px;
  flex-flow: row wrap;
}
.container &gt; img{
  width: auto;
  height: auto;
  max-width: 40px;
  max-height: 40px
}

<!-- language: lang-html -->

&lt;div class=&quot;container&quot;&gt;
  &lt;img src=&quot;https://upload.wikimedia.org/wikipedia/commons/thumb/e/e1/Google_Chrome_icon_%28February_2022%29.svg/1024px-Google_Chrome_icon_%28February_2022%29.svg.png&quot;&gt;
  &lt;img src=&quot;https://upload.wikimedia.org/wikipedia/commons/thumb/e/e1/Google_Chrome_icon_%28February_2022%29.svg/1024px-Google_Chrome_icon_%28February_2022%29.svg.png&quot;&gt;
  &lt;img src=&quot;https://upload.wikimedia.org/wikipedia/commons/thumb/e/e1/Google_Chrome_icon_%28February_2022%29.svg/1024px-Google_Chrome_icon_%28February_2022%29.svg.png&quot;&gt;
  &lt;img src=&quot;https://upload.wikimedia.org/wikipedia/commons/thumb/e/e1/Google_Chrome_icon_%28February_2022%29.svg/1024px-Google_Chrome_icon_%28February_2022%29.svg.png&quot;&gt;
  &lt;img src=&quot;https://upload.wikimedia.org/wikipedia/commons/thumb/e/e1/Google_Chrome_icon_%28February_2022%29.svg/1024px-Google_Chrome_icon_%28February_2022%29.svg.png&quot;&gt;
  &lt;img src=&quot;https://upload.wikimedia.org/wikipedia/commons/thumb/e/e1/Google_Chrome_icon_%28February_2022%29.svg/1024px-Google_Chrome_icon_%28February_2022%29.svg.png&quot;&gt;
  &lt;img src=&quot;https://upload.wikimedia.org/wikipedia/commons/thumb/e/e1/Google_Chrome_icon_%28February_2022%29.svg/1024px-Google_Chrome_icon_%28February_2022%29.svg.png&quot;&gt;
  &lt;img src=&quot;https://upload.wikimedia.org/wikipedia/commons/thumb/e/e1/Google_Chrome_icon_%28February_2022%29.svg/1024px-Google_Chrome_icon_%28February_2022%29.svg.png&quot;&gt;
  &lt;img src=&quot;https://upload.wikimedia.org/wikipedia/commons/thumb/e/e1/Google_Chrome_icon_%28February_2022%29.svg/1024px-Google_Chrome_icon_%28February_2022%29.svg.png&quot;&gt;
  &lt;img src=&quot;https://upload.wikimedia.org/wikipedia/commons/thumb/e/e1/Google_Chrome_icon_%28February_2022%29.svg/1024px-Google_Chrome_icon_%28February_2022%29.svg.png&quot;&gt;
&lt;/div&gt;

<!-- end snippet -->

Does anyone know how to do this using keyframes or javascript or maybe a combination of these?

答案1

得分: 1

你可以在JavaScript中获取元素,并使用setTimeout函数逐渐添加类。

const div = document.getElementById("divID");
const children = div.childNodes;
const delay = 500; // 延迟的时间(毫秒)

// 你可能想要在某个地方执行的函数(也许是在onLoad事件中)
const childAppear = () => {
    children.forEach((child) => {
        setTimeout(() => {
            child.classList.add("transition-class");
        }, delay);
    });
}

然后是CSS部分:

.container img {
    opacity: 0;
    transition: 1s; /* 过渡的时长 */
}

.transition-class {
    opacity: 1;
    /* 你想要用于过渡图像的其他样式 */
}

不要忘记给你的div元素添加一个ID,这样你就可以在JavaScript中语义化地访问它:

<div id="divID">图片</div>
英文:

You could get the elements in javascript and add a class gradually with a setTimeout function

const div = document.getElementById(&quot;divID&quot;);
const children = div.childNodes;
const delay = 500; // time in milliseconds for the delay


// Your function that you might want to execute somewhere (maybe onLoad)
const childAppear = () =&gt; {
    children.forEach((child) =&gt; {
        setTimeout(() =&gt; {
            child.classList.add(&quot;transition-class&quot;);
        }, delay);
    }
}

Then for the CSS:

.container img {
    opacity: 0;
    transition: 1s; /* transition length */
 }

.transition-class {

    opacity: 100;
    /* any other styles you want for the transitioned image */
}

Don't forget to add an ID to your div element so you can access semantically in JavaScript

&lt;div id = &quot;divID&quot;&gt;IMAGES&lt;/div&gt;

huangapple
  • 本文由 发表于 2023年5月14日 09:55:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76245516.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定