12

I'm currently using this plugin: http://github.com/davist11/jQuery-One-Page-Nav

but would prefer to switch to using Twitter's Bootstrap.js Scrollspy: http://twitter.github.com/bootstrap/javascript.html#scrollspy

However, this doesn't offer an option to animate the scroll movement when clicked. How can I add this?

5 Answers 5

13

You should take a look at http://plugins.jquery.com/scrollTo/, it should be faily simple to combine with bootstrap. I will happily give a more detailed explination on how to implement it if you would like.

Sign up to request clarification or add additional context in comments.

8 Comments

A little more help would be great! Where exactly do I put the scrollTo command, e.g. $.scrollTo('#'+target', 1000);?
The scrollspy doesn't handle the movement to the element, this is done natively by the browser. All scroll spy does is change the selected element as the user scrolls. Here is a quick implementation of scrollTo that should help: jsfiddle.net/flabbyrabbit/69z7x
Brilliant! One final question, can I set a top-offset? Please see here for reference: jsfiddle.net/69z7x/5 So here there'd be a 200px offset.
Try this: $.scrollTo(target, 1000, {offset:-200});
no need to update any functions, you can just add data-offset="200" to the body
|
7

With or without bootstrap:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/jquery-scrollTo/1.4.3/jquery.scrollTo.min.js"></script> <script> $(function() { $('#yournav').bind('click', 'ul li a', function(event) { $.scrollTo(event.target.hash, 250); }); }); </script> 

1 Comment

To prevent flickering with scrollspy, add event.preventDefault()
3

Ok guys, no need to add any extra pointless js plugins.

Add this to body

<body data-spy="scroll" data-offset="500" data-target="#navbar"> 

Add custom.js to your theme or just use another proper file.

Add this to custom.js

// strict just to keep concept of bootstrap +function ($) { 'use strict'; // spy and scroll menu boogey $("#navbar 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: $(this.hash).offset().top -500 }, 1000, function(){ window.location.hash = hash -500 }) }) }(jQuery); 

Be sure you use <tag id="#myLink"></tag> and <a href="#myLink>

data-offset="500" in body and -500 places in function are to offset place of a scroll

1 Comment

It not animate for me. Can you help me?
2

It's a one liner with jQuery.localScroll:

$.localScroll(); 

Comments

1

This is basically an extension of the answers above but I implemented them slightly differently for a more integrated approach. I don't expect any points for originality but maybe this can help someone.

As mentioned in the answer above, add the scrollTo() script:

<script src="//cdnjs.cloudflare.com/ajax/libs/jquery-scrollTo/1.4.3/jquery.scrollTo.min.js"></script> 

In the bootstrap.js file find:

// SCROLLSPY CLASS DEFINITION // ========================== 

In the variable declarations, right under this.process() add the variable:
this.scroll()

Then right under the ScrollSpy.prototype.activate function, and above:

// SCROLLSPY PLUGIN DEFINITION // =========================== 

add your scroll() method:

ScrollSpy.prototype.scroll = function (target) { $('#spyOnThis').bind('click', 'ul li a', function(event) { $.scrollTo(event.target.hash, 250); }); }; 

This worked for me. Thanks again for helpful advice up above.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.