affix.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* ========================================================================
  2. * Bootstrap: affix.js v3.0.3
  3. * http://getbootstrap.com/javascript/#affix
  4. * ========================================================================
  5. * Copyright 2013 Twitter, Inc.
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. * ======================================================================== */
  19. +function ($) { "use strict";
  20. // AFFIX CLASS DEFINITION
  21. // ======================
  22. var Affix = function (element, options) {
  23. this.options = $.extend({}, Affix.DEFAULTS, options)
  24. this.$window = $(window)
  25. .on('scroll.bs.affix.data-api', $.proxy(this.checkPosition, this))
  26. .on('click.bs.affix.data-api', $.proxy(this.checkPositionWithEventLoop, this))
  27. this.$element = $(element)
  28. this.affixed =
  29. this.unpin = null
  30. this.checkPosition()
  31. }
  32. Affix.RESET = 'affix affix-top affix-bottom'
  33. Affix.DEFAULTS = {
  34. offset: 0
  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.$window.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()
  49. if (typeof offsetBottom == 'function') offsetBottom = offset.bottom()
  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) this.$element.css('top', '')
  55. this.affixed = affix
  56. this.unpin = affix == 'bottom' ? position.top - scrollTop : null
  57. this.$element.removeClass(Affix.RESET).addClass('affix' + (affix ? '-' + affix : ''))
  58. if (affix == 'bottom') {
  59. this.$element.offset({ top: document.body.offsetHeight - offsetBottom - this.$element.height() })
  60. }
  61. }
  62. // AFFIX PLUGIN DEFINITION
  63. // =======================
  64. var old = $.fn.affix
  65. $.fn.affix = function (option) {
  66. return this.each(function () {
  67. var $this = $(this)
  68. var data = $this.data('bs.affix')
  69. var options = typeof option == 'object' && option
  70. if (!data) $this.data('bs.affix', (data = new Affix(this, options)))
  71. if (typeof option == 'string') data[option]()
  72. })
  73. }
  74. $.fn.affix.Constructor = Affix
  75. // AFFIX NO CONFLICT
  76. // =================
  77. $.fn.affix.noConflict = function () {
  78. $.fn.affix = old
  79. return this
  80. }
  81. // AFFIX DATA-API
  82. // ==============
  83. $(window).on('load', function () {
  84. $('[data-spy="affix"]').each(function () {
  85. var $spy = $(this)
  86. var data = $spy.data()
  87. data.offset = data.offset || {}
  88. if (data.offsetBottom) data.offset.bottom = data.offsetBottom
  89. if (data.offsetTop) data.offset.top = data.offsetTop
  90. $spy.affix(data)
  91. })
  92. })
  93. }(jQuery);