bootstrap.js 60.5 KB
Newer Older
XhmikosR's avatar
XhmikosR committed
2001
  'use strict';
Mark Otto's avatar
grunt    
Mark Otto committed
2002

XhmikosR's avatar
XhmikosR committed
2003
2004
  // AFFIX CLASS DEFINITION
  // ======================
Mark Otto's avatar
Mark Otto committed
2005

XhmikosR's avatar
XhmikosR committed
2006
2007
  var Affix = function (element, options) {
    this.options = $.extend({}, Affix.DEFAULTS, options)
Mark Otto's avatar
Mark Otto committed
2008

XhmikosR's avatar
XhmikosR committed
2009
2010
2011
    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
2012

XhmikosR's avatar
XhmikosR committed
2013
2014
2015
2016
    this.$element     = $(element)
    this.affixed      =
    this.unpin        =
    this.pinnedOffset = null
fat's avatar
fat committed
2017

XhmikosR's avatar
XhmikosR committed
2018
2019
    this.checkPosition()
  }
fat's avatar
fat committed
2020

Mark Otto's avatar
Mark Otto committed
2021
  Affix.VERSION  = '3.2.0'
2022

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

XhmikosR's avatar
XhmikosR committed
2025
2026
2027
2028
  Affix.DEFAULTS = {
    offset: 0,
    target: window
  }
2029

2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
  Affix.prototype.getState = function (scrollHeight, height, offsetTop, offsetBottom) {
    var scrollTop    = this.$target.scrollTop()
    var position     = this.$element.offset()
    var targetHeight = this.$target.height()

    if (offsetTop != null && this.affixed == 'top') return scrollTop < offsetTop ? 'top' : false

    if (this.affixed == 'bottom') {
      if (offsetTop != null) return (scrollTop + this.unpin <= position.top) ? false : 'bottom'
      return (scrollTop + targetHeight <= scrollHeight - offsetBottom) ? false : 'bottom'
    }

    var initializing   = this.affixed == null
    var colliderTop    = initializing ? scrollTop : position.top
    var colliderHeight = initializing ? targetHeight : height

    if (offsetTop != null && colliderTop <= offsetTop) return 'top'
    if (offsetBottom != null && (colliderTop + colliderHeight >= scrollHeight - offsetBottom)) return 'bottom'

    return false
  }

XhmikosR's avatar
XhmikosR committed
2052
2053
2054
2055
2056
2057
2058
  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)
  }
2059

XhmikosR's avatar
XhmikosR committed
2060
2061
2062
  Affix.prototype.checkPositionWithEventLoop = function () {
    setTimeout($.proxy(this.checkPosition, this), 1)
  }
fat's avatar
fat committed
2063

XhmikosR's avatar
XhmikosR committed
2064
2065
  Affix.prototype.checkPosition = function () {
    if (!this.$element.is(':visible')) return
2066

2067
    var height       = this.$element.height()
XhmikosR's avatar
XhmikosR committed
2068
2069
2070
    var offset       = this.options.offset
    var offsetTop    = offset.top
    var offsetBottom = offset.bottom
2071
    var scrollHeight = $('body').height()
2072

XhmikosR's avatar
XhmikosR committed
2073
2074
2075
    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)
2076

2077
    var affix = this.getState(scrollHeight, height, offsetTop, offsetBottom)
2078

2079
2080
    if (this.affixed != affix) {
      if (this.unpin != null) this.$element.css('top', '')
2081

2082
2083
      var affixType = 'affix' + (affix ? '-' + affix : '')
      var e         = $.Event(affixType + '.bs.affix')
2084

2085
      this.$element.trigger(e)
2086

2087
      if (e.isDefaultPrevented()) return
2088

2089
2090
      this.affixed = affix
      this.unpin = affix == 'bottom' ? this.getPinnedOffset() : null
2091

2092
2093
2094
2095
2096
      this.$element
        .removeClass(Affix.RESET)
        .addClass(affixType)
        .trigger(affixType.replace('affix', 'affixed') + '.bs.affix')
    }
2097

XhmikosR's avatar
XhmikosR committed
2098
2099
    if (affix == 'bottom') {
      this.$element.offset({
2100
        top: scrollHeight - height - offsetBottom
XhmikosR's avatar
XhmikosR committed
2101
      })
2102
    }
XhmikosR's avatar
XhmikosR committed
2103
  }
Mark Otto's avatar
Mark Otto committed
2104

2105

XhmikosR's avatar
XhmikosR committed
2106
2107
  // AFFIX PLUGIN DEFINITION
  // =======================
2108

XhmikosR's avatar
XhmikosR committed
2109
2110
2111
2112
2113
  function Plugin(option) {
    return this.each(function () {
      var $this   = $(this)
      var data    = $this.data('bs.affix')
      var options = typeof option == 'object' && option
2114

XhmikosR's avatar
XhmikosR committed
2115
2116
2117
2118
      if (!data) $this.data('bs.affix', (data = new Affix(this, options)))
      if (typeof option == 'string') data[option]()
    })
  }
2119

XhmikosR's avatar
XhmikosR committed
2120
  var old = $.fn.affix
Mark Otto's avatar
Mark Otto committed
2121

XhmikosR's avatar
XhmikosR committed
2122
2123
  $.fn.affix             = Plugin
  $.fn.affix.Constructor = Affix
2124
2125


XhmikosR's avatar
XhmikosR committed
2126
2127
  // AFFIX NO CONFLICT
  // =================
2128

XhmikosR's avatar
XhmikosR committed
2129
2130
2131
2132
  $.fn.affix.noConflict = function () {
    $.fn.affix = old
    return this
  }
2133
2134


XhmikosR's avatar
XhmikosR committed
2135
2136
  // AFFIX DATA-API
  // ==============
2137

XhmikosR's avatar
XhmikosR committed
2138
2139
2140
2141
  $(window).on('load', function () {
    $('[data-spy="affix"]').each(function () {
      var $spy = $(this)
      var data = $spy.data()
2142

XhmikosR's avatar
XhmikosR committed
2143
      data.offset = data.offset || {}
2144

XhmikosR's avatar
XhmikosR committed
2145
2146
      if (data.offsetBottom) data.offset.bottom = data.offsetBottom
      if (data.offsetTop)    data.offset.top    = data.offsetTop
2147

XhmikosR's avatar
XhmikosR committed
2148
      Plugin.call($spy, data)
2149
    })
Mark Otto's avatar
Mark Otto committed
2150
2151
  })

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