Dia 12/2019 - Slider com CSS

Desafio #92daysofcode que fiz no final de 2019. Nesse artigo temos o dia 12, mostrando o uso do seletor de irmãos seletor ~ seletor, com animação.

Pré-visualização

Código

Começamos com a estrutura do HTML.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Day 12</title>
<style>
*, *:before, *:after { -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing:border-box; }
html { -webkit-box-sizing:border-box; -moz-box-sizing:border-box; box-sizing:border-box; }
* { margin: 0; padding: 0; list-style: none; }
body { background: #eee; font-family: Arial, Helvetica, sans-serif; }
</style>
<link rel="stylesheet" type="text/css" href="./day-12.css">
</head>
<body>
<div class="container">
<!-- slider -->
</div>
</body>
</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
<input class="input" type="radio" name="next" id="slide1" checked>
<input class="input" type="radio" name="next" id="slide2">
<input class="input" type="radio" name="next" id="slide3">
<input class="input" type="radio" name="next" id="slide4">

<div class="slider">
<label for="slide4" class="arrow arrow--1 left"></label>
<label for="slide2" class="arrow arrow--1 right"></label>
<label for="slide1" class="arrow arrow--2 left"></label>
<label for="slide3" class="arrow arrow--2 right"></label>
<label for="slide2" class="arrow arrow--3 left"></label>
<label for="slide4" class="arrow arrow--3 right"></label>
<label for="slide3" class="arrow arrow--4 left"></label>
<label for="slide1" class="arrow arrow--4 right"></label>
<div class="slider-content">
<div class="slider-images">
<img class="slider-image" src="https://place-hold.it/700x400/999/fff" alt="Item 1">
<img class="slider-image" src="https://place-hold.it/700x400/ffcc00/fff" alt="Item 2">
<img class="slider-image" src="https://place-hold.it/700x400/ff0000/fff" alt="Item 3">
<img class="slider-image" src="https://place-hold.it/700x400/ccc/fff" alt="Item 4">
</div>
<div class="slider-pagination">
<label for="slide1" class="slider-pagination-bullet bullet1"></label>
<label for="slide2" class="slider-pagination-bullet bullet2"></label>
<label for="slide3" class="slider-pagination-bullet bullet3"></label>
<label for="slide4" class="slider-pagination-bullet bullet4"></label>
</div>
</div>
</div>
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
.input { display: none; }

.container { height: 100%; width: 100%; }

.slider { margin: 0 auto; }

.slider-content {
border: solid 2px #e0e0e0;
display: block;
height: 40%;
margin: 20px auto;
overflow: hidden;
width:60%;
}

.slider-image {
background-size: contain;
float: left;
margin: 0;
padding: 0;
transition: all 300ms ease-in-out;
width: 25%;
}

.slider-images { display: block; height: 40%; width: 400%; }

.slider-pagination {
position: relative;
width: 100%;
height: 20px;
margin-top: -21px;
float: left;
text-align: center;
z-index: 100;
}

.slider-pagination-bullet {
display: inline-block;
width: 16px;
height: 16px;
border-radius: 8px;
background: rgba(0,0,0,.8);
z-index: 5;
}

.slider-pagination-bullet:hover { background: rgba(255,255,255,.6); cursor: pointer; }

.slider-pagination-bullet:before {
background: #fff;
border-radius: 5px;
content: "";
display: none;
height: 10px;
margin: 3px;
width: 10px;
}

.right, .left {
background: #ccc;
background-size: 100%;
border-radius: 32px;
cursor: pointer;
height: 64px;
margin-top: 15%;
display: inline-block;
opacity: 0.9;
width: 64px;
}

.left { float: left; margin-left: 12%; }

.right { float: right; margin-right: 12%; }

.right:hover {
animation: _right ease 0.5s;
background-color: #ffcc00;
}

.left:hover {
animation: _left ease 0.5s;
background-color: #ffcc00;
}

.arrow { display: none; transition: background-color 300ms ease-in-out; }

#slide1:checked ~ .slider .slider-content .slider-pagination .bullet1:before,
#slide1:checked ~ .slider .arrow--1 { display: inline-block; }

#slide2:checked ~ .slider .slider-content .slider-images { margin-left: -100%; }
#slide2:checked ~ .slider .arrow--2,
#slide2:checked ~ .slider .slider-content .slider-pagination .bullet2:before { display: inline-block; }

#slide3:checked ~ .slider .slider-content .slider-images { margin-left: -200%; }
#slide3:checked ~ .slider .arrow--3,
#slide3:checked ~ .slider .slider-content .slider-pagination .bullet3:before { display: inline-block; }

#slide4:checked ~ .slider .slider-content .slider-images { margin-left: -300%; }
#slide4:checked ~ .slider .arrow--4,
#slide4:checked ~ .slider .slider-content .slider-pagination .bullet4:before { display: inline-block; }

@keyframes _right {
0% { background-position: 0px 0px; }
49.9% { background-position: 64px 0px; }
50% { display: none; background-position: -64px; }
50.1% { background-position: -64px; }
100% { background-position: 0px 0px; }
}

@keyframes _left {
0% { background-position: 0px 0px; }
49.9% { background-position: -64px 0px; }
50% { display: none; background-position: 64px; }
50.1% { background-position: 64px; }
100% { background-position: 0px 0px; }
}

Conclusão

A postagem original pode ser vista no meu Instagram e o código está acessível no meu Github.

Ao fechar este aviso ou continuar navegando no site Nerd Calistênico, você aceita o uso de cookies.

Este site usa cookies para assegurar a melhor experiência para os nossos usuários. Consulte nossa política de privacidade.

Uma nova versão está disponível. Clique aqui para atualizar.