scrollspy.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* ========================================================================
  2. * Bootstrap: scrollspy.js v3.3.2
  3. * http://getbootstrap.com/javascript/#scrollspy
  4. * ========================================================================
  5. * Copyright 2011-2015 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.3.2'
  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. this.activeTarget = null
  77. return this.clear()
  78. }
  79. for (i = offsets.length; i--;) {
  80. activeTarget != targets[i]
  81. && scrollTop >= offsets[i]
  82. && (!offsets[i + 1] || scrollTop <= offsets[i + 1])
  83. && this.activate(targets[i])
  84. }
  85. }
  86. ScrollSpy.prototype.activate = function (target) {
  87. this.activeTarget = target
  88. this.clear()
  89. var selector = this.selector +
  90. '[data-target="' + target + '"],' +
  91. this.selector + '[href="' + target + '"]'
  92. var active = $(selector)
  93. .parents('li')
  94. .addClass('active')
  95. if (active.parent('.dropdown-menu').length) {
  96. active = active
  97. .closest('li.dropdown')
  98. .addClass('active')
  99. }
  100. active.trigger('activate.bs.scrollspy')
  101. }
  102. ScrollSpy.prototype.clear = function () {
  103. $(this.selector)
  104. .parentsUntil(this.options.target, '.active')
  105. .removeClass('active')
  106. }
  107. // SCROLLSPY PLUGIN DEFINITION
  108. // ===========================
  109. function Plugin(option) {
  110. return this.each(function () {
  111. var $this = $(this)
  112. var data = $this.data('bs.scrollspy')
  113. var options = typeof option == 'object' && option
  114. if (!data) $this.data('bs.scrollspy', (data = new ScrollSpy(this, options)))
  115. if (typeof option == 'string') data[option]()
  116. })
  117. }
  118. var old = $.fn.scrollspy
  119. $.fn.scrollspy = Plugin
  120. $.fn.scrollspy.Constructor = ScrollSpy
  121. // SCROLLSPY NO CONFLICT
  122. // =====================
  123. $.fn.scrollspy.noConflict = function () {
  124. $.fn.scrollspy = old
  125. return this
  126. }
  127. // SCROLLSPY DATA-API
  128. // ==================
  129. $(window).on('load.bs.scrollspy.data-api', function () {
  130. $('[data-spy="scroll"]').each(function () {
  131. var $spy = $(this)
  132. Plugin.call($spy, $spy.data())
  133. })
  134. })
  135. }(jQuery);