foreach using css3

Asked

Viewed 257 times

2

So I have a div with 3 imagens

<div class="pai">
  <img src="1.jpg" />
  <img src="2.jpg" />
  <img src="3.jpg" />
</div> 

I would like these three images to run from right to left in order until you reach your positions, and when you arrive, stop there.

But I would like them not all to go out together. I would like the second to take a time of about 2 seconds after the exit of the first.

Thinking about it, I came up with the idea of using a foreach. But you can implement this in css3? as?

The model I made just makes them come in sequence.

Obs: NAY would like to use Javascript. I want to use CSS only.

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Documento sem título</title>
<style>
.pai {  
    width: 1000px;
    height:300px;
    position: relative;
    overflow: hidden;
}

.pai > img {
    width: 10%;
    position:relative;
    animation: animacao 5s;
    animation-delay: 3s;
    animation-direction: alternate;
}


@keyframes animacao{
    0%   {left: 110%; top: 0px;}
    100% {left: 0px; top: 0px;}
}

</style>
</head>
<body>

  <div class="pai"> 
    <img src="1.jpg">
    <img src="2.jpg">
    <img src="3.jpg">
  </div>

</body>
</html>

1 answer

4


This code does not use JS, however you need to create DELAY for each item, with SASS it would be possible to make N items easily. I added Javascript to make the elements appear only when scrolling the page.

$( window ).scroll(function() {
		if($( window ).scrollTop() > $('#pai').offset().top - 300) 	           $('#pai').addClass("pai");
	});
#rolagem {
  width: 100%;
  height: 800px;
  background: #CCC;
}

#pai {
  width: 1000px;
  height:300px;
  position: relative;
  overflow: hidden;
}

img {
  display: none;
}

.pai > img {
  display: inline-block;
  width: 10%;
  position:relative;
  animation: animacao 3s;
  animation-fill-mode: forwards;
}

.pai > img:nth-child(2) {
  animation-delay: 2s;
  left: 110%;
}

.pai > img:nth-child(3) {
  animation-delay: 4s;
  left: 110%;
}

@keyframes animacao{
    0%   {left: 110%; top: 0px; opacity: 0;}
    55%  {opacity: 0;}
    100% {left: 0px; top: 0px; opacity: 1}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="rolagem"></div>

<div id="pai"> 
    <img src="http://www.suasletras.com/fotos_artista/637eacb259910a2df5bde545f367ab77.jpg">
    <img src="http://www.suasletras.com/fotos_artista/637eacb259910a2df5bde545f367ab77.jpg">
    <img src="http://www.suasletras.com/fotos_artista/637eacb259910a2df5bde545f367ab77.jpg">
  </div>

  • So that was it. But taking advantage, as it does for these figures, in their times of appearance, appear as if it were lightning (flash). But they only appear when, when you scroll down the page, you reach the div of them?

  • I believe it is now necessary JS, you will use?

  • No, I just wanted to use CSS. It’s much faster and lighter css.

  • I believe that to capture the location of Scroll it is necessary to use JS, why can’t you use it? (explains better this idea of flash)

  • kkk, it appears on the right with a clarity, when it arrives in the middle, it gives a flash in the image and then the image goes back to normal clarity state

  • I added an idea of flash, I apologize for using my photo, it was the first thing I had the hand haha

  • I found an example of the type of flash I wanted: https://www.nubank.com.br/gerando-boletos/, night when going down (and only after arriving in the div), observe how the first image when scrolling the screen appears. It was this flash effect that I wanted. I think it has to do with the clarity of the image itself!

  • huh! your photo? Which of the 4? The 4? kkk

  • I changed the photo and put a generic of Google haha, I understood your need and I already update CSS! I repeat that to make the effect run only with the scrolling of the page you need Javascript.

  • Yeah, I got it, I did it in pure JS. But then gave me another question, in that if, I should put all the elements that are below the screen height because the page has several Ivs one below the other, and the Ivs that are within the screen height? How will I predict this for the various screen sizes?

  • @Carlosrocha not really $('#pai').offset().top picks up how much scroll is needed to get to the div #pai independent of screen size.

  • That is, the top of css, that is, the media from the beginning of the browser to the edge of the parent div

Show 7 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.