How can I make it so when you click on a link, it scrolls to a div on the current page. Im trying to make a one-page website (all of the sections will be located on one page)
P;S Heres a example of what im trying to acomplish: http://aresiio.com/
How can I make it so when you click on a link, it scrolls to a div on the current page. Im trying to make a one-page website (all of the sections will be located on one page)
P;S Heres a example of what im trying to acomplish: http://aresiio.com/
Like this:
<a href="#placeToScroll">Bananas</a>
<div id="placeToScroll">All my lovely content</div>
In your example, you need to make sure your navigation is set to position:fixed.
This will make your page sort of jump to the section. If you want to add a little animation to make things smooth you can use jQuery:
$("a").on("click", function() {
href = $(this).attr('href');
$(document.body).animate({
'scrollTop': $(href).offset().top;
}, 1000);
});
The preferred way of doing this is with named anchors, not Javascript or CSS.
If you put an anchor element like <a id="section-1"></a>
before your div, you can link to it using <a href="#section-1">Jump to Section 1</a>
and the browser will scroll there when the link is clicked.
This will also allow bookmarks to link directly to that section.
you can do that with simple html anchor tag or jquery
Every other answer so far is talking about HTML anchor links to named sections of the site, e.g. <a href="#stuff">stuff</a>
linking to <div id="stuff">more stuff</div>
on the same page.
This does work but it will "jump" directly to the element rather than scroll like the example you linked to. For that functionality you should use a jQuery plugin like the very popular scrollTo.