Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


Fransiska Narulita
Fransiska Narulita
5,620 Points

How do I gradually change opacity of a div as I scroll?

I want to create an effect which as I scroll, the opacity change gradually depends on the scroll (value? position?). Example: when it's 1000px it's 100%, 1010px it's 90%, and at the end it will be 0% when it reach 1100px.

Currently I'm using: $('#cities_shadow').animate({opacity: 0}, 1000) But I don't want to use time duration.


1 Answer

Lorenzo Pieri
Lorenzo Pieri
19,772 Points

Hi Fransiska, you might want to check out the explanation from the jQuery API Documentation about SCROLLING EVENTS, reported in this link jQuery SCROLL API Doc.

Have a nice day!

You can also use this nice fiddle as a reference:

jQUERY scrollTop() usage