bootstrap.js 58.9 KB
Newer Older
2001
2002
2003
2004
2005
2006
2007
    setTimeout($.proxy(this.checkPosition, this), 1)
  }

  Affix.prototype.checkPosition = function () {
    if (!this.$element.is(':visible')) return

    var scrollHeight = $(document).height()
Chris Rebert's avatar
Chris Rebert committed
2008
    var scrollTop    = this.$target.scrollTop()
2009
2010
2011
2012
2013
2014
    var position     = this.$element.offset()
    var offset       = this.options.offset
    var offsetTop    = offset.top
    var offsetBottom = offset.bottom

    if (typeof offset != 'object')         offsetBottom = offsetTop = offset
2015
2016
    if (typeof offsetTop == 'function')    offsetTop    = offset.top(this.$element)
    if (typeof offsetBottom == 'function') offsetBottom = offset.bottom(this.$element)
2017
2018
2019
2020
2021
2022

    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

    if (this.affixed === affix) return
fat's avatar
fat committed
2023
    if (this.unpin != null) this.$element.css('top', '')
2024

2025
2026
2027
2028
2029
2030
2031
    var affixType = 'affix' + (affix ? '-' + affix : '')
    var e         = $.Event(affixType + '.bs.affix')

    this.$element.trigger(e)

    if (e.isDefaultPrevented()) return

2032
    this.affixed = affix
fat's avatar
fat committed
2033
    this.unpin = affix == 'bottom' ? this.getPinnedOffset() : null
2034

2035
2036
2037
2038
    this.$element
      .removeClass(Affix.RESET)
      .addClass(affixType)
      .trigger($.Event(affixType.replace('affix', 'affixed')))
2039
2040

    if (affix == 'bottom') {
XhmikosR's avatar
XhmikosR committed
2041
2042
2043
      this.$element.offset({
        top: scrollHeight - this.$element.height() - offsetBottom
      })
2044
    }
Mark Otto's avatar
Mark Otto committed
2045
2046
  }

2047
2048
2049
2050

  // AFFIX PLUGIN DEFINITION
  // =======================

Mark Otto's avatar
Mark Otto committed
2051
  function Plugin(option) {
2052
2053
2054
2055
2056
2057
2058
2059
    return this.each(function () {
      var $this   = $(this)
      var data    = $this.data('bs.affix')
      var options = typeof option == 'object' && option

      if (!data) $this.data('bs.affix', (data = new Affix(this, options)))
      if (typeof option == 'string') data[option]()
    })
2060
2061
  }

Mark Otto's avatar
Mark Otto committed
2062
2063
2064
  var old = $.fn.affix

  $.fn.affix             = Plugin
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
  $.fn.affix.Constructor = Affix


  // AFFIX NO CONFLICT
  // =================

  $.fn.affix.noConflict = function () {
    $.fn.affix = old
    return this
  }


  // AFFIX DATA-API
  // ==============

  $(window).on('load', function () {
    $('[data-spy="affix"]').each(function () {
      var $spy = $(this)
      var data = $spy.data()

      data.offset = data.offset || {}

      if (data.offsetBottom) data.offset.bottom = data.offsetBottom
      if (data.offsetTop)    data.offset.top    = data.offsetTop

Mark Otto's avatar
Mark Otto committed
2090
      Plugin.call($spy, data)
2091
    })
Mark Otto's avatar
Mark Otto committed
2092
2093
  })

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