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 trialmelissa brown
4,670 Pointswhat if you wanted it to scroll slowing to the top of the page?
for example some websites have that example when you click on the first page and it slow scrolls down to the start of the second page. how would we do this?
3 Answers
Elwin de Witte
1,357 PointsI mostly use a short script, if you have this in your head and you navigate to an ID attribute with an href="" it will slowly scroll there.
Example:
<html> <head> <title>Sample</title>
<!-- Smooth Scroll -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$(function() {
$('a[href*=#]:not([href=#])').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
$('html,body').animate({
scrollTop: target.offset().top
}, 1000);
return false;
}
}
});
});
</script>
</head>
<body>
<header id="top">
<a href="#section1">Next section</a>
</header>
<section id="section1">
<a href="#section2">Next section</a>
</section>
<section id="section2">
<a href="#footer">Next section</a>
</section>
<footer id="footer">
<a href="#top">Back to top</a>
</footer>
</body>
</html>
The "1000" in the script is the speed in ms, you can change this. Sample website where this is used: http://insider.harwindesigners.nl/dekruidendokter (when you click the arrow you scroll down)
Is this what you asked?
Unsubscribed User
5,512 PointsAwesome solution, Elwin. For some more neat scrolling effects, check out http://cferdinandi.github.io/smooth-scroll/
Elwin de Witte
1,357 PointsThanks Stuart!
melissa brown
4,670 Pointsawesome thanks it totally worked