bootstrap.js 60 KB
Newer Older
2001
 * ======================================================================== */
Mark Otto's avatar
Mark Otto committed
2002
2003


XhmikosR's avatar
XhmikosR committed
2004
2005
+function ($) {
  'use strict';
Mark Otto's avatar
grunt    
Mark Otto committed
2006

XhmikosR's avatar
XhmikosR committed
2007
2008
  // AFFIX CLASS DEFINITION
  // ======================
Mark Otto's avatar
Mark Otto committed
2009

XhmikosR's avatar
XhmikosR committed
2010
2011
  var Affix = function (element, options) {
    this.options = $.extend({}, Affix.DEFAULTS, options)
Mark Otto's avatar
Mark Otto committed
2012

XhmikosR's avatar
XhmikosR committed
2013
2014
2015
    this.$target = $(this.options.target)
      .on('scroll.bs.affix.data-api', $.proxy(this.checkPosition, this))
      .on('click.bs.affix.data-api',  $.proxy(this.checkPositionWithEventLoop, this))
Chris Rebert's avatar
Chris Rebert committed
2016

XhmikosR's avatar
XhmikosR committed
2017
2018
2019
2020
    this.$element     = $(element)
    this.affixed      =
    this.unpin        =
    this.pinnedOffset = null
fat's avatar
fat committed
2021

XhmikosR's avatar
XhmikosR committed
2022
2023
    this.checkPosition()
  }
fat's avatar
fat committed
2024

Mark Otto's avatar
Mark Otto committed
2025
  Affix.VERSION  = '3.2.0'
2026

XhmikosR's avatar
XhmikosR committed
2027
  Affix.RESET    = 'affix affix-top affix-bottom'
Mark Otto's avatar
grunt    
Mark Otto committed
2028

XhmikosR's avatar
XhmikosR committed
2029
2030
2031
2032
  Affix.DEFAULTS = {
    offset: 0,
    target: window
  }
2033

XhmikosR's avatar
XhmikosR committed
2034
2035
2036
2037
2038
2039
2040
  Affix.prototype.getPinnedOffset = function () {
    if (this.pinnedOffset) return this.pinnedOffset
    this.$element.removeClass(Affix.RESET).addClass('affix')
    var scrollTop = this.$target.scrollTop()
    var position  = this.$element.offset()
    return (this.pinnedOffset = position.top - scrollTop)
  }
2041

XhmikosR's avatar
XhmikosR committed
2042
2043
2044
  Affix.prototype.checkPositionWithEventLoop = function () {
    setTimeout($.proxy(this.checkPosition, this), 1)
  }
fat's avatar
fat committed
2045

XhmikosR's avatar
XhmikosR committed
2046
2047
  Affix.prototype.checkPosition = function () {
    if (!this.$element.is(':visible')) return
2048

XhmikosR's avatar
XhmikosR committed
2049
2050
2051
2052
2053
2054
    var scrollHeight = $(document).height()
    var scrollTop    = this.$target.scrollTop()
    var position     = this.$element.offset()
    var offset       = this.options.offset
    var offsetTop    = offset.top
    var offsetBottom = offset.bottom
2055

XhmikosR's avatar
XhmikosR committed
2056
2057
2058
    if (typeof offset != 'object')         offsetBottom = offsetTop = offset
    if (typeof offsetTop == 'function')    offsetTop    = offset.top(this.$element)
    if (typeof offsetBottom == 'function') offsetBottom = offset.bottom(this.$element)
2059

XhmikosR's avatar
XhmikosR committed
2060
2061
2062
    var affix = this.unpin   != null && (scrollTop + this.unpin <= position.top) ? false :
                offsetBottom != null && (position.top + this.$element.height() >= scrollHeight - offsetBottom) ? 'bottom' :
                offsetTop    != null && (scrollTop <= offsetTop) ? 'top' : false
2063

XhmikosR's avatar
XhmikosR committed
2064
2065
    if (this.affixed === affix) return
    if (this.unpin != null) this.$element.css('top', '')
2066

XhmikosR's avatar
XhmikosR committed
2067
2068
    var affixType = 'affix' + (affix ? '-' + affix : '')
    var e         = $.Event(affixType + '.bs.affix')
2069

XhmikosR's avatar
XhmikosR committed
2070
    this.$element.trigger(e)
2071

XhmikosR's avatar
XhmikosR committed
2072
    if (e.isDefaultPrevented()) return
2073

XhmikosR's avatar
XhmikosR committed
2074
2075
    this.affixed = affix
    this.unpin = affix == 'bottom' ? this.getPinnedOffset() : null
2076

XhmikosR's avatar
XhmikosR committed
2077
2078
2079
    this.$element
      .removeClass(Affix.RESET)
      .addClass(affixType)
Mark Otto's avatar
grunt    
Mark Otto committed
2080
      .trigger(affixType.replace('affix', 'affixed') + '.bs.affix')
2081

XhmikosR's avatar
XhmikosR committed
2082
2083
2084
2085
    if (affix == 'bottom') {
      this.$element.offset({
        top: scrollHeight - this.$element.height() - offsetBottom
      })
2086
    }
XhmikosR's avatar
XhmikosR committed
2087
  }
Mark Otto's avatar
Mark Otto committed
2088

2089

XhmikosR's avatar
XhmikosR committed
2090
2091
  // AFFIX PLUGIN DEFINITION
  // =======================
2092

XhmikosR's avatar
XhmikosR committed
2093
2094
2095
2096
2097
  function Plugin(option) {
    return this.each(function () {
      var $this   = $(this)
      var data    = $this.data('bs.affix')
      var options = typeof option == 'object' && option
2098

XhmikosR's avatar
XhmikosR committed
2099
2100
2101
2102
      if (!data) $this.data('bs.affix', (data = new Affix(this, options)))
      if (typeof option == 'string') data[option]()
    })
  }
2103

XhmikosR's avatar
XhmikosR committed
2104
  var old = $.fn.affix
Mark Otto's avatar
Mark Otto committed
2105

XhmikosR's avatar
XhmikosR committed
2106
2107
  $.fn.affix             = Plugin
  $.fn.affix.Constructor = Affix
2108
2109


XhmikosR's avatar
XhmikosR committed
2110
2111
  // AFFIX NO CONFLICT
  // =================
2112

XhmikosR's avatar
XhmikosR committed
2113
2114
2115
2116
  $.fn.affix.noConflict = function () {
    $.fn.affix = old
    return this
  }
2117
2118


XhmikosR's avatar
XhmikosR committed
2119
2120
  // AFFIX DATA-API
  // ==============
2121

XhmikosR's avatar
XhmikosR committed
2122
2123
2124
2125
  $(window).on('load', function () {
    $('[data-spy="affix"]').each(function () {
      var $spy = $(this)
      var data = $spy.data()
2126

XhmikosR's avatar
XhmikosR committed
2127
      data.offset = data.offset || {}
2128

XhmikosR's avatar
XhmikosR committed
2129
2130
      if (data.offsetBottom) data.offset.bottom = data.offsetBottom
      if (data.offsetTop)    data.offset.top    = data.offsetTop
2131

XhmikosR's avatar
XhmikosR committed
2132
      Plugin.call($spy, data)
2133
    })
Mark Otto's avatar
Mark Otto committed
2134
2135
  })

XhmikosR's avatar
XhmikosR committed
2136
}(jQuery);
For faster browsing, not all history is shown. View entire blame