How to use Twitter Bootstrap ScrollSpy to execute a function

余生长醉 提交于 2019-11-30 17:44:33

问题


I would like to load more data after ScrollSpy receives notification. But I don't see how to capture that event and execute a function.


回答1:


UPDATED This trigger has been pushed into the main build now. You can see it in full docs. http://twitter.github.com/bootstrap/javascript.html#scrollspy

There is an update in the working branch on github that fires an activate callback.

References

  • https://github.com/twitter/bootstrap/issues/2482

Once you grab the activate trigger, you can call it like so:

$("#nav li").on('activate', function() {
   //do stuff
});



回答2:


This is for Bootstrap 4 (beta 3):
In general, the usage of the Scrollspy event did not change from Bootstrap 3. As it is stated at the Events section of the Scrollspy documentation, the activate.bs.scrollspy event will be fired on the scroll element –the one with data-spy="scroll"– whenever a new item becomes activated by the Scrollspy. You can listen to it like that:

$('[data-spy="scroll"]').on('activate.bs.scrollspy', function(event) {
    console.log('activate.bs.scrollspy', event);
})

But!
It is not documented, that when the Scrollspy is used on the <body> element, the activate.bs.scrollspy event will be available only on the window object.
So, in this rather common case, you can catch the Scrollspy event like so:

$(window).on('activate.bs.scrollspy', function (event) {
    console.log('activate.bs.scrollspy', event);
})



回答3:


For bootstrap 3 you can do it like this (docs)

$('#myScrollspy').on('activate.bs.scrollspy', function () {
  // do something…
})


来源:https://stackoverflow.com/questions/9343715/how-to-use-twitter-bootstrap-scrollspy-to-execute-a-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!