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

239
Vistas
Number appending other number instead of adding - Javascript

I am trying the slice a digit and add it in a style.backgroundPosition function but it's always returning 2040 instead of 20 + 20 = 40. This is just an example the actual number to add is dynamically added depending on the pageYOffset.

JS:

let bg = document.querySelector('body .section_1')

let bgPos = window.getComputedStyle(bg).getPropertyValue('background-position'); // returns '20% 0' (without quotes)
let bgPosNum = bgPos.slice(0, 2)
let bgPosArr = bgPosNum.toString()

document.addEventListener('scroll', () => {                
  let x = window.pageYOffset
  bg.style.backgroundPosition  = '' + (bgPosNum + x/6) + '% 0';            
})
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Thanks to everyone who replied, I fixed it with Code Maniac's code:

let bg = document.querySelector('body .section_1')

let bgPos = window.getComputedStyle(bg).getPropertyValue('background-position');
let bgPosNum = bgPos.slice(0, 2)
let bgPosStr = parseInt(bgPosNum)

document.addEventListener('scroll', () => {                
  let x = window.pageYOffset
  bg.style.backgroundPosition  = '' + (bgPosNum + x/6) + '% 0';            
})
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