bootstrap.js 59.4 KB
Newer Older
XhmikosR's avatar
XhmikosR committed
2001
    this.pinnedOffset = null
fat's avatar
fat committed
2002

XhmikosR's avatar
XhmikosR committed
2003
2004
    this.checkPosition()
  }
fat's avatar
fat committed
2005

Mark Otto's avatar
Mark Otto committed
2006
  Affix.VERSION  = '3.2.0'
2007

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

XhmikosR's avatar
XhmikosR committed
2010
2011
2012
2013
  Affix.DEFAULTS = {
    offset: 0,
    target: window
  }
2014

XhmikosR's avatar
XhmikosR committed
2015
2016
2017
2018
2019
2020
2021
  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)
  }
2022

XhmikosR's avatar
XhmikosR committed
2023
2024
2025
  Affix.prototype.checkPositionWithEventLoop = function () {
    setTimeout($.proxy(this.checkPosition, this), 1)
  }
fat's avatar
fat committed
2026

XhmikosR's avatar
XhmikosR committed
2027
2028
  Affix.prototype.checkPosition = function () {
    if (!this.$element.is(':visible')) return
2029

XhmikosR's avatar
XhmikosR committed
2030
2031
2032
2033
2034
2035
    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
2036

XhmikosR's avatar
XhmikosR committed
2037
2038
2039
    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)
2040

XhmikosR's avatar
XhmikosR committed
2041
2042
2043
    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
2044

XhmikosR's avatar
XhmikosR committed
2045
2046
    if (this.affixed === affix) return
    if (this.unpin != null) this.$element.css('top', '')
2047

XhmikosR's avatar
XhmikosR committed
2048
2049
    var affixType = 'affix' + (affix ? '-' + affix : '')
    var e         = $.Event(affixType + '.bs.affix')
2050

XhmikosR's avatar
XhmikosR committed
2051
    this.$element.trigger(e)
2052

XhmikosR's avatar
XhmikosR committed
2053
    if (e.isDefaultPrevented()) return
2054

XhmikosR's avatar
XhmikosR committed
2055
2056
    this.affixed = affix
    this.unpin = affix == 'bottom' ? this.getPinnedOffset() : null
2057

XhmikosR's avatar
XhmikosR committed
2058
2059
2060
    this.$element
      .removeClass(Affix.RESET)
      .addClass(affixType)
Mark Otto's avatar
grunt    
Mark Otto committed
2061
      .trigger(affixType.replace('affix', 'affixed') + '.bs.affix')
2062

XhmikosR's avatar
XhmikosR committed
2063
2064
2065
2066
    if (affix == 'bottom') {
      this.$element.offset({
        top: scrollHeight - this.$element.height() - offsetBottom
      })
2067
    }
XhmikosR's avatar
XhmikosR committed
2068
  }
Mark Otto's avatar
Mark Otto committed
2069

2070

XhmikosR's avatar
XhmikosR committed
2071
2072
  // AFFIX PLUGIN DEFINITION
  // =======================
2073

XhmikosR's avatar
XhmikosR committed
2074
2075
2076
2077
2078
  function Plugin(option) {
    return this.each(function () {
      var $this   = $(this)
      var data    = $this.data('bs.affix')
      var options = typeof option == 'object' && option
2079

XhmikosR's avatar
XhmikosR committed
2080
2081
2082
2083
      if (!data) $this.data('bs.affix', (data = new Affix(this, options)))
      if (typeof option == 'string') data[option]()
    })
  }
2084

XhmikosR's avatar
XhmikosR committed
2085
  var old = $.fn.affix
Mark Otto's avatar
Mark Otto committed
2086

XhmikosR's avatar
XhmikosR committed
2087
2088
  $.fn.affix             = Plugin
  $.fn.affix.Constructor = Affix
2089
2090


XhmikosR's avatar
XhmikosR committed
2091
2092
  // AFFIX NO CONFLICT
  // =================
2093

XhmikosR's avatar
XhmikosR committed
2094
2095
2096
2097
  $.fn.affix.noConflict = function () {
    $.fn.affix = old
    return this
  }
2098
2099


XhmikosR's avatar
XhmikosR committed
2100
2101
  // AFFIX DATA-API
  // ==============
2102

XhmikosR's avatar
XhmikosR committed
2103
2104
2105
2106
  $(window).on('load', function () {
    $('[data-spy="affix"]').each(function () {
      var $spy = $(this)
      var data = $spy.data()
2107

XhmikosR's avatar
XhmikosR committed
2108
      data.offset = data.offset || {}
2109

XhmikosR's avatar
XhmikosR committed
2110
2111
      if (data.offsetBottom) data.offset.bottom = data.offsetBottom
      if (data.offsetTop)    data.offset.top    = data.offsetTop
2112

XhmikosR's avatar
XhmikosR committed
2113
      Plugin.call($spy, data)
2114
    })
Mark Otto's avatar
Mark Otto committed
2115
2116
  })

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