Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

237
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!