JavaScript - Javascript para cambiar imágenes

 
Vista:
sin imagen de perfil
Val: 10
Ha disminuido su posición en 6 puestos en JavaScript (en relación al último mes)
Gráfica de JavaScript

Javascript para cambiar imágenes

Publicado por Señor X (8 intervenciones) el 14/01/2019 22:49:05
Tengo este código y necesito que al darle al botón con forma de sol, la imagen cambie a la segunda y cuando el botón cambie a luna y le clickes, la imagen vuelva a ser la anterior

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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<title>María Zambrano</title>
<style type="text/css">@import "bourbon";
@import "neat";
*, *:before, *:after {
  box-sizing: border-box;
  outline: none;
}
body {
  background-color: whitesmoke;
  font-family: 'Source Serif Pro', sans-serif;
  font-size: 16px;
  font-smooth: auto;
  font-weight: 400;
  line-height: 1.95;
  color: #444;
  transition: background-color 250ms ease;
}
body.night {
  color: whitesmoke;
  background-color: #2d3143;
}
body.night .button {
  color: #2d3143;
  background-color: transparent;
}
body.night .button span:nth-of-type(1) {
  display: none;
}
body.night .button span:nth-of-type(2) {
  display: block;
}
header {
  position: relative;
  display: block;
  width: 100%;
  height: 100px;
  background-image: url('images/sol.jpeg');
  background-position: center center;
  background-size: cover;
}
header.night {
  background-image: url('images/luna.jpeg');
  background-position: center center;
  background-size: cover;
}
header h1 {
  position: absolute;
  top: 53px;
  left: 0;
  margin: 0;
  padding: 0;
  color: whitesmoke;
  text-transform: uppercase;
  transition: color 250ms ease;
}
header h1.night {
  color: #2d3143;
}
section {
  padding: 50px 0;
  font-size: 20px;
}
.container {
  position: relative;
  display: block;
  max-width: 65%;
  margin: 0 auto;
}
.button {
  position: fixed;
  display: flex;
  align-items: center;
  justify-content: center;
  top: 20px;
  right: 20px;
  background-color: transparent;
  border-radius: 50%;
  border: none;
  font-family: 'Source Sans Pro', sans-serif;
  font-size: 32px;
  font-weight: 400;
  color: white;
  text-transform: uppercase;
  cursor: pointer;
  z-index: 99;
}
.button span {
  border-radius: 50%;
  margin: 0;
  padding: 0;
}
.button span:nth-of-type(1) {
  display: block;
}
.button span:nth-of-type(2) {
  display: none;
}
button[data-tooltip].left:before, button[data-tooltip].left:after {
  transform: translateX(0px);
}
button[data-tooltip].left:hover:after, button[data-tooltip].left:hover:before {
  transform: translateX(-10px);
}
button[data-tooltip]:after, button[data-tooltip]:before {
  position: absolute;
  visibility: hidden;
  opacity: 0;
  transition: transform 200ms ease, opacity 200ms;
  z-index: 99;
}
button[data-tooltip]:before {
  content: attr(data-tooltip);
  background: #000;
  color: #fff;
  font-size: 10px;
  font-weight: bold;
  padding: 10px 15px;
  border-radius: 5px;
  white-space: nowrap;
  text-decoration: none;
  text-transform: uppercase;
  letter-spacing: 1px;
}
button[data-tooltip]:after {
  content: '';
  width: 0;
  height: 0;
  border-top: 6px solid transparent;
  border-bottom: 6px solid transparent;
  border-left: 6px solid green;
}
button[data-tooltip]:hover:after, button[data-tooltip]:hover:before {
  visibility: visible;
  opacity: 0.85;
  transform: translateY(0px);
}
button[data-tooltip][data-position="left"]:before {
  top: 0;
  right: 100%;
  margin-right: 6px;
}
button[data-tooltip][data-position="left"]:after {
  border-left-color: #000;
  border-right: none;
  top: 25%;
  right: 100%;
}
#mySidenav a {
  text-align: center;
  position: fixed;
  left: -80px;
  transition: 0.3s;
  padding: 15px;
  width: 120px;
  text-decoration: none;
  font-size: 20px;
  color: white;
  border-radius: 0 5px 5px 0;
}
 
#mySidenav a:hover {
  left: 0;
}
 
#about {
  top: 40px;
  background-color: #4CAF50;
}
 
#blog {
  top: 173px;
  background-color: #2196F3;
}
 
#projects {
  top: 300px;
  background-color: #f44336;
}
 
#contact {
  top: 300px;
  background-color: #555
}
 
h1 {
  text-align: center;
   font-size:300%;
}</style>
 
</head>
<body>
 
<button id="toggle"   data-tooltip="Click aquí para cambiar entre modo día/noche" data-position="left" class="button left">
	<span>
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
Imágen de perfil de ScriptShow
Val: 2.019
Plata
Ha mantenido su posición en JavaScript (en relación al último mes)
Gráfica de JavaScript

Javascript para cambiar imágenes

Publicado por ScriptShow (692 intervenciones) el 15/01/2019 03:10:19
Saludos,

veamos 2 ejemplos sencillos con JavaScript:

Código 1
1
2
3
4
5
<script>
var lunaImg= "luna.jpg";
var solImg= "sol.jpg";
</script>
<img src="sol.jpg" onclick="this.src = this.src == solImg ? lunaImg : solImg;"/>

Código 2
1
2
3
4
5
<button onclick="document.getElementById('Imagen').src='luna.jpg'">L u n a</button>
 
<button onclick="document.getElementById('Imagen').src='sol.jpg'">S o l</button>
 
<p align="center"><img id="Imagen" src="sol.jpg" /></p>

Se pueden ampliar y adaptar con CSS Style y JavaScript. En cualquier caso, no precisa librerías de terceros.

Espero sea útil.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
Imágen de perfil de ScriptShow
Val: 2.019
Plata
Ha mantenido su posición en JavaScript (en relación al último mes)
Gráfica de JavaScript

Javascript para cambiar imágenes

Publicado por ScriptShow (692 intervenciones) el 16/01/2019 13:29:47
Saludos,

veamos un sencillo ejemplo adaptable, ampliable, compatible... No precisa librerías de terceros.

Revisa el contenido del archivo adjunto.

Espero sea útil.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar