I am using Bootstrap 3.2.0. I am trying to use two bootstrap javascript features together: Scrollspy and Affix. The goal is to make a scrollspy on the side that will scroll with the rest of the page using affix. Demo
Below is my javascript file, included in the head
by Rails asset pipeline.
$(document).ready(function () {
// Use affix plugin
$("#sidebar").affix({
offset: { top: 280 }
});
// Use Scrollspy
$('body').scrollspy({ target: '#sidebar', offset: 200 });
}
The scrollspy works smoothly, but affix plugin does not, unless I refresh the page after initial loading. Using developer tools in chrome, I noticed that the javascript does not add affix-top
to sidebar
the first time page loads.
When I put the affix plugin javascript directly into the body, it works without refreshing. But I don't really want to use inline javascript. And I am really curious why this strange behavior is happening.
I am using Rails 4.1.5.