js/bootstrap-scrollspy.js
changeset 0 ba8ab09f730e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/bootstrap-scrollspy.js	Fri Jul 04 16:42:41 2014 +0400
     1.3 @@ -0,0 +1,162 @@
     1.4 +/* =============================================================
     1.5 + * bootstrap-scrollspy.js v2.3.1
     1.6 + * http://twitter.github.com/bootstrap/javascript.html#scrollspy
     1.7 + * =============================================================
     1.8 + * Copyright 2012 Twitter, Inc.
     1.9 + *
    1.10 + * Licensed under the Apache License, Version 2.0 (the "License");
    1.11 + * you may not use this file except in compliance with the License.
    1.12 + * You may obtain a copy of the License at
    1.13 + *
    1.14 + * http://www.apache.org/licenses/LICENSE-2.0
    1.15 + *
    1.16 + * Unless required by applicable law or agreed to in writing, software
    1.17 + * distributed under the License is distributed on an "AS IS" BASIS,
    1.18 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    1.19 + * See the License for the specific language governing permissions and
    1.20 + * limitations under the License.
    1.21 + * ============================================================== */
    1.22 +
    1.23 +
    1.24 +!function ($) {
    1.25 +
    1.26 +  "use strict"; // jshint ;_;
    1.27 +
    1.28 +
    1.29 + /* SCROLLSPY CLASS DEFINITION
    1.30 +  * ========================== */
    1.31 +
    1.32 +  function ScrollSpy(element, options) {
    1.33 +    var process = $.proxy(this.process, this)
    1.34 +      , $element = $(element).is('body') ? $(window) : $(element)
    1.35 +      , href
    1.36 +    this.options = $.extend({}, $.fn.scrollspy.defaults, options)
    1.37 +    this.$scrollElement = $element.on('scroll.scroll-spy.data-api', process)
    1.38 +    this.selector = (this.options.target
    1.39 +      || ((href = $(element).attr('href')) && href.replace(/.*(?=#[^\s]+$)/, '')) //strip for ie7
    1.40 +      || '') + ' .nav li > a'
    1.41 +    this.$body = $('body')
    1.42 +    this.refresh()
    1.43 +    this.process()
    1.44 +  }
    1.45 +
    1.46 +  ScrollSpy.prototype = {
    1.47 +
    1.48 +      constructor: ScrollSpy
    1.49 +
    1.50 +    , refresh: function () {
    1.51 +        var self = this
    1.52 +          , $targets
    1.53 +
    1.54 +        this.offsets = $([])
    1.55 +        this.targets = $([])
    1.56 +
    1.57 +        $targets = this.$body
    1.58 +          .find(this.selector)
    1.59 +          .map(function () {
    1.60 +            var $el = $(this)
    1.61 +              , href = $el.data('target') || $el.attr('href')
    1.62 +              , $href = /^#\w/.test(href) && $(href)
    1.63 +            return ( $href
    1.64 +              && $href.length
    1.65 +              && [[ $href.position().top + (!$.isWindow(self.$scrollElement.get(0)) && self.$scrollElement.scrollTop()), href ]] ) || null
    1.66 +          })
    1.67 +          .sort(function (a, b) { return a[0] - b[0] })
    1.68 +          .each(function () {
    1.69 +            self.offsets.push(this[0])
    1.70 +            self.targets.push(this[1])
    1.71 +          })
    1.72 +      }
    1.73 +
    1.74 +    , process: function () {
    1.75 +        var scrollTop = this.$scrollElement.scrollTop() + this.options.offset
    1.76 +          , scrollHeight = this.$scrollElement[0].scrollHeight || this.$body[0].scrollHeight
    1.77 +          , maxScroll = scrollHeight - this.$scrollElement.height()
    1.78 +          , offsets = this.offsets
    1.79 +          , targets = this.targets
    1.80 +          , activeTarget = this.activeTarget
    1.81 +          , i
    1.82 +
    1.83 +        if (scrollTop >= maxScroll) {
    1.84 +          return activeTarget != (i = targets.last()[0])
    1.85 +            && this.activate ( i )
    1.86 +        }
    1.87 +
    1.88 +        for (i = offsets.length; i--;) {
    1.89 +          activeTarget != targets[i]
    1.90 +            && scrollTop >= offsets[i]
    1.91 +            && (!offsets[i + 1] || scrollTop <= offsets[i + 1])
    1.92 +            && this.activate( targets[i] )
    1.93 +        }
    1.94 +      }
    1.95 +
    1.96 +    , activate: function (target) {
    1.97 +        var active
    1.98 +          , selector
    1.99 +
   1.100 +        this.activeTarget = target
   1.101 +
   1.102 +        $(this.selector)
   1.103 +          .parent('.active')
   1.104 +          .removeClass('active')
   1.105 +
   1.106 +        selector = this.selector
   1.107 +          + '[data-target="' + target + '"],'
   1.108 +          + this.selector + '[href="' + target + '"]'
   1.109 +
   1.110 +        active = $(selector)
   1.111 +          .parent('li')
   1.112 +          .addClass('active')
   1.113 +
   1.114 +        if (active.parent('.dropdown-menu').length)  {
   1.115 +          active = active.closest('li.dropdown').addClass('active')
   1.116 +        }
   1.117 +
   1.118 +        active.trigger('activate')
   1.119 +      }
   1.120 +
   1.121 +  }
   1.122 +
   1.123 +
   1.124 + /* SCROLLSPY PLUGIN DEFINITION
   1.125 +  * =========================== */
   1.126 +
   1.127 +  var old = $.fn.scrollspy
   1.128 +
   1.129 +  $.fn.scrollspy = function (option) {
   1.130 +    return this.each(function () {
   1.131 +      var $this = $(this)
   1.132 +        , data = $this.data('scrollspy')
   1.133 +        , options = typeof option == 'object' && option
   1.134 +      if (!data) $this.data('scrollspy', (data = new ScrollSpy(this, options)))
   1.135 +      if (typeof option == 'string') data[option]()
   1.136 +    })
   1.137 +  }
   1.138 +
   1.139 +  $.fn.scrollspy.Constructor = ScrollSpy
   1.140 +
   1.141 +  $.fn.scrollspy.defaults = {
   1.142 +    offset: 10
   1.143 +  }
   1.144 +
   1.145 +
   1.146 + /* SCROLLSPY NO CONFLICT
   1.147 +  * ===================== */
   1.148 +
   1.149 +  $.fn.scrollspy.noConflict = function () {
   1.150 +    $.fn.scrollspy = old
   1.151 +    return this
   1.152 +  }
   1.153 +
   1.154 +
   1.155 + /* SCROLLSPY DATA-API
   1.156 +  * ================== */
   1.157 +
   1.158 +  $(window).on('load', function () {
   1.159 +    $('[data-spy="scroll"]').each(function () {
   1.160 +      var $spy = $(this)
   1.161 +      $spy.scrollspy($spy.data())
   1.162 +    })
   1.163 +  })
   1.164 +
   1.165 +}(window.jQuery);
   1.166 \ No newline at end of file