HTML - Slider infinito Problema JS

 
Vista:
Imágen de perfil de IPhayton
Val: 2
Ha aumentado su posición en 31 puestos en HTML (en relación al último mes)
Gráfica de HTML

Slider infinito Problema JS

Publicado por IPhayton (1 intervención) el 03/05/2021 06:27:44
Buenas estoy intentando realizar un slider infinito manual, pero al pasar los bloques de imagenes, los hace de manera brusca y no lo hace suave, creo que tengo un problema en JS, o Css por favor si saben espero su ayuda resolviendo este problema , Gracias. E puesto todo como para pegar en donde sea que realicen sus paginas con el fin que se les haga mas facil entender el problema , por favor ayuda.

HTML CODIGO
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
<div class="container">
 
  <div class="carousel">
    <div class="slider">
 
      <section>
         <div class="img1"></div>
         </section>
 
      <section>
      <div class="img2"></div>
      </section>
 
      <section>
       <div class="img3"></div>
      </section>
 
      <section>
       <div class="img4"></div>
      </section>
 
 
    </div>
 
    <div class="controls">
      <button class="izquierda">
        <i class="material-icons">></i>
      </button>
 
      <button class="derecha">
        <i class="material-icons"><
</i>
 
</button>
    </div>
 
 
  </div>
</div>



CSS CODIGO

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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
body {
background-color: #f1e2ca;
display: flex;
}
 
.container {
  width: 80%;
  margin: 50px auto;
}
.carousel {
  border-radius: 5px;
  width: 100%;
  height: 800px;
  display: flex;
  justify-content: flex-start;
  overflow: hidden;
box-shadow: 0 0 0 10px #fff, 0 15px 50px;
  position: relative;
}
.slider {
  display: flex;
  height: 100%;
  width: 400%;
  flex-shrink: 0;
transition: all ease-out 0.5s;
}
 
 
 
.slider section {
  flex-basis: 25%;
  width: 25%;
  flex-shrink:0;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 20px;}
 
 
 
.img1{ display:block;
  background-color: rgb(105, 10, 10);
  width: 100%;
  height:100%;
}
 
  .img2{ display:block;
  background-color: rgb(202, 140, 7);
  width: 100%;
  height:100%;
}
 
  .img3{ display:block;
  background-color: rgb(10, 124, 124);
  width: 100%;
  height:100%;
}
 
  .img4{ display:block;
  background-color: rgb(71, 10, 46);
  width: 100%;
  height:100%;
}
 
 
 
.controls button.izquierda {
  position: absolute;
  right: 20px;
  top: 50%;
  width: 5%; min-width: 5%;
  height: 10%; min-height: 10%;
background-color: rgba(255, 255, 255, 0.7);
  border-radius: 50%;
  border: none;
  cursor: pointer;
  color: #000000;
}
 
.controls button.izquierda:hover{background-color: #fff;}
 
.controls button.derecha {
  position: absolute;
  left: 20px;
  top: 50%;
   width: 5%; min-width: 5%;
  height: 10%; min-height: 10%;
background-color: rgba(255, 255, 255, 0.7);
  border-radius: 50%;
  border: none;
  cursor: pointer;
}
.controls button i {
  font-size: 50px;
}
 
 
 
JS CODIGO
 
const carousel = document.querySelector('.carousel');
const slider = document.querySelector('.slider');
 
const izquierda = document.querySelector('.izquierda');
 
const derecha = document.querySelector('.derecha');
let direction;
 
 
izquierda.addEventListener('click', function () {
	if (direction === 1) {
		slider.prepend(slider.lastElementChild);
		carousel.style.justifyContent = 'flex-start';
		direction = -1;
	}
	slider.style.transform = 'translateX(-20%)';
});
 
 
derecha.addEventListener('click', function () {
	if (direction === -1) {
		direction = 1;
		slider.appendChild(slider.firstElementChild);
	}
	carousel.style.justifyContent = 'flex-end';
	slider.style.transform = 'translate(25%)';
});
 
slider.addEventListener(
	'transitionend',
	function () {
 
		if (direction === 1) {
			slider.prepend(slider.lastElementChild);
		} else {
			slider.appendChild(slider.firstElementChild);
		}
 
		slider.style.transition = 'none';
		slider.style.transform = 'translate(0)';
		setTimeout(() => {
			slider.style.transition = 'all 0.5s';
		});
	},
false
);
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder