Ver Mensaje Individual
  #4 (permalink)  
Antiguo 09/11/2019, 19:39
prueba230683
 
Fecha de Ingreso: abril-2011
Mensajes: 170
Antigüedad: 13 años
Puntos: 68
Respuesta: if else o swich para añadir efecto

Bien, pues a ver así:

https://www.w3schools.com/code/tryit.asp?filename=G9SP4PXWFUNX

Código CSS:
Ver original
  1. #header{
  2.  background-color:transparent;
  3.  position:fixed;
  4.  top: 0;
  5.  width: 100%;
  6.  transition: top 0.3s;
  7. }

Código Javascript:
Ver original
  1. var prevScrollpos = window.pageYOffset;
  2. window.onscroll = function() {
  3.   var currentScrollPos = window.pageYOffset;
  4.  
  5.   if(currentScrollPos == 0){  // si la distancia es cero
  6.        document.getElementById("header").style.backgroundColor = "rgba(255, 0, 0, 0)"; // poner totalmente transparente
  7.   }else if(currentScrollPos < 200){ // si la distancia esta entre 0 y 200
  8.        document.getElementById("header").style.backgroundColor = "rgba(255, 0, 0, 0.9)"; // poner en rojo
  9.   }else{ // si la distancia es mayor que 200
  10.      if(prevScrollpos < currentScrollPos){ // si estamos bajando con el scroll
  11.         document.getElementById("header").style.top = "-100%";// esconder header
  12.      }else{ // si estamos subiendo con el scroll
  13.         document.getElementById("header").style.top = "0"; // mostrar header
  14.      }
  15.   }
  16.   prevScrollpos = currentScrollPos; // actualizar posicion previa
  17. };

Última edición por prueba230683; 09/11/2019 a las 19:50