Reputation: 421
I've created a long horizontal page and using anchors to navigate to section's within the page. I added a jQuery smooth scrolling function but it's not taking affect?
Here's the navigation:
<ul class="nav">
<li><a href="#starters">Starters</a></li>
<li><a href="#main">Main Dishes</a></li>
<li><a href="#special">Special Dishes</a></li>
<li><a href="#side">Side Dishes</a></li>
</ul>
Within the content i've added corresponding anchors:
<a name="starters"></a>
And here's the jQuery function:
$(function() {
$('ul.nav a').bind('click',function(event){
var $anchor = $(this);
$('html, body').stop().animate({
scrollLeft: $($anchor.attr('href')).offset().left
}, 1500, "easeInOutExpo");
event.preventDefault();
});
});
The anchor's work fine, clicking the navigation takes you to the desired section but it jumps instead of scrolling.
Any ideas why??
Upvotes: 2
Views: 9249
Reputation: 379
It's simple add class to your div
which need to scroll for example below:
<ul class="nav">
<li class="a"><a href="#starters">Starters</a></li>
<li class="b"><a href="#main">Main Dishes</a></li>
<li class="c"><a href="#special">Special Dishes</a></li>
<li class="d"><a href="#side">Side Dishes</a></li>
</ul>
Now, your js will be like this:
$(function() {
$('#clickable element').bind('click',function(event){
$('#targetelement or div').stop().animate({
scrollLeft: $('.a').offset().left
}, 500);
event.preventDefault();
});
});
likewise you can build js for b,c,d classes.
Upvotes: 2
Reputation: 1
Maybe you forget to use jquery easing for animations http://matthewlein.com/experiments/easing.html
Upvotes: 0
Reputation: 1
It looks like your code is correct except for this:
<a name="starters"></a>
Change name
to id
. There is no HTML name
attribute for anchor tags as far as I know. Also, you don't need "corresponding anchors" for your sections. Assuming your sections are wrapped with a <section>
or <div>
, just give each of your wrapping block elements an id
identical to the href
from each of your anchor links. So:
<section id="starters">content</section>
Upvotes: 0