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

201
Views
How to change value of CSS variable as page scrolls gsap?

I have a css variable named --color. I want to change the value of this css variable as the page scrolls down. The scrolling of the page should "scrub" the value.

Example: --color starts out at rgb(255, 255, 255). As the page scrolls down, the value gets closer to and eventually reaches rgb(0, 0, 0) upon scrolling down all the way. When scrolling back up, the value should slowly advance back to rgb(255, 255, 255), reaching it by the time the page is scrolled up completely.

I have been trying to achieve this with GSAP but have yet to find any success, although I believe it must be possible with the library.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Sure, here you go:

gsap.to("body", {
  "--color": "black",
  scrollTrigger: {
    start: 0,
    end: "max",
    scrub: true
  }
});

https://codepen.io/GreenSock/pen/WNOzWaM?editors=0110

By the way, there are dedicated GSAP forums at https://greensock.com/forums

about 4 years ago · Juan Pablo Isaza Report

0

unfortunately I am not familiar with GSAP, but here is the link that describes how to get the computed styles and the property value of that style with JS. You need to listen to scroll event and change the value of the css variable inside of the listener, like so:

document.addEventListener('scroll', function(e) {
  var root = document.querySelector(':root');
  var newColor = // do your color calculations here;
  root.style.setProperty('--color', newColor);
  }

here are some useful links.

https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleDeclaration/setProperty https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleDeclaration/getPropertyValue https://developer.mozilla.org/en-US/docs/Web/API/Window/getComputedStyle

about 4 years ago · Juan Pablo Isaza Report

0

It's helpful when you provide a code snippet to show what has already been attempted. Helps with specificity of the answer.

To give a general answer to your question, you can use ScrollTrigger to define the point at which the color changes. Define the markers where it should start changing and use properties such as coloronEnter, onLeave, onEnterBack to your callback to change the color.

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!