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
Emeka Okoye
2,489 PointsscrollTop issue - non track related
Last month I created a one-page site and use the scrollTop function to create a nice animated scroll on the page to various sections.
I now need to expand the website to create other pages. This is not a problem but I've got a sticky menu with a nav that contains links that don't work on any other page but the home page.
How can I make it work in such a way that when one of the scrollTop links is clicked, it first returns to the homepage and then scrolls to the relevant section?
Here's my JS
$(function(){
$(".navbar-nav li a").on("click", function(e) {
$('html, body').animate({
scrollTop : $($(this).data('target')).offset().top - 68
});
e.preventDefault();
return false;
});
Here's the corresponding HTML
<ul class="nav navbar-nav">
<li><a href="#" data-target="#about">About</a></li>
<li><a href="#" data-target="#products">Products</a></li>
<li><a href="#" data-target="#order">How to Order</a></li>
<li><a href="#" data-target="#contact">Contact</a></li>
</ul>
1 Answer
dorzki
4,312 Pointstake your code and wrap it with a function, then use this:
$(document).ready(function (){
if (location.hash){
setTimeout(function(){
$('html, body').scrollTop(0).show();
// call the function which do smooth scroll
}, 0);
}else{
$('html, body').show();
}
});
taken from here: http://stackoverflow.com/questions/9652944/jquery-page-scroll-to-different-page