Questo vi piacerà

Oggi imparerai a creare un’animazione a scorrimento delle immagini , le immagini scorreranno come per scorrere dall’alto verso il basso o dall’opposto. È inoltre possibile inserire collegamenti alle immagini per reindirizzare gli utenti alla destinazione scelta. 

Anteprima

Prima di condividere il codice sorgente , parliamone. Innanzitutto ricevo immagini casuali da picsum.photos , puoi chiamare questo lorem ipsum delle foto. È possibile ottenere facilmente foto fittizie e casuali.

HTML

<!DOCTYPE html>

<html>

<head>
  <meta charset="UTF-8">
  <title>Animated Scrolling Images </title>
      <link rel="stylesheet" href="style.css">
	
</head>

<body>

  <div class="main">
	  
	<div class="single-column">
		<a href="#"><img src="https://picsum.photos/300/300?random=1" alt="image"></a>
		<a href="#"><img src="https://picsum.photos/300/300?random=2" alt="image"></a>
		<a href="#"><img src="https://picsum.photos/300/300?random=3" alt="image"></a>
		<a href="#"><img src="https://picsum.photos/300/300?random=4" alt="image"></a>
	</div>
	<div class="single-column">
		<a href="#"><img src="https://picsum.photos/300/300?random=5" alt="image"></a>
		<a href="#"><img src="https://picsum.photos/300/300?random=6" alt="image"></a>
		<a href="#"><img src="https://picsum.photos/300/300?random=7" alt="image"></a>
		<a href="#"><img src="https://picsum.photos/300/300?random=8" alt="image"></a>
	</div>
	<div class="single-column">
		<a href="#"><img src="https://picsum.photos/300/300?random=9" alt="image"></a>
		<a href="#"><img src="https://picsum.photos/300/300?random=10" alt="image"></a>
		<a href="#"><img src="https://picsum.photos/300/300?random=11" alt="image"></a>
		<a href="#"><img src="https://picsum.photos/300/300?random=12" alt="image"></a>
	</div>
	<div class="single-column">
		<a href="#"><img src="https://picsum.photos/300/300?random=13" alt="image"></a>
		<a href="#"><img src="https://picsum.photos/300/300?random=14" alt="image"></a>
		<a href="#"><img src="https://picsum.photos/300/300?random=15" alt="image"></a>
		<a href="#"><img src="https://picsum.photos/300/300?random=16" alt="image"></a>
	</div>
	<div class="single-column">
		<a href="#"><img src="https://picsum.photos/300/300?random=17" alt="image"></a>
		<a href="#"><img src="https://picsum.photos/300/300?random=18" alt="image"></a>
		<a href="#"><img src="https://picsum.photos/300/300?random=19" alt="image"></a>
		<a href="#"><img src="https://picsum.photos/300/300?random=20" alt="image"></a>
	</div>
	<div class="single-column">
		<a href="#"><img src="https://picsum.photos/300/300?random=21" alt="image"></a>
		<a href="#"><img src="https://picsum.photos/300/300?random=22" alt="image"></a>
		<a href="#"><img src="https://picsum.photos/300/300?random=23" alt="image"></a>
		<a href="#"><img src="https://picsum.photos/300/300?random=24" alt="image"></a>
	</div>
	<div class="single-column">
		<a href="#"><img src="https://picsum.photos/300/300?random=25" alt="image"></a>
		<a href="#"><img src="https://picsum.photos/300/300?random=26" alt="image"></a>
		<a href="#"><img src="https://picsum.photos/300/300?random=27" alt="image"></a>
		<a href="#"><img src="https://picsum.photos/300/300?random=28" alt="image"></a>
	</div>
	<div class="single-column">
		<a href="#"><img src="https://picsum.photos/300/300?random=29" alt="image"></a>
		<a href="#"><img src="https://picsum.photos/300/300?random=30" alt="image"></a>
		<a href="#"><img src="https://picsum.photos/300/300?random=31" alt="image"></a>
		<a href="#"><img src="https://picsum.photos/300/300?random=32" alt="image"></a>
	</div>
</div>
  
    <script  src="function.js"></script>

</body>
</html>

CSS


body {
  margin: 0;
  padding: 0;
  min-height: 100vh;
  display: grid;
  place-items: center;
}

.main {
  display: flex;
  overflow: hidden;
  height: 50vw;
  background: #333;
}

.main img {
  max-width: 100%;
  vertical-align: middle;
  border: 2px solid white;
  box-sizing: border-box;
  transition: opacity .2s;
}

.main:hover img {
  opacity: 0.5;
}

.main img:hover {
  opacity: 1;
}

.main .single-column {
	animation: var(--animation, none) 16s infinite linear;
}

.main .single-column:hover {
	animation-play-state: paused;
}

.main .single-column:nth-of-type(odd) {
  align-self: flex-end;
  --direction: 50%;
}

@keyframes slide {
  to {
    -webkit-transform: translateY(var(--direction, -50%));
            transform: translateY(var(--direction, -50%));
  }
}

Javascript


[...document.querySelectorAll('.single-column')].map(column => {
	column.style.setProperty('--animation', 'slide');
	column.style.setProperty('height', '200%');
	column.innerHTML = column.innerHTML + column.innerHTML;
});

Lascia un commento