Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

241
Vistas
Creating parallax background effect from knockout text

I am trying to create a parallax background effect with some knockout text. I am facing two issues:

  1. The parallax effect does not work at all when background-size: contain; is set.

  2. The parallax effect, for whatever reason, does not work outside of Codepen - when scaled up fully in chrome, the effect disappears and behaves exactly as if the background is stuck to your mouse...I am not sure if this is a scaling issue or issue with chrome and my method.

Here is my code:

(function() {
  // Add event listener
  document.addEventListener("mousemove", parallax);
  const elem = document.querySelector("#text");
  // Magic happens here
  function parallax(e) {
    let _w = window.innerWidth / 2;
    let _h = window.innerHeight / 2;
    let _mouseX = e.clientX;
    let _mouseY = e.clientY;
    let _depth1 = `${50 - (_mouseX - _w) * 0.01}% ${50 - (_mouseY - _h) * 0.01}%`;
    let _depth2 = `${50 - (_mouseX - _w) * 0.02}% ${50 - (_mouseY - _h) * 0.02}%`;
    let _depth3 = `${50 - (_mouseX - _w) * 0.06}% ${50 - (_mouseY - _h) * 0.06}%`;
    let x = `${_depth3}, ${_depth2}, ${_depth1}`;
    console.log(x);
    elem.style.backgroundPosition = x;
  }

})();
.background {
  background: #000;
}

#text {
  background: url("https://images.unsplash.com/photo-1465101162946-4377e57745c3?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1957&q=80");
  /*background-size: contain;*/
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  font-size: 8vw;
}
<body class="background">
  <h1 id="text"><b>Galaxy</b></h1>
</body>

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The solution turned out to be that it depends on the size of the image, which it seems must be smaller than the container (in this case the entire screen/body) in order for this to work.

The image I was using in the above codepen was different to the image I was using in my local instance. To make it behave correctly, I simply applied background-size: 50%; to my much larger local background image so that it behaves more like the smaller background image used in the codepen.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda