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

Prueba con if, else if:

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 alpha a 0.9 en color 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.backgroundColor = "rgba(1, 56, 106, 0.9)"; // poner alpha a 0.9 en color azul
  12.        }else{ // si estamos subiendo con el scroll
  13.             document.getElementById("header").style.backgroundColor = "rgba(255, 0, 0, 0.9)"; // poner alpha a 0.9 en color rojo
  14.        }
  15.   }
  16.   prevScrollpos = currentScrollPos; // actualizar posicion previa
  17. };

Y la transición la estableces desde CSS:

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


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

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