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

192
Views
Boolean flickering from true to false

I'm trying to switch the position of an element between fixed and absolute based on how close the element is to the other. So I have a button which is fixed from the top to the bottom till it's bottom value + 25px is less or equal to footers y value so that it stops to the top. My problem is that the button stops in the right place so the switch from fixed to absolute goes well but while the relative position of the button to the footer are negative the boolean which toggles the classes of the button flickers between true and false so that it goes from absolute to fixed back and forth very quickly.

This is my code that is tracking the position & switching the boolean:

Is there a logic issue or what? I cannot figure this out :(

var button = document.querySelector('.bottomCtaFixed')
var footer = document.querySelector('.footer')
window.addEventListener('scroll', (e) => {
  var buttonRect = button.getBoundingClientRect()
  var buttonBottom = buttonRect.bottom + 25

  var footerRect = footer.getBoundingClientRect()
  var footerTop = footerRect.y

  var relativePos = footerTop - buttonBottom
  console.log('button', buttonRect)
  console.log('footer', footerRect)
  console.log('buttonBottom', buttonBottom)
  console.log('relativePos', relativePos)

  if (relativePos <= 0) {
    this.isStill = true
    if (footerTop >= buttonBottom) {
      this.isStill = false
    } else {
      this.isStill = true
    }
  } else {
    this.isStill = false
  }
  console.log(this.isStill)
})

And here's the classes:

.bottomCtaFixed {
  position: fixed;
  bottom: 25px;
  left: 25px;
  margin-top: 25px;
  width: calc(100% - 50px);
  z-index: 101;

  @media screen and (max-width: 290px) {
    display: none
  }

  @media screen and (min-width: $bp-xs) {
    display: none
  }
}

.bottomCtaStill {
  position: absolute;
  top: calc(-9vh - 25px);
  left: 25px;
  width: calc(100% - 50px);
  z-index: 101;

  @media screen and (max-width: 290px) {
    display: none
  }

  @media screen and (min-width: $bp-xs) {
    display: none
  }
}
about 4 years ago · Juan Pablo Isaza
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!