bootstrap-affix.js 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* ==========================================================
  2. * bootstrap-affix.js v2.1.1
  3. * http://twitter.github.com/bootstrap/javascript.html#affix
  4. * ==========================================================
  5. * Copyright 2012 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).on('scroll.affix.data-api', $.proxy(this.checkPosition, this))
  26. this.$element = $(element)
  27. this.checkPosition()
  28. }
  29. Affix.prototype.checkPosition = function () {
  30. if (!this.$element.is(':visible')) return
  31. var scrollHeight = $(document).height()
  32. , scrollTop = this.$window.scrollTop()
  33. , position = this.$element.offset()
  34. , offset = this.options.offset
  35. , offsetBottom = offset.bottom
  36. , offsetTop = offset.top
  37. , reset = 'affix affix-top affix-bottom'
  38. , affix
  39. if (typeof offset != 'object') offsetBottom = offsetTop = offset
  40. if (typeof offsetTop == 'function') offsetTop = offset.top()
  41. if (typeof offsetBottom == 'function') offsetBottom = offset.bottom()
  42. affix = this.unpin != null && (scrollTop + this.unpin <= position.top) ?
  43. false : offsetBottom != null && (position.top + this.$element.height() >= scrollHeight - offsetBottom) ?
  44. 'bottom' : offsetTop != null && scrollTop <= offsetTop ?
  45. 'top' : false
  46. if (this.affixed === affix) return
  47. this.affixed = affix
  48. this.unpin = affix == 'bottom' ? position.top - scrollTop : null
  49. this.$element.removeClass(reset).addClass('affix' + (affix ? '-' + affix : ''))
  50. }
  51. /* AFFIX PLUGIN DEFINITION
  52. * ======================= */
  53. $.fn.affix = function (option) {
  54. return this.each(function () {
  55. var $this = $(this)
  56. , data = $this.data('affix')
  57. , options = typeof option == 'object' && option
  58. if (!data) $this.data('affix', (data = new Affix(this, options)))
  59. if (typeof option == 'string') data[option]()
  60. })
  61. }
  62. $.fn.affix.Constructor = Affix
  63. $.fn.affix.defaults = {
  64. offset: 0
  65. }
  66. /* AFFIX DATA-API
  67. * ============== */
  68. $(window).on('load', function () {
  69. $('[data-spy="affix"]').each(function () {
  70. var $spy = $(this)
  71. , data = $spy.data()
  72. data.offset = data.offset || {}
  73. data.offsetBottom && (data.offset.bottom = data.offsetBottom)
  74. data.offsetTop && (data.offset.top = data.offsetTop)
  75. $spy.affix(data)
  76. })
  77. })
  78. }(window.jQuery);