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