scrollspy.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /* ========================================================================
  2. * Bootstrap: scrollspy.js v3.2.0
  3. * http://getbootstrap.com/javascript/#scrollspy
  4. * ========================================================================
  5. * Copyright 2011-2014 Twitter, Inc.
  6. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
  7. * ======================================================================== */
  8. +function ($) {
  9. 'use strict';
  10. // SCROLLSPY CLASS DEFINITION
  11. // ==========================
  12. function ScrollSpy(element, options) {
  13. var process = $.proxy(this.process, this)
  14. this.$body = $('body')
  15. this.$scrollElement = $(element).is('body') ? $(window) : $(element)
  16. this.options = $.extend({}, ScrollSpy.DEFAULTS, options)
  17. this.selector = (this.options.target || '') + ' .nav li > a'
  18. this.offsets = []
  19. this.targets = []
  20. this.activeTarget = null
  21. this.scrollHeight = 0
  22. this.$scrollElement.on('scroll.bs.scrollspy', process)
  23. this.refresh()
  24. this.process()
  25. }
  26. ScrollSpy.VERSION = '3.2.0'
  27. ScrollSpy.DEFAULTS = {
  28. offset: 10
  29. }
  30. ScrollSpy.prototype.getScrollHeight = function () {
  31. return this.$scrollElement[0].scrollHeight || Math.max(this.$body[0].scrollHeight, document.documentElement.scrollHeight)
  32. }
  33. ScrollSpy.prototype.refresh = function () {
  34. var offsetMethod = 'offset'
  35. var offsetBase = 0
  36. if (!$.isWindow(this.$scrollElement[0])) {
  37. offsetMethod = 'position'
  38. offsetBase = this.$scrollElement.scrollTop()
  39. }
  40. this.offsets = []
  41. this.targets = []
  42. this.scrollHeight = this.getScrollHeight()
  43. var self = this
  44. this.$body
  45. .find(this.selector)
  46. .map(function () {
  47. var $el = $(this)
  48. var href = $el.data('target') || $el.attr('href')
  49. var $href = /^#./.test(href) && $(href)
  50. return ($href
  51. && $href.length
  52. && $href.is(':visible')
  53. && [[$href[offsetMethod]().top + offsetBase, href]]) || null
  54. })
  55. .sort(function (a, b) { return a[0] - b[0] })
  56. .each(function () {
  57. self.offsets.push(this[0])
  58. self.targets.push(this[1])
  59. })
  60. }
  61. ScrollSpy.prototype.process = function () {
  62. var scrollTop = this.$scrollElement.scrollTop() + this.options.offset
  63. var scrollHeight = this.getScrollHeight()
  64. var maxScroll = this.options.offset + scrollHeight - this.$scrollElement.height()
  65. var offsets = this.offsets
  66. var targets = this.targets
  67. var activeTarget = this.activeTarget
  68. var i
  69. if (this.scrollHeight != scrollHeight) {
  70. this.refresh()
  71. }
  72. if (scrollTop >= maxScroll) {
  73. return activeTarget != (i = targets[targets.length - 1]) && this.activate(i)
  74. }
  75. if (activeTarget && scrollTop <= offsets[0]) {
  76. return activeTarget != (i = targets[0]) && this.activate(i)
  77. }
  78. for (i = offsets.length; i--;) {
  79. activeTarget != targets[i]
  80. && scrollTop >= offsets[i]
  81. && (!offsets[i + 1] || scrollTop <= offsets[i + 1])
  82. && this.activate(targets[i])
  83. }
  84. }
  85. ScrollSpy.prototype.activate = function (target) {
  86. this.activeTarget = target
  87. $(this.selector)
  88. .parentsUntil(this.options.target, '.active')
  89. .removeClass('active')
  90. var selector = this.selector +
  91. '[data-target="' + target + '"],' +
  92. this.selector + '[href="' + target + '"]'
  93. var active = $(selector)
  94. .parents('li')
  95. .addClass('active')
  96. if (active.parent('.dropdown-menu').length) {
  97. active = active
  98. .closest('li.dropdown')
  99. .addClass('active')
  100. }
  101. active.trigger('activate.bs.scrollspy')
  102. }
  103. // SCROLLSPY PLUGIN DEFINITION
  104. // ===========================
  105. function Plugin(option) {
  106. return this.each(function () {
  107. var $this = $(this)
  108. var data = $this.data('bs.scrollspy')
  109. var options = typeof option == 'object' && option
  110. if (!data) $this.data('bs.scrollspy', (data = new ScrollSpy(this, options)))
  111. if (typeof option == 'string') data[option]()
  112. })
  113. }
  114. var old = $.fn.scrollspy
  115. $.fn.scrollspy = Plugin
  116. $.fn.scrollspy.Constructor = ScrollSpy
  117. // SCROLLSPY NO CONFLICT
  118. // =====================
  119. $.fn.scrollspy.noConflict = function () {
  120. $.fn.scrollspy = old
  121. return this
  122. }
  123. // SCROLLSPY DATA-API
  124. // ==================
  125. $(window).on('load.bs.scrollspy.data-api', function () {
  126. $('[data-spy="scroll"]').each(function () {
  127. var $spy = $(this)
  128. Plugin.call($spy, $spy.data())
  129. })
  130. })
  131. }(jQuery);