I’m currently working on a project with a one page design that’ll slide up and down between sections on an <a href> link…
Currently, i have it written as follows:
<ul>
<li><a href="javascript:void(0)" onClick="goToByScroll('top')">home</a></li>
<li><a href="javascript:void(0)" onClick="goToByScroll('artistsmaterials')">artist's materials</a></li>
<li><a href="javascript:void(0)" onClick="goToByScroll('pictureframing')">picture framing</a></li>
<li><a href="javascript:void(0)" onClick="goToByScroll('gallery')">gallery</a></li>
<li><a href="javascript:void(0)" onClick="goToByScroll('contactus')">contact us</a></li>
</ul>
…with the most relevant portion being the links:
<a href="javascript:void(0)" onClick="goToByScroll('contactus')">
Then in a .js file I have:
function goToByScroll(id){
$('html,body').animate({scrollTop: $("#"+id).offset().top},'slow');
}
Is this ok? Or should this be done a different way?
A better approach would be to not use anchors in this case, you can simplify your markup and code like this:
And then this script:
This adds a click handler to your
<li>elements telling them to use theirrel=""property to know where to scroll to. Just change your CSS around to point to the<li>elements instead of<a>and you’re all set.