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

Sam Daugherty
Sam Daugherty
1,876 Points

jQuery scrollTop not scrolling far enough on mobile

I'm very new to JS and jQuery. Forgive me if my code isn't the best, but I'm still working through the modules.

I created a page where I have a bunch of hidden divs. When I click a link, I want to make the div visible, and scroll to it. I achieved it like this: $("#portOne").click(function() { $("#proj1").fadeToggle("slow").toggleClass("hidden"); $("#proj2").addClass("hidden"); $("#proj3").addClass("hidden"); $("#proj4").addClass("hidden"); $("#proj5").addClass("hidden"); $("#proj6").addClass("hidden"); $("#proj7").addClass("hidden"); $("#proj8").addClass("hidden"); $("html, body").animate({ scrollTop: $("#proj1").offset().top + 200 }, 500); })

I'm guessing it scrolls to the top of the page, and then moves down 200px? That's fine on my laptop, but on mobile, it doesn't scroll far enough. It doesn't actually scroll to the div, because the div is now further down the page than 200px.

What's the solution for this? What am I missing? Please help!