1
0

affix.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /* ========================================================================
  2. * Bootstrap: affix.js v3.1.1
  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.$window = $(window)
  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.RESET = 'affix affix-top affix-bottom'
  24. Affix.DEFAULTS = {
  25. offset: 0
  26. }
  27. Affix.prototype.getPinnedOffset = function () {
  28. if (this.pinnedOffset) return this.pinnedOffset
  29. this.$element.removeClass(Affix.RESET).addClass('affix')
  30. var scrollTop = this.$window.scrollTop()
  31. var position = this.$element.offset()
  32. return (this.pinnedOffset = position.top - scrollTop)
  33. }
  34. Affix.prototype.checkPositionWithEventLoop = function () {
  35. setTimeout($.proxy(this.checkPosition, this), 1)
  36. }
  37. Affix.prototype.checkPosition = function () {
  38. if (!this.$element.is(':visible')) return
  39. var scrollHeight = $(document).height()
  40. var scrollTop = this.$window.scrollTop()
  41. var position = this.$element.offset()
  42. var offset = this.options.offset
  43. var offsetTop = offset.top
  44. var offsetBottom = offset.bottom
  45. if (this.affixed == 'top') position.top += scrollTop
  46. if (typeof offset != 'object') offsetBottom = offsetTop = offset
  47. if (typeof offsetTop == 'function') offsetTop = offset.top(this.$element)
  48. if (typeof offsetBottom == 'function') offsetBottom = offset.bottom(this.$element)
  49. var affix = this.unpin != null && (scrollTop + this.unpin <= position.top) ? false :
  50. offsetBottom != null && (position.top + this.$element.height() >= scrollHeight - offsetBottom) ? 'bottom' :
  51. offsetTop != null && (scrollTop <= offsetTop) ? 'top' : false
  52. if (this.affixed === affix) return
  53. if (this.unpin) this.$element.css('top', '')
  54. var affixType = 'affix' + (affix ? '-' + affix : '')
  55. var e = $.Event(affixType + '.bs.affix')
  56. this.$element.trigger(e)
  57. if (e.isDefaultPrevented()) return
  58. this.affixed = affix
  59. this.unpin = affix == 'bottom' ? this.getPinnedOffset() : null
  60. this.$element
  61. .removeClass(Affix.RESET)
  62. .addClass(affixType)
  63. .trigger($.Event(affixType.replace('affix', 'affixed')))
  64. if (affix == 'bottom') {
  65. this.$element.offset({ top: scrollHeight - offsetBottom - this.$element.height() })
  66. }
  67. }
  68. // AFFIX PLUGIN DEFINITION
  69. // =======================
  70. var old = $.fn.affix
  71. $.fn.affix = function (option) {
  72. return this.each(function () {
  73. var $this = $(this)
  74. var data = $this.data('bs.affix')
  75. var options = typeof option == 'object' && option
  76. if (!data) $this.data('bs.affix', (data = new Affix(this, options)))
  77. if (typeof option == 'string') data[option]()
  78. })
  79. }
  80. $.fn.affix.Constructor = Affix
  81. // AFFIX NO CONFLICT
  82. // =================
  83. $.fn.affix.noConflict = function () {
  84. $.fn.affix = old
  85. return this
  86. }
  87. // AFFIX DATA-API
  88. // ==============
  89. $(window).on('load', function () {
  90. $('[data-spy="affix"]').each(function () {
  91. var $spy = $(this)
  92. var data = $spy.data()
  93. data.offset = data.offset || {}
  94. if (data.offsetBottom) data.offset.bottom = data.offsetBottom
  95. if (data.offsetTop) data.offset.top = data.offsetTop
  96. $spy.affix(data)
  97. })
  98. })
  99. }(jQuery);