js/bootstrap-scrollspy.js
author indvd00m (gotoindvdum[at]gmail[dot]com)
Fri, 04 Jul 2014 16:42:41 +0400
changeset 0 ba8ab09f730e
permissions -rw-r--r--
First home page
indvd00m@0
     1
/* =============================================================
indvd00m@0
     2
 * bootstrap-scrollspy.js v2.3.1
indvd00m@0
     3
 * http://twitter.github.com/bootstrap/javascript.html#scrollspy
indvd00m@0
     4
 * =============================================================
indvd00m@0
     5
 * Copyright 2012 Twitter, Inc.
indvd00m@0
     6
 *
indvd00m@0
     7
 * Licensed under the Apache License, Version 2.0 (the "License");
indvd00m@0
     8
 * you may not use this file except in compliance with the License.
indvd00m@0
     9
 * You may obtain a copy of the License at
indvd00m@0
    10
 *
indvd00m@0
    11
 * http://www.apache.org/licenses/LICENSE-2.0
indvd00m@0
    12
 *
indvd00m@0
    13
 * Unless required by applicable law or agreed to in writing, software
indvd00m@0
    14
 * distributed under the License is distributed on an "AS IS" BASIS,
indvd00m@0
    15
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
indvd00m@0
    16
 * See the License for the specific language governing permissions and
indvd00m@0
    17
 * limitations under the License.
indvd00m@0
    18
 * ============================================================== */
indvd00m@0
    19
indvd00m@0
    20
indvd00m@0
    21
!function ($) {
indvd00m@0
    22
indvd00m@0
    23
  "use strict"; // jshint ;_;
indvd00m@0
    24
indvd00m@0
    25
indvd00m@0
    26
 /* SCROLLSPY CLASS DEFINITION
indvd00m@0
    27
  * ========================== */
indvd00m@0
    28
indvd00m@0
    29
  function ScrollSpy(element, options) {
indvd00m@0
    30
    var process = $.proxy(this.process, this)
indvd00m@0
    31
      , $element = $(element).is('body') ? $(window) : $(element)
indvd00m@0
    32
      , href
indvd00m@0
    33
    this.options = $.extend({}, $.fn.scrollspy.defaults, options)
indvd00m@0
    34
    this.$scrollElement = $element.on('scroll.scroll-spy.data-api', process)
indvd00m@0
    35
    this.selector = (this.options.target
indvd00m@0
    36
      || ((href = $(element).attr('href')) && href.replace(/.*(?=#[^\s]+$)/, '')) //strip for ie7
indvd00m@0
    37
      || '') + ' .nav li > a'
indvd00m@0
    38
    this.$body = $('body')
indvd00m@0
    39
    this.refresh()
indvd00m@0
    40
    this.process()
indvd00m@0
    41
  }
indvd00m@0
    42
indvd00m@0
    43
  ScrollSpy.prototype = {
indvd00m@0
    44
indvd00m@0
    45
      constructor: ScrollSpy
indvd00m@0
    46
indvd00m@0
    47
    , refresh: function () {
indvd00m@0
    48
        var self = this
indvd00m@0
    49
          , $targets
indvd00m@0
    50
indvd00m@0
    51
        this.offsets = $([])
indvd00m@0
    52
        this.targets = $([])
indvd00m@0
    53
indvd00m@0
    54
        $targets = this.$body
indvd00m@0
    55
          .find(this.selector)
indvd00m@0
    56
          .map(function () {
indvd00m@0
    57
            var $el = $(this)
indvd00m@0
    58
              , href = $el.data('target') || $el.attr('href')
indvd00m@0
    59
              , $href = /^#\w/.test(href) && $(href)
indvd00m@0
    60
            return ( $href
indvd00m@0
    61
              && $href.length
indvd00m@0
    62
              && [[ $href.position().top + (!$.isWindow(self.$scrollElement.get(0)) && self.$scrollElement.scrollTop()), href ]] ) || null
indvd00m@0
    63
          })
indvd00m@0
    64
          .sort(function (a, b) { return a[0] - b[0] })
indvd00m@0
    65
          .each(function () {
indvd00m@0
    66
            self.offsets.push(this[0])
indvd00m@0
    67
            self.targets.push(this[1])
indvd00m@0
    68
          })
indvd00m@0
    69
      }
indvd00m@0
    70
indvd00m@0
    71
    , process: function () {
indvd00m@0
    72
        var scrollTop = this.$scrollElement.scrollTop() + this.options.offset
indvd00m@0
    73
          , scrollHeight = this.$scrollElement[0].scrollHeight || this.$body[0].scrollHeight
indvd00m@0
    74
          , maxScroll = scrollHeight - this.$scrollElement.height()
indvd00m@0
    75
          , offsets = this.offsets
indvd00m@0
    76
          , targets = this.targets
indvd00m@0
    77
          , activeTarget = this.activeTarget
indvd00m@0
    78
          , i
indvd00m@0
    79
indvd00m@0
    80
        if (scrollTop >= maxScroll) {
indvd00m@0
    81
          return activeTarget != (i = targets.last()[0])
indvd00m@0
    82
            && this.activate ( i )
indvd00m@0
    83
        }
indvd00m@0
    84
indvd00m@0
    85
        for (i = offsets.length; i--;) {
indvd00m@0
    86
          activeTarget != targets[i]
indvd00m@0
    87
            && scrollTop >= offsets[i]
indvd00m@0
    88
            && (!offsets[i + 1] || scrollTop <= offsets[i + 1])
indvd00m@0
    89
            && this.activate( targets[i] )
indvd00m@0
    90
        }
indvd00m@0
    91
      }
indvd00m@0
    92
indvd00m@0
    93
    , activate: function (target) {
indvd00m@0
    94
        var active
indvd00m@0
    95
          , selector
indvd00m@0
    96
indvd00m@0
    97
        this.activeTarget = target
indvd00m@0
    98
indvd00m@0
    99
        $(this.selector)
indvd00m@0
   100
          .parent('.active')
indvd00m@0
   101
          .removeClass('active')
indvd00m@0
   102
indvd00m@0
   103
        selector = this.selector
indvd00m@0
   104
          + '[data-target="' + target + '"],'
indvd00m@0
   105
          + this.selector + '[href="' + target + '"]'
indvd00m@0
   106
indvd00m@0
   107
        active = $(selector)
indvd00m@0
   108
          .parent('li')
indvd00m@0
   109
          .addClass('active')
indvd00m@0
   110
indvd00m@0
   111
        if (active.parent('.dropdown-menu').length)  {
indvd00m@0
   112
          active = active.closest('li.dropdown').addClass('active')
indvd00m@0
   113
        }
indvd00m@0
   114
indvd00m@0
   115
        active.trigger('activate')
indvd00m@0
   116
      }
indvd00m@0
   117
indvd00m@0
   118
  }
indvd00m@0
   119
indvd00m@0
   120
indvd00m@0
   121
 /* SCROLLSPY PLUGIN DEFINITION
indvd00m@0
   122
  * =========================== */
indvd00m@0
   123
indvd00m@0
   124
  var old = $.fn.scrollspy
indvd00m@0
   125
indvd00m@0
   126
  $.fn.scrollspy = function (option) {
indvd00m@0
   127
    return this.each(function () {
indvd00m@0
   128
      var $this = $(this)
indvd00m@0
   129
        , data = $this.data('scrollspy')
indvd00m@0
   130
        , options = typeof option == 'object' && option
indvd00m@0
   131
      if (!data) $this.data('scrollspy', (data = new ScrollSpy(this, options)))
indvd00m@0
   132
      if (typeof option == 'string') data[option]()
indvd00m@0
   133
    })
indvd00m@0
   134
  }
indvd00m@0
   135
indvd00m@0
   136
  $.fn.scrollspy.Constructor = ScrollSpy
indvd00m@0
   137
indvd00m@0
   138
  $.fn.scrollspy.defaults = {
indvd00m@0
   139
    offset: 10
indvd00m@0
   140
  }
indvd00m@0
   141
indvd00m@0
   142
indvd00m@0
   143
 /* SCROLLSPY NO CONFLICT
indvd00m@0
   144
  * ===================== */
indvd00m@0
   145
indvd00m@0
   146
  $.fn.scrollspy.noConflict = function () {
indvd00m@0
   147
    $.fn.scrollspy = old
indvd00m@0
   148
    return this
indvd00m@0
   149
  }
indvd00m@0
   150
indvd00m@0
   151
indvd00m@0
   152
 /* SCROLLSPY DATA-API
indvd00m@0
   153
  * ================== */
indvd00m@0
   154
indvd00m@0
   155
  $(window).on('load', function () {
indvd00m@0
   156
    $('[data-spy="scroll"]').each(function () {
indvd00m@0
   157
      var $spy = $(this)
indvd00m@0
   158
      $spy.scrollspy($spy.data())
indvd00m@0
   159
    })
indvd00m@0
   160
  })
indvd00m@0
   161
indvd00m@0
   162
}(window.jQuery);