Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.


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!