JavaScript - slider simple de imagenes.

 
Vista:
sin imagen de perfil

slider simple de imagenes.

Publicado por Iñigo (3 intervenciones) el 06/04/2018 21:29:56
Hola a todos, tengo un problema, he encontrado el siguiente slider de imagenes que es muy simple.

me funciona bien pero quiero poner un vinculo a cada imagen para poder abrirla y verla a tamaño completo y no se como hacerlo.

Espero me puedan ayudar.


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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<style type="text/css">
* {
margin: 0;
padding: 0;
border: none;
position: relative;
}
 
html, body {
width: 100%;
height: 100%;
}
 
a {outline: 0;}
 
 
.cont{
width: auto;
height: auto;
}
 
 
.cont > div {
display: inline-block;
vertical-align: top;
}
 
 
 
div#contenedor{
width: 400px;
height: 250px;
margin: 0 auto;
overflow: hidden;
border: 1px solid #CCC;
}
 
 
 
div#contenedor div.slider{
width: 200%; /* equivale al ancho de 2 imágenes. no tocar */
position: relative;
left: 0;
top: 0;
}
 
 
div#contenedor div.slider img {
width: 400px; /* mismo width de #contenedor */
height: 250px; /* mismo height de #contenedor */
float: left;
}
</style>
<script type="text/javascript">
function Evento(elemento, nomevento, fnc) {
 
  	if (elemento.addEventListener) {
 
		elemento.addEventListener(nomevento, fnc, false);
 
    	} else if (elemento.attachEvent) {
 
		elemento.attachEvent('on' + nomevento, fnc);
 
  	}
}
 
 
 
var imgslide = [
new imagenes('http://facebookportadas.com/imagenes/comotellamas.jpg'),
new imagenes('http://facebookportadas.com/imagenes/inocente.jpg'),
new imagenes('http://facebookportadas.com/imagenes/minecraft2.jpg'),
new imagenes('http://facebookportadas.com/imagenes/diloquesientes.jpg')
];
 
 
 
function imagenes(img, txt) {
	this.imagen = img;
}
 
 
 
var preImg = [];
 
for(var i = 0; i < imgslide.length; i++) {
 
	preImg[i] = new Image();
	preImg[i].src = imgslide[i].imagen;
}
 
 
function mover(posInicial, posFinal, slider, indiceImg){
 
	if (posInicial <= posFinal) {
 
		var siguienteImg = document.createElement('img');
		siguienteImg.setAttribute('src', preImg[indiceImg].src);
		slider.appendChild(siguienteImg);
 
		(function desplazar() {
 
			setTimeout(function(){
 
				if (posInicial >= posFinal) {
 
					slider.removeChild(slider.getElementsByTagName('img')[0]);
					slider.style.left = 0;
					return;
				}
 
				posInicial += 2;
				slider.style.left = -posInicial + '%';
				desplazar();
 
			},6);
 
		})();
	}
 
 
	setTimeout(function() {
 
		var nuevoIndice = parseInt(indiceImg+1) % imgslide.length;
 
		mover(0, posFinal, slider, nuevoIndice);
 
	},3000);
}
 
 
function inicia() {
 
	setTimeout(function() {
 
		var slider = document.querySelector('div.slider');
 
		mover(0, 100, slider, 1);
 
	}, 3000);
}
 
 
Evento(window, 'load', inicia);
</script>
</head>
<body>
 
<div id="contenedor">
 
<div class="slider">
	<img src="http://facebookportadas.com/imagenes/comotellamas.jpg" alt="i0" />
</div>
 
</div>
 
</body>
</html>
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