Last active
February 14, 2019 16:05
-
-
Save joseacat/8db1302d36f9e68b348b9926c59cce7f to your computer and use it in GitHub Desktop.
Ver/ocultar elementos uno a uno con JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
document.addEventListener('DOMContentLoaded', function() { | |
var bloques_ocultos = document.getElementsByClassName('bloque oculto'); | |
var bloques = document.getElementsByClassName('bloque'); | |
var numero_bloques = bloques.length - 1; | |
// Click en ver más | |
document.getElementById('ver-mas').addEventListener('click', function() { | |
if(bloques_ocultos[0]){ | |
console.log(bloques_ocultos.length); | |
bloques_ocultos[0].classList.remove('oculto'); | |
if(bloques_ocultos.length == 0){ | |
this.classList.toggle('oculto'); | |
document.getElementById('ver-menos').classList.toggle('oculto'); | |
numero_bloques = bloques.length - 1; | |
} | |
} | |
}); | |
document.getElementById('ver-menos').addEventListener('click', function() { | |
if(numero_bloques >1){ | |
console.log(bloques[numero_bloques].classList.toggle('oculto')); | |
numero_bloques--; | |
if(numero_bloques == 1){ | |
this.classList.toggle('oculto'); | |
document.getElementById('ver-mas').classList.toggle('oculto'); | |
numero_bloques = bloques.length - 1; | |
} | |
} | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
html{ | |
background: #FFF; | |
font-family: Arial, Helvetica, sans-serif; | |
} | |
.bloque{ | |
padding: 1rem; | |
color: #FFF; | |
text-align: center; | |
background: tomato; | |
margin-bottom: 1rem; | |
} | |
.oculto{ | |
display: none; | |
} | |
.boton span { | |
text-decoration: none; | |
font-size: 20px; | |
text-transform: uppercase; | |
font-weight: bold; | |
color: green; | |
cursor: pointer; | |
} | |
.boton { | |
text-align: center; | |
padding: 1rem; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Titulo</title> | |
<base href="" target="_blank"> | |
<link rel="stylesheet" type="text/css" href="estilo.css"> | |
<script src="app.js"></script> | |
<meta name="keywords" content="HTML, CSS, XML, XHTML, JavaScript"> | |
<meta name="description" content="Description of what is contained on the Website"> | |
<meta name="author" content="Timothy E. Michel"> | |
<!--<meta http-equiv="refresh" content="30">--> | |
<meta charset="utf-8"> | |
</head> | |
<body> | |
<section> | |
<div class="bloque"> | |
Bloque 1 | |
</div> | |
<div class="bloque"> | |
Bloque 2 | |
</div> | |
<div class="bloque oculto"> | |
Bloque 3 | |
</div> | |
<div class="bloque oculto"> | |
Bloque 4 | |
</div> | |
<div class="bloque oculto"> | |
Bloque 5 | |
</div> | |
<div class="boton"> | |
<span id="ver-mas" href="#" title="Ver más" >Ver más</span> | |
<span id="ver-menos" class="oculto" href="#" title="Ver menos">Ver menos</span> | |
</div> | |
</section> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment