affix.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* ========================================================================
  2. * Bootstrap: affix.js v3.2.0
  3. * http://getbootstrap.com/javascript/#affix
  4. * ========================================================================
  5. * Copyright 2011-2014 Twitter, Inc.
  6. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
  7. * ======================================================================== */
  8. +function ($) {
  9. 'use strict';
  10. // AFFIX CLASS DEFINITION
  11. // ======================
  12. var Affix = function (element, options) {
  13. this.options = $.extend({}, Affix.DEFAULTS, options)
  14. this.$target = $(this.options.target)
  15. .on('scroll.bs.affix.data-api', $.proxy(this.checkPosition, this))
  16. .on('click.bs.affix.data-api', $.proxy(this.checkPositionWithEventLoop, this))
  17. this.$element = $(element)
  18. this.affixed =
  19. this.unpin =
  20. this.pinnedOffset = null
  21. this.checkPosition()
  22. }
  23. Affix.VERSION = '3.2.0'
  24. Affix.RESET = 'affix affix-top affix-bottom'
  25. Affix.DEFAULTS = {
  26. offset: 0,
  27. target: window
  28. }
  29. Affix.prototype.getPinnedOffset = function () {
  30. if (this.pinnedOffset) return this.pinnedOffset
  31. this.$element.removeClass(Affix.RESET).addClass('affix')
  32. var scrollTop = this.$target.scrollTop()
  33. var position = this.$element.offset()
  34. return (this.pinnedOffset = position.top - scrollTop)
  35. }
  36. Affix.prototype.checkPositionWithEventLoop = function () {
  37. setTimeout($.proxy(this.checkPosition, this), 1)
  38. }
  39. Affix.prototype.checkPosition = function () {
  40. if (!this.$element.is(':visible')) return
  41. var scrollHeight = $(document).height()
  42. var scrollTop = this.$target.scrollTop()
  43. var position = this.$element.offset()
  44. var offset = this.options.offset
  45. var offsetTop = offset.top
  46. var offsetBottom = offset.bottom
  47. if (typeof offset != 'object') offsetBottom = offsetTop = offset
  48. if (typeof offsetTop == 'function') offsetTop = offset.top(this.$element)
  49. if (typeof offsetBottom == 'function') offsetBottom = offset.bottom(this.$element)
  50. var affix = this.unpin != null && (scrollTop + this.unpin <= position.top) ? false :
  51. offsetBottom != null && (position.top + this.$element.height() >= scrollHeight - offsetBottom) ? 'bottom' :
  52. offsetTop != null && (scrollTop <= offsetTop) ? 'top' : false
  53. if (this.affixed === affix) return
  54. if (this.unpin != null) this.$element.css('top', '')
  55. var affixType = 'affix' + (affix ? '-' + affix : '')
  56. var e = $.Event(affixType + '.bs.affix')
  57. this.$element.trigger(e)
  58. if (e.isDefaultPrevented()) return
  59. this.affixed = affix
  60. this.unpin = affix == 'bottom' ? this.getPinnedOffset() : null
  61. this.$element
  62. .removeClass(Affix.RESET)
  63. .addClass(affixType)
  64. .trigger($.Event(affixType.replace('affix', 'affixed')))
  65. if (affix == 'bottom') {
  66. this.$element.offset({
  67. top: scrollHeight - this.$element.height() - offsetBottom
  68. })
  69. }
  70. }
  71. // AFFIX PLUGIN DEFINITION
  72. // =======================
  73. function Plugin(option) {
  74. return this.each(function () {
  75. var $this = $(this)
  76. var data = $this.data('bs.affix')
  77. var options = typeof option == 'object' && option
  78. if (!data) $this.data('bs.affix', (data = new Affix(this, options)))
  79. if (typeof option == 'string') data[option]()
  80. })
  81. }
  82. var old = $.fn.affix
  83. $.fn.affix = Plugin
  84. $.fn.affix.Constructor = Affix
  85. // AFFIX NO CONFLICT
  86. // =================
  87. $.fn.affix.noConflict = function () {
  88. $.fn.affix = old
  89. return this
  90. }
  91. // AFFIX DATA-API
  92. // ==============
  93. $(window).on('load', function () {
  94. $('[data-spy="affix"]').each(function () {
  95. var $spy = $(this)
  96. var data = $spy.data()
  97. data.offset = data.offset || {}
  98. if (data.offsetBottom) data.offset.bottom = data.offsetBottom
  99. if (data.offsetTop) data.offset.top = data.offsetTop
  100. Plugin.call($spy, data)
  101. })
  102. })
  103. }(jQuery);