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

JavaScript

Francesco Paolini
Francesco Paolini
9,910 Points

to top button (jQuery)

I'm trying to create a to top button, it should show after scrollPosition became > 1500, but it doesn't seems to work

let scrollPosition = $(document).scrollTop();

$('.to-top-btn').hide();

$(document).on('scroll', ()=>{ if(scrollPosition > 1500){ $('.to-top-btn').fadeIn(); }; } )

1 Answer

Steven Parker
Steven Parker
231,153 Points

Instead of storing the original "scrollPosition" (which never changes), use "scrollTop" inside the handler to get the current location at the time of the event.