PageSourceSearch

https://xiechao1211.github.io/js/src/scrollspy.js?v=5.1.4

js xiechao1211.github.io collected 2026-10-03 10:11:45 UTC 4,925 bytes, 182 lines download raw bytes

vendor: 4,925 bytes, lines 1-182
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
9/**
10 * Custom by iissnan
11 *
12 * - Add a `clear.bs.scrollspy` event.
13 * - Esacpe targets selector.
14 */
15
16
17+function ($) {
18  'use strict';
19
20  // SCROLLSPY CLASS DEFINITION
21  // ==========================
22
23  function ScrollSpy(element, options) {
24    this.$body          = $(document.body)
25    this.$scrollElement = $(element).is(document.body) ? $(window) : $(element)
26    this.options        = $.extend({}, ScrollSpy.DEFAULTS, options)
27    this.selector       = (this.options.target || '') + ' .nav li > a'
28    this.offsets        = []
29    this.targets        = []
30    this.activeTarget   = null
31    this.scrollHeight   = 0
32
33    this.$scrollElement.on('scroll.bs.scrollspy', $.proxy(this.process, this))
34    this.refresh()
35    this.process()
36  }
37
38  ScrollSpy.VERSION  = '3.3.2'
39
40  ScrollSpy.DEFAULTS = {
41    offset: 10
42  }
43
44  ScrollSpy.prototype.getScrollHeight = function () {
45    return this.$scrollElement[0].scrollHeight || Math.max(this.$body[0].scrollHeight, document.documentElement.scrollHeight)
46  }
47
48  ScrollSpy.prototype.refresh = function () {
49    var that          = this
50    var offsetMethod  = 'offset'
51    var offsetBase    = 0
52
53    this.offsets      = []
54    this.targets      = []
55    this.scrollHeight = this.getScrollHeight()
56
57    if (!$.isWindow(this.$scrollElement[0])) {
58      offsetMethod = 'position'
59      offsetBase   = this.$scrollElement.scrollTop()
60    }
61
62    this.$body
63      .find(this.selector)
64      .map(function () {
65        var $el   = $(this)
66        var href  = $el.data('target') || $el.attr('href')
67        var $href = /^#./.test(href) && $(NexT.utils.escapeSelector(href)) // Need to escape selector.
68
69        return ($href
70          && $href.length
71          && $href.is(':visible')
72          && [[$href[offsetMethod]().top + offsetBase, href]]) || null
73      })
74      .sort(function (a, b) { return a[0] - b[0] })
75      .each(function () {
76        that.offsets.push(this[0])
77        that.targets.push(this[1])
78      })
79
80
81  }
82
83  ScrollSpy.prototype.process = function () {
84    var scrollTop    = this.$scrollElement.scrollTop() + this.options.offset
85    var scrollHeight = this.getScrollHeight()
86    var maxScroll    = this.options.offset + scrollHeight - this.$scrollElement.height()
87    var offsets      = this.offsets
88    var targets      = this.targets
89    var activeTarget = this.activeTarget
90    var i
91
92    if (this.scrollHeight != scrollHeight) {
93      this.refresh()
94    }
95
96    if (scrollTop >= maxScroll) {
97      return activeTarget != (i = targets[targets.length - 1]) && this.activate(i)
98    }
99
100    if (activeTarget && scrollTop < offsets[0]) {
101      $(this.selector).trigger('clear.bs.scrollspy')  // Add a custom event.
102      this.activeTarget = null
103      return this.clear()
104    }
105
106    for (i = offsets.length; i--;) {
107      activeTarget != targets[i]
108        && scrollTop >= offsets[i]
109        && (!offsets[i + 1] || scrollTop <= offsets[i + 1])
110        && this.activate(targets[i])
111    }
112  }
113
114  ScrollSpy.prototype.activate = function (target) {
115    this.activeTarget = target
116
117    this.clear()
118
119    var selector = this.selector +
120      '[data-target="' + target + '"],' +
121      this.selector + '[href="' + target + '"]'
122
123    var active = $(selector)
124      .parents('li')
125      .addClass('active')
126
127    if (active.parent('.dropdown-menu').length) {
128      active = active
129        .closest('li.dropdown')
130        .addClass('active')
131    }
132
133    active.trigger('activate.bs.scrollspy')
134  }
135
136  ScrollSpy.prototype.clear = function () {
137    $(this.selector)
138      .parentsUntil(this.options.target, '.active')
139      .removeClass('active')
140  }
141
142
143  // SCROLLSPY PLUGIN DEFINITION
144  // ===========================
145
146  function Plugin(option) {
147    return this.each(function () {
148      var $this   = $(this)
149      var data    = $this.data('bs.scrollspy')
150      var options = typeof option == 'object' && option
151
152      if (!data) $this.data('bs.scrollspy', (data = new ScrollSpy(this, options)))
153      if (typeof option == 'string') data[option]()
154    })
155  }
156
157  var old = $.fn.scrollspy
158
159  $.fn.scrollspy             = Plugin
160  $.fn.scrollspy.Constructor = ScrollSpy
161
162
163  // SCROLLSPY NO CONFLICT
164  // =====================
165
166  $.fn.scrollspy.noConflict = function () {
167    $.fn.scrollspy = old
168    return this
169  }
170
171
172  // SCROLLSPY DATA-API
173  // ==================
174
175  $(window).on('load.bs.scrollspy.data-api', function () {
176    $('[data-spy="scroll"]').each(function () {
177      var $spy = $(this)
178      Plugin.call($spy, $spy.data())
179    })
180  })
181
182}(jQuery);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.