Código HTML :
<!DOCTYPE html> <html lang="es"> <head> <title> Analsis Position fixed </title> <meta charset="utf-8" /> <link rel="stylesheet" href="styles.css" /> </head> <body> <div id="main-wrap"> <div id="fixed-container"> <div id="container-a"> <div id="container-b"> </div> <div id="container-c"> </div> </div> </div> <div id="showcase"> </div> </div> <div id='btn-toggle'> </div> <!-- Scripts .js.--> <script src="jQuery-v211.js"> </script> <script src="mi-script.js"> </script> </body> </html>
Código CSS :
*
{
border:0;
box-sizing: border-box; /* El padding y border del elemento no incrementan su ancho */
-moz-box-sizing: border-box; /* moz = prefijo para mozilla firefox */
-ms-sizing: border-box; /* ms = prefijo para Internet Explorer */
-o-box-sizing: border-box; /* o = prefijo para mozilla Opera */
-webkit-box-sizing: border-box; /* webkit = prefijo Chrome y Safari */
margin: 0;
outline: none;
padding: 0;
text-indent: 0;
}
#btn-toggle
{
position: fixed;
bottom: 30px; /* Distancia desde abajo */
right: 30px; /* Distancia desde la derecha */
width: 60px; /* Ancho del botón */
height: 60px; /* Alto del botón */
display: block;
background: url(flecha-arriba.png) no-repeat center center;
}
#container-a
{
width: 80%; /* también puede ser el 100% total */
background: green;
height: 300px;
margin: 0 auto;
}
#container-b
{
width: 50%;
background: brown;
height: 200px;
margin: 0 auto;
float: left;
margin-top: 45px;
}
#container-c
{
width: 50%;
background: yellow;
height: 200px;
margin: 0 auto;
float: left;
margin-top: 45px;
}
#main-wrap
{
background: blue;
width: 100%;
}
#showcase
{
width: 80%;
background: pink;
height: 5000px;
margin: 0 auto;
}
Código Javascript :
// boton para hacer "toggle" con el '#fixed-container'
$(document).ready(function(){
$('#btn-toggle').on('click',function(){
$('#fixed-container').slideToggle('slow');
if ($('#fixed-container').show())
{
$('#btn-toggle').css('background-image', 'url(flecha-abajo.png)');
}
else
{
$('#btn-toggle').css('background-image', 'url(flecha-arriba.png)'); // OJO ESTE ES LO QUE NO ME HACE, VOLVER A MOSTRAR LA FLECHA HACIA ARRIBA
}
});
// hacer aparecer el boton al hacr scroll hacia abajo'
$("#btn-toggle").hide();
$(function () {
$(window).scroll(function () {
if ($(this).scrollTop() > 2000)
{
$('#btn-toggle').fadeIn();
}
else
{
$('#btn-toggle').fadeOut();
}
});
});
// hacer aparecer (si esta oculto) el div '#fixed-container' al hacer scroll hacia arriba'
$(window).scroll(function () {
if ($(this).scrollTop() < 2000)
{
$('#fixed-container').slideDown();
$('#btn-toggle').css('background-image', 'url(flecha-arriba.png)');
}
});
});
