Estoy probando el segmento de un dígito y lo agrego en una función style.backgroundPosition
, pero siempre devuelve 2040 en lugar de 20 + 20 = 40. Este es solo un ejemplo, el número real para agregar se agrega dinámicamente según el 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'; })
Gracias a todos los que respondieron, lo arreglé con el código de Code Maniac:
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'; })