Пример:
HTML:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<div class="slideshow"> <div class="slideshow-item"> <img src="photo-1.jpg" alt=""> <div class="slideshow-item-text"> <h5>Заголовок</h5> <p>Описание</p> </div> </div> <div class="slideshow-item"> <img src="photo-2.jpg" alt=""> <div class="slideshow-item-text"> <h5>Заголовок</h5> <p>Описание</p> </div> </div> <div class="slideshow-item"> <img src="photo-3.jpg" alt=""> <div class="slideshow-item-text"> <h5>Заголовок</h5> <p>Описание</p> </div> </div> <div class="slideshow-item"> <img src="photo-4.jpg" alt=""> <div class="slideshow-item-text"> <h5>Заголовок</h5> <p>Описание</p> </div> </div> </div> |
CSS:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
.slideshow { width: 100%; height: 500px; position: relative; overflow: hidden; background: #000; margin: 20px 0; } .slideshow-item { width: 100%; height: 100%; position: absolute; opacity: 0; animation: slideanim 40s infinite; } .slideshow-item:nth-child(1), .slideshow-item:nth-child(1) img { animation-delay: 0; } .slideshow-item:nth-child(2), .slideshow-item:nth-child(2) img { animation-delay: 10s; } .slideshow-item:nth-child(3), .slideshow-item:nth-child(3) img { animation-delay: 20s; } .slideshow-item:nth-child(4), .slideshow-item:nth-child(4) img { animation-delay: 30s; } .slideshow-item img { width: 100%; height: 100%; object-fit: cover; animation: zoom 40s infinite; } .slideshow-item-text { max-width: 50%; position: absolute; bottom: 20px; left: 20px; background-color: rgba(0,0,0,0.7); color: #fff; padding: 20px 30px; font-family: Verdana, sans-serif; } .slideshow-item-text h5 { font-size: 22px; margin: 0 0 10px 0; color: #BFE2FF; } .slideshow-item-text p { font-size: 15px; margin-bottom: 10px; } @keyframes slideanim { 12.5%{ opacity: 1; } 25%{ opacity: 1; } 37.5%{ opacity: 0; } } @keyframes zoom { 50%{ transform: scale(1.3); } } @media screen and (max-width: 1100px){ .slideshow-item-text{ max-width: 75%; } } @media screen and (max-width: 456px){ .slideshow-item-text { bottom: 0; left: 0; max-width: 100%; } .slideshow-item-text h5 { font-size: 18px; } .slideshow-item-text p { font-size: 13px; } } |
Немного о коде
На показ каждого слайдера выделяется 10 секунд, а всего их 4. Поэтому общая продолжительность анимации составляет 40 секунд. Это указывается в строках animation: slideanim 40s infinite
и animation: zoom 40s infinite
Для каждого слайда отдельно устанавливается задержка, это классы .slideshow-item:nth-child(1)
, .slideshow-item:nth-child(1) img
и т.д.
Значения в анимации slideanim
устанавливаются так:
12.5% {opacity: 1}
- 100 / 4 (кол-во слайдов) / 2, при котором переходим с полной прозрачности в видимую область25% {opacity: 1}
- 100 / 4 (кол-во слайдов), при котором показываем непрозрачный слайд37.5% {opacity: 0}
- складываем первые два значения для перехода опять в прозрачность
Значения в анимации zoom
устанавливаются так:
50% {transform: scale(1.3)}
- 100 / 4 (кол-во слайдов) x 2, при котором увеличиваем фотографию
Например, для 7-ми слайдов эти анимации будут выглядеть так:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
@keyframes slideanim { 7.15%{ opacity: 1; } 14.3%{ opacity: 1; } 21.5%{ opacity: 0; } } @keyframes zoom { 28.5%{ transform: scale(1.3); } } |
Добавить комментарий: