Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
25
rated 0 times [  27] [ 2]  / answers: 1 / hits: 172968  / 12 Years ago, mon, february 11, 2013, 12:00:00

I've been trying to add a smooth scrolling function to my site for a while now but can't seem to get it to work.



Here is my HTML code relating to my navigation:



<div id=nav-wrapper>
<div id=nav class=navbar navbar-inverse affix-top data-spy=affix data-offset-top=675>
<div class=navbar-inner data-spy=affix-top>
<div class=container>

<!-- .btn-navbar is used as the toggle for collapsed navbar content -->
<a class=btn btn-navbar data-toggle=collapse data-target=.nav-collapse>
<span class=icon-bar></span>
<span class=icon-bar></span>
<span class=icon-bar></span>
</a>

<!-- Everything you want hidden at 940px or less, place within here -->
<div class=nav-collapse collapse>
<ul class=nav>
<li><a href=#home>Home</a></li>
<li><a href=#service-top>Services</a></li>
<li><a href=#contact-arrow>Contact</a></li>
</ul><!--/.nav-->
</div><!--/.nav-collapse collapse pull-right-->
</div><!--/.container-->
</div><!--/.navbar-inner-->
</div><!--/#nav /.navbar navbar-inverse-->
</div><!--/#nav-wrapper-->


Here is the JS code I've added:



<script src=js/jquery.scrollTo-1.4.3.1-min.js></script>

<script>
$(document).ready(function(e) {

$('#nav').scrollSpy()
$('#nav ul li a').bind('click', function(e) {
e.preventDefault();
target = this.hash;
console.log(target);
$.scrollTo(target, 1000);
});
});
</script>


For what it's worth, here is where I received info on what I've done so far, and here is my site in it's current form. If you can help me I'll bake you a pie or cookies or something. Thanks!


More From » html

 Answers
22

Do you really need that plugin? You can just animate the scrollTop property:



$(#nav ul li a[href^='#']).on('click', function(e) {

// prevent default anchor click behavior
e.preventDefault();

// store hash
var hash = this.hash;

// animate
$('html, body').animate({
scrollTop: $(hash).offset().top
}, 300, function(){

// when done, add hash to url
// (default click behaviour)
window.location.hash = hash;
});

});


fiddle


[#80306] Saturday, February 9, 2013, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
soniap

Total Points: 626
Total Questions: 119
Total Answers: 110

Location: Palestine
Member since Tue, Jul 20, 2021
3 Years ago
soniap questions
Mon, Jun 22, 20, 00:00, 4 Years ago
Fri, May 8, 20, 00:00, 4 Years ago
Fri, Mar 20, 20, 00:00, 4 Years ago
;