bootstrap.js 59.3 KB
Newer Older
XhmikosR's avatar
XhmikosR committed
2001
  }
fat's avatar
fat committed
2002

XhmikosR's avatar
XhmikosR committed
2003
  Affix.VERSION  = '3.1.1'
2004

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

XhmikosR's avatar
XhmikosR committed
2007
2008
2009
2010
  Affix.DEFAULTS = {
    offset: 0,
    target: window
  }
2011

XhmikosR's avatar
XhmikosR committed
2012
2013
2014
2015
2016
2017
2018
  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)
  }
2019

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

XhmikosR's avatar
XhmikosR committed
2024
2025
  Affix.prototype.checkPosition = function () {
    if (!this.$element.is(':visible')) return
2026

XhmikosR's avatar
XhmikosR committed
2027
2028
2029
2030
2031
2032
    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
2033

XhmikosR's avatar
XhmikosR committed
2034
2035
2036
    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)
2037

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

XhmikosR's avatar
XhmikosR committed
2042
2043
    if (this.affixed === affix) return
    if (this.unpin != null) this.$element.css('top', '')
2044

XhmikosR's avatar
XhmikosR committed
2045
2046
    var affixType = 'affix' + (affix ? '-' + affix : '')
    var e         = $.Event(affixType + '.bs.affix')
2047

XhmikosR's avatar
XhmikosR committed
2048
    this.$element.trigger(e)
2049

XhmikosR's avatar
XhmikosR committed
2050
    if (e.isDefaultPrevented()) return
2051

XhmikosR's avatar
XhmikosR committed
2052
2053
    this.affixed = affix
    this.unpin = affix == 'bottom' ? this.getPinnedOffset() : null
2054

XhmikosR's avatar
XhmikosR committed
2055
2056
2057
2058
    this.$element
      .removeClass(Affix.RESET)
      .addClass(affixType)
      .trigger($.Event(affixType.replace('affix', 'affixed')))
2059

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

2067

XhmikosR's avatar
XhmikosR committed
2068
2069
  // AFFIX PLUGIN DEFINITION
  // =======================
2070

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

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

XhmikosR's avatar
XhmikosR committed
2082
  var old = $.fn.affix
Mark Otto's avatar
Mark Otto committed
2083

XhmikosR's avatar
XhmikosR committed
2084
2085
  $.fn.affix             = Plugin
  $.fn.affix.Constructor = Affix
2086
2087


XhmikosR's avatar
XhmikosR committed
2088
2089
  // AFFIX NO CONFLICT
  // =================
2090

XhmikosR's avatar
XhmikosR committed
2091
2092
2093
2094
  $.fn.affix.noConflict = function () {
    $.fn.affix = old
    return this
  }
2095
2096


XhmikosR's avatar
XhmikosR committed
2097
2098
  // AFFIX DATA-API
  // ==============
2099

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

XhmikosR's avatar
XhmikosR committed
2105
      data.offset = data.offset || {}
2106

XhmikosR's avatar
XhmikosR committed
2107
2108
      if (data.offsetBottom) data.offset.bottom = data.offsetBottom
      if (data.offsetTop)    data.offset.top    = data.offsetTop
2109

XhmikosR's avatar
XhmikosR committed
2110
      Plugin.call($spy, data)
2111
    })
Mark Otto's avatar
Mark Otto committed
2112
2113
  })

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