You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

107 line
2.9 KiB

  1. /* =============================================================
  2. * bootstrap-scrollspy.js v1.4.0
  3. * http://twitter.github.com/bootstrap/javascript.html#scrollspy
  4. * =============================================================
  5. * Copyright 2011 Twitter, Inc.
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. * ============================================================== */
  19. !function ( $ ) {
  20. "use strict"
  21. var $window = $(window)
  22. function ScrollSpy( topbar, selector ) {
  23. var processScroll = $.proxy(this.processScroll, this)
  24. this.$topbar = $(topbar)
  25. this.selector = selector || 'li > a'
  26. this.refresh()
  27. this.$topbar.delegate(this.selector, 'click', processScroll)
  28. $window.scroll(processScroll)
  29. this.processScroll()
  30. }
  31. ScrollSpy.prototype = {
  32. refresh: function () {
  33. this.targets = this.$topbar.find(this.selector).map(function () {
  34. var href = $(this).attr('href')
  35. return /^#\w/.test(href) && $(href).length ? href : null
  36. })
  37. this.offsets = $.map(this.targets, function (id) {
  38. return $(id).offset().top
  39. })
  40. }
  41. , processScroll: function () {
  42. var scrollTop = $window.scrollTop() + 10
  43. , offsets = this.offsets
  44. , targets = this.targets
  45. , activeTarget = this.activeTarget
  46. , i
  47. for (i = offsets.length; i--;) {
  48. activeTarget != targets[i]
  49. && scrollTop >= offsets[i]
  50. && (!offsets[i + 1] || scrollTop <= offsets[i + 1])
  51. && this.activateButton( targets[i] )
  52. }
  53. }
  54. , activateButton: function (target) {
  55. this.activeTarget = target
  56. this.$topbar
  57. .find(this.selector).parent('.active')
  58. .removeClass('active')
  59. this.$topbar
  60. .find(this.selector + '[href="' + target + '"]')
  61. .parent('li')
  62. .addClass('active')
  63. }
  64. }
  65. /* SCROLLSPY PLUGIN DEFINITION
  66. * =========================== */
  67. $.fn.scrollSpy = function( options ) {
  68. var scrollspy = this.data('scrollspy')
  69. if (!scrollspy) {
  70. return this.each(function () {
  71. $(this).data('scrollspy', new ScrollSpy( this, options ))
  72. })
  73. }
  74. if ( options === true ) {
  75. return scrollspy
  76. }
  77. if ( typeof options == 'string' ) {
  78. scrollspy[options]()
  79. }
  80. return this
  81. }
  82. $(document).ready(function () {
  83. $('body').scrollSpy('[data-scrollspy] li > a')
  84. })
  85. }( window.jQuery || window.ender );