bootstrap-affix.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* ==========================================================
  2. * bootstrap-affix.js v2.3.2
  3. * http://getbootstrap.com/2.3.2/javascript.html#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 ($) {
  20. "use strict"; // jshint ;_;
  21. /* AFFIX CLASS DEFINITION
  22. * ====================== */
  23. var Affix = function (element, options) {
  24. this.options = $.extend({}, $.fn.affix.defaults, options)
  25. this.$window = $(window)
  26. .on('scroll.affix.data-api', $.proxy(this.checkPosition, this))
  27. .on('click.affix.data-api', $.proxy(function () { setTimeout($.proxy(this.checkPosition, this), 1) }, this))
  28. this.$element = $(element)
  29. this.checkPosition()
  30. }
  31. Affix.prototype.checkPosition = function () {
  32. if (!this.$element.is(':visible')) return
  33. var scrollHeight = $(document).height()
  34. , scrollTop = this.$window.scrollTop()
  35. , position = this.$element.offset()
  36. , offset = this.options.offset
  37. , offsetBottom = offset.bottom
  38. , offsetTop = offset.top
  39. , reset = 'affix affix-top affix-bottom'
  40. , affix
  41. if (typeof offset != 'object') offsetBottom = offsetTop = offset
  42. if (typeof offsetTop == 'function') offsetTop = offset.top()
  43. if (typeof offsetBottom == 'function') offsetBottom = offset.bottom()
  44. affix = this.unpin != null && (scrollTop + this.unpin <= position.top) ?
  45. false : offsetBottom != null && (position.top + this.$element.height() >= scrollHeight - offsetBottom) ?
  46. 'bottom' : offsetTop != null && scrollTop <= offsetTop ?
  47. 'top' : false
  48. if (this.affixed === affix) return
  49. this.affixed = affix
  50. this.unpin = affix == 'bottom' ? position.top - scrollTop : null
  51. this.$element.removeClass(reset).addClass('affix' + (affix ? '-' + affix : ''))
  52. }
  53. /* AFFIX PLUGIN DEFINITION
  54. * ======================= */
  55. var old = $.fn.affix
  56. $.fn.affix = function (option) {
  57. return this.each(function () {
  58. var $this = $(this)
  59. , data = $this.data('affix')
  60. , options = typeof option == 'object' && option
  61. if (!data) $this.data('affix', (data = new Affix(this, options)))
  62. if (typeof option == 'string') data[option]()
  63. })
  64. }
  65. $.fn.affix.Constructor = Affix
  66. $.fn.affix.defaults = {
  67. offset: 0
  68. }
  69. /* AFFIX NO CONFLICT
  70. * ================= */
  71. $.fn.affix.noConflict = function () {
  72. $.fn.affix = old
  73. return this
  74. }
  75. /* AFFIX DATA-API
  76. * ============== */
  77. $(window).on('load', function () {
  78. $('[data-spy="affix"]').each(function () {
  79. var $spy = $(this)
  80. , data = $spy.data()
  81. data.offset = data.offset || {}
  82. data.offsetBottom && (data.offset.bottom = data.offsetBottom)
  83. data.offsetTop && (data.offset.top = data.offsetTop)
  84. $spy.affix(data)
  85. })
  86. })
  87. }(window.jQuery);