bootstrap.js 58.8 KB
Newer Older
2001
2002
2003
2004
  Affix.prototype.checkPosition = function () {
    if (!this.$element.is(':visible')) return

    var scrollHeight = $(document).height()
Chris Rebert's avatar
Chris Rebert committed
2005
    var scrollTop    = this.$target.scrollTop()
2006
2007
2008
2009
2010
2011
    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
2012
2013
    if (typeof offsetTop == 'function')    offsetTop    = offset.top(this.$element)
    if (typeof offsetBottom == 'function') offsetBottom = offset.bottom(this.$element)
2014
2015
2016
2017
2018
2019

    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
2020
    if (this.unpin != null) this.$element.css('top', '')
2021

2022
2023
2024
2025
2026
2027
2028
    var affixType = 'affix' + (affix ? '-' + affix : '')
    var e         = $.Event(affixType + '.bs.affix')

    this.$element.trigger(e)

    if (e.isDefaultPrevented()) return

2029
    this.affixed = affix
fat's avatar
fat committed
2030
    this.unpin = affix == 'bottom' ? this.getPinnedOffset() : null
2031

2032
2033
2034
2035
    this.$element
      .removeClass(Affix.RESET)
      .addClass(affixType)
      .trigger($.Event(affixType.replace('affix', 'affixed')))
2036
2037

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

2044
2045
2046
2047

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

Mark Otto's avatar
Mark Otto committed
2048
  function Plugin(option) {
2049
2050
2051
2052
2053
2054
2055
2056
    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]()
    })
2057
2058
  }

Mark Otto's avatar
Mark Otto committed
2059
2060
2061
  var old = $.fn.affix

  $.fn.affix             = Plugin
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
  $.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
2087
      Plugin.call($spy, data)
2088
    })
Mark Otto's avatar
Mark Otto committed
2089
2090
  })

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