1+ <!DOCTYPE html>
2+ < html lang ="en-us ">
3+ < head >
4+ < meta charset ="utf-8 "/>
5+ < meta name ="viewport " content ="width=device-width, initial-scale=1 "/>
6+ < style type ="text/css ">
7+
8+ body {
9+ display : flex;
10+ min-height : 100vh ;
11+ justify-content : center;
12+ align-items : center;
13+ }
14+
15+ .block__prev , .block__next {
16+ padding : 0 ;
17+ background : # ddd ;
18+ border-radius : 15px ;
19+ border : 1px solid gray;
20+ font-size : 24px ;
21+ line-height : 24px ;
22+ color : # 444 ;
23+ display : block;
24+ }
25+
26+ .block__prev : focus , .block__next : focus {
27+ outline : none;
28+ }
29+
30+ .block__prev : hover , .block__next : hover {
31+ background : # ccc ;
32+ cursor : pointer;
33+ }
34+
35+ .block__carousel {
36+ height : 130px ;
37+ width : 1300px ; /*9999px or 1600px or 1300px*/
38+ margin : 0 ;
39+ padding : 0 ;
40+ list-style : none;
41+ font-size : 0 ;
42+ }
43+
44+ .block__img {
45+ width : 130px ;
46+ height : 130px ;
47+ display : block; /*remove espaço extra perto das imagens*/
48+ }
49+
50+ .block__item-carousel {
51+ display : inline-block; /*remove espaço extra entre os itens da lista*/
52+ /*position: relative;*/
53+ /*margin: 0 15px 0 15px;*/
54+ }
55+
56+ .block__label {
57+ position : absolute;
58+ left : 0 ;
59+ top : 0 ;
60+ }
61+
62+ .block {
63+ width : 480px ;
64+ /*overflow: hidden;*/
65+ display : grid;
66+ grid-template-columns : 1fr 2fr 1fr ;
67+ justify-content : center;
68+ align-items : center;
69+ padding : 0 10px 0 10px ;
70+ background-color : lightgray;
71+ border-radius : 7px ;
72+ }
73+
74+ .block__content {
75+ width : 390px ;
76+ overflow : hidden;
77+ }
78+ </ style >
79+ </ head >
80+ < body >
81+
82+ < div class ="block ">
83+ < button class ="block__prev "> ⇦</ button >
84+ < div class ="block__content ">
85+ < ul class ="block__carousel ">
86+ < li class ="block__item-carousel ">
87+ < img class ="block__img " src ="https://en.js.cx/carousel/1.png " alt ="image emoj "/>
88+ </ li >
89+ < li class ="block__item-carousel ">
90+ < img class ="block__img " src ="https://en.js.cx/carousel/2.png " alt ="image emoj "/>
91+ </ li >
92+ < li class ="block__item-carousel ">
93+ < img class ="block__img " src ="https://en.js.cx/carousel/3.png " alt ="image emoj "/>
94+ </ li >
95+ < li class ="block__item-carousel ">
96+ < img class ="block__img " src ="https://en.js.cx/carousel/4.png " alt ="image emoj "/>
97+ </ li >
98+ < li class ="block__item-carousel ">
99+ < img class ="block__img " src ="https://en.js.cx/carousel/5.png " alt ="image emoj "/>
100+ </ li >
101+ < li class ="block__item-carousel ">
102+ < img class ="block__img " src ="https://en.js.cx/carousel/6.png " alt ="image emoj "/>
103+ </ li >
104+ < li class ="block__item-carousel ">
105+ < img class ="block__img " src ="https://en.js.cx/carousel/7.png " alt ="image emoj "/>
106+ </ li >
107+ < li class ="block__item-carousel ">
108+ < img class ="block__img " src ="https://en.js.cx/carousel/8.png " alt ="image emoj "/>
109+ </ li >
110+ < li class ="block__item-carousel ">
111+ < img class ="block__img " src ="https://en.js.cx/carousel/9.png " alt ="image emoj "/>
112+ </ li >
113+ < li class ="block__item-carousel ">
114+ < img class ="block__img " src ="https://en.js.cx/carousel/10.png " alt ="imag emoj "/>
115+ </ li >
116+ </ ul >
117+ </ div >
118+ < button class ="block__next "> ⇨</ button >
119+ </ div >
120+
121+ < script type ="text/javascript ">
122+ /**
123+ * Crie um “carrossel” – uma faixa de imagens que pode ser rolada clicando nas setas.
124+ *
125+ * Mais tarde, podemos adicionar mais recursos a ele: rolagem infinita, carregamento dinâmico etc.
126+ *
127+ * PS Para esta tarefa, a estrutura HTML/CSS é na verdade 90% da solução.
128+ */
129+
130+ //rotule as imagens para rastreá-las visualmente, apenas por conveniência,
131+ // este código pode ser removido
132+ let i = 1 ;
133+ let carousel = document . querySelector ( ".block__carousel" ) ;
134+ let blockContent = document . querySelector ( ".block__content" ) ;
135+ for ( let li of carousel . querySelectorAll ( 'li' ) ) {
136+ li . insertAdjacentHTML ( 'beforeend' , `<span class="block__label">${ i } </span>` ) ;
137+ i ++ ;
138+ }
139+
140+ //...seu código para dar vida ao carrossel!
141+ let arrowNext = document . querySelector ( ".block__next" ) ;
142+ let arrowPrev = document . querySelector ( ".block__prev" ) ;
143+ let itensCarousel = carousel . children , currentIndex = 0 ;
144+ itensCarousel = Array . from ( itensCarousel ) ;
145+ blockContent . scrollLeft = 0 ;
146+
147+ function countInterIndex ( currentIndex , lastIndex ) {
148+ let count = 1 ;
149+ for ( let i = currentIndex ; i < lastIndex ; i ++ ) {
150+ count ++ ;
151+ }
152+ return count ;
153+ }
154+
155+ function widthAll ( index , deslocamento , sentido ) {
156+ //conta deslocamente para frente
157+ let itens = [ ] , w = 0 ;
158+ if ( sentido === "positivo" ) {
159+ itens = itensCarousel . slice ( index , index + deslocamento ) ;
160+ for ( let i = 0 ; i < itens . length ; i ++ ) {
161+ w = w + itens [ i ] . clientWidth ;
162+ }
163+ return w ;
164+ } else {
165+ //conta deslocamento para tras
166+ itens = itensCarousel . slice ( index - deslocamento , index ) ;
167+ for ( let i = itens . length - 1 ; i >= 0 ; i -- ) {
168+ w = w + itens [ i ] . clientWidth ;
169+ }
170+ return w ;
171+ }
172+ }
173+
174+ arrowNext . addEventListener ( "click" , ( event ) => {
175+ if ( currentIndex < itensCarousel . length - 1 ) {
176+ if ( currentIndex % 3 === 0 ) {
177+ //posso mostrar 3
178+ if ( countInterIndex ( currentIndex , itensCarousel . length ) >= 3 ) {
179+ blockContent . scrollLeft = blockContent . scrollLeft + widthAll ( currentIndex , 3 , "positivo" ) ;
180+ //atualiza para posição atual
181+ currentIndex += 3 ;
182+ } else {
183+ //não posso mostrar 3
184+ //quanto posso andar ?
185+ blockContent . scrollLeft = blockContent . scrollLeft + widthAll ( currentIndex , countInterIndex ( currentIndex , itensCarousel . length ) , "positivo" ) ;
186+ currentIndex += countInterIndex ( currentIndex , itensCarousel . length ) ;
187+ }
188+ } else {
189+ console . log ( 'Não divisivel por três' ) ;
190+ }
191+ } else {
192+ blockContent . scrollLeft = blockContent . scrollLeft + itensCarousel [ currentIndex ] . clientWidth ;
193+ }
194+ } ) ;
195+
196+ arrowPrev . addEventListener ( "click" , ( event ) => {
197+ if ( currentIndex > 0 ) {
198+ if ( currentIndex % 3 === 0 ) {
199+ //posso mostrar 3 ?
200+ if ( countInterIndex ( 0 , currentIndex ) >= 3 ) {
201+ blockContent . scrollLeft = blockContent . scrollLeft - widthAll ( currentIndex , 3 , "negativo" ) ;
202+ //atualiza para posição atual
203+ currentIndex -= 3 ;
204+ } else {
205+ //não posso mostrar 3
206+ //quanto posso andar ?
207+ blockContent . scrollLeft = blockContent . scrollLeft - widthAll ( currentIndex , countInterIndex ( 0 , currentIndex ) , "negativo" ) ;
208+ currentIndex -= countInterIndex ( 0 , currentIndex ) ;
209+ }
210+ } else {
211+ console . log ( 'Não divivel por três' ) ;
212+ }
213+ } else {
214+ blockContent . scrollLeft = blockContent . scrollLeft - itensCarousel [ currentIndex ] . clientWidth ;
215+ }
216+ } ) ;
217+ </ script >
218+ </ body >
219+ </ html >
0 commit comments