Skip to content

Instantly share code, notes, and snippets.

@manuelcanga
Created January 7, 2022 19:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manuelcanga/1ecb5d941caebba166a954a8477030ca to your computer and use it in GitHub Desktop.
Save manuelcanga/1ecb5d941caebba166a954a8477030ca to your computer and use it in GitHub Desktop.
Cabecera flotante
/** Cambia de cabecera */
var trasweb = {};
trasweb.cabecera = function(slayer){
this.ALTURA_HACER_FLOTANTE_MENU = 400;
this.cabecera = document.getElementById(slayer);
this.getScroll = function () {
if (window.pageYOffset !== undefined) {
return [pageXOffset, pageYOffset];
}
else {
var sx, sy, d = document, r = d.documentElement, b = d.body;
sx = r.scrollLeft || b.scrollLeft || 0;
sy = r.scrollTop || b.scrollTop || 0;
return [sx, sy];
}
};
this.flotante = function () {
var position = this.getScroll();
if (position[1] > this.ALTURA_HACER_FLOTANTE_MENU) {
this.cabecera.classList.add('flotante');
} else {
this.cabecera.classList.remove('flotante');
}
};
window.onscroll= function() {
this.flotante();
}.bind(this);
};
/** ... **/
document.addEventListener('DOMContentLoaded', function(){
/* ---- Menú principal flotante --- */
var cabecera = new trasweb.cabecera('cabecera');
cabecera.flotante();
});
#cabecera.flotante {
position: fixed;
top: 0;
left: 0;
width: 100%;
right: 0;
margin: 0 auto;
background-color: #79b2d0;
z-index: 2000000010;
padding: 10px 0 0 10px;
}
#cabecera.flotante #logo {
margin: 10px 0 0 10px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment