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.

172 lines
4.6 KiB

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