You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 lines
4.7 KiB

4 years ago
  1. /* ========================================================================
  2. * Bootstrap: affix.js v3.3.7
  3. * http://getbootstrap.com/javascript/#affix
  4. * ========================================================================
  5. * Copyright 2011-2016 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 = null
  19. this.unpin = null
  20. this.pinnedOffset = null
  21. this.checkPosition()
  22. }
  23. Affix.VERSION = '3.3.7'
  24. Affix.RESET = 'affix affix-top affix-bottom'
  25. Affix.DEFAULTS = {
  26. offset: 0,
  27. target: window
  28. }
  29. Affix.prototype.getState = function (scrollHeight, height, offsetTop, offsetBottom) {
  30. var scrollTop = this.$target.scrollTop()
  31. var position = this.$element.offset()
  32. var targetHeight = this.$target.height()
  33. if (offsetTop != null && this.affixed == 'top') return scrollTop < offsetTop ? 'top' : false
  34. if (this.affixed == 'bottom') {
  35. if (offsetTop != null) return (scrollTop + this.unpin <= position.top) ? false : 'bottom'
  36. return (scrollTop + targetHeight <= scrollHeight - offsetBottom) ? false : 'bottom'
  37. }
  38. var initializing = this.affixed == null
  39. var colliderTop = initializing ? scrollTop : position.top
  40. var colliderHeight = initializing ? targetHeight : height
  41. if (offsetTop != null && scrollTop <= offsetTop) return 'top'
  42. if (offsetBottom != null && (colliderTop + colliderHeight >= scrollHeight - offsetBottom)) return 'bottom'
  43. return false
  44. }
  45. Affix.prototype.getPinnedOffset = function () {
  46. if (this.pinnedOffset) return this.pinnedOffset
  47. this.$element.removeClass(Affix.RESET).addClass('affix')
  48. var scrollTop = this.$target.scrollTop()
  49. var position = this.$element.offset()
  50. return (this.pinnedOffset = position.top - scrollTop)
  51. }
  52. Affix.prototype.checkPositionWithEventLoop = function () {
  53. setTimeout($.proxy(this.checkPosition, this), 1)
  54. }
  55. Affix.prototype.checkPosition = function () {
  56. if (!this.$element.is(':visible')) return
  57. var height = this.$element.height()
  58. var offset = this.options.offset
  59. var offsetTop = offset.top
  60. var offsetBottom = offset.bottom
  61. var scrollHeight = Math.max($(document).height(), $(document.body).height())
  62. if (typeof offset != 'object') offsetBottom = offsetTop = offset
  63. if (typeof offsetTop == 'function') offsetTop = offset.top(this.$element)
  64. if (typeof offsetBottom == 'function') offsetBottom = offset.bottom(this.$element)
  65. var affix = this.getState(scrollHeight, height, offsetTop, offsetBottom)
  66. if (this.affixed != affix) {
  67. if (this.unpin != null) this.$element.css('top', '')
  68. var affixType = 'affix' + (affix ? '-' + affix : '')
  69. var e = $.Event(affixType + '.bs.affix')
  70. this.$element.trigger(e)
  71. if (e.isDefaultPrevented()) return
  72. this.affixed = affix
  73. this.unpin = affix == 'bottom' ? this.getPinnedOffset() : null
  74. this.$element
  75. .removeClass(Affix.RESET)
  76. .addClass(affixType)
  77. .trigger(affixType.replace('affix', 'affixed') + '.bs.affix')
  78. }
  79. if (affix == 'bottom') {
  80. this.$element.offset({
  81. top: scrollHeight - height - offsetBottom
  82. })
  83. }
  84. }
  85. // AFFIX PLUGIN DEFINITION
  86. // =======================
  87. function Plugin(option) {
  88. return this.each(function () {
  89. var $this = $(this)
  90. var data = $this.data('bs.affix')
  91. var options = typeof option == 'object' && option
  92. if (!data) $this.data('bs.affix', (data = new Affix(this, options)))
  93. if (typeof option == 'string') data[option]()
  94. })
  95. }
  96. var old = $.fn.affix
  97. $.fn.affix = Plugin
  98. $.fn.affix.Constructor = Affix
  99. // AFFIX NO CONFLICT
  100. // =================
  101. $.fn.affix.noConflict = function () {
  102. $.fn.affix = old
  103. return this
  104. }
  105. // AFFIX DATA-API
  106. // ==============
  107. $(window).on('load', function () {
  108. $('[data-spy="affix"]').each(function () {
  109. var $spy = $(this)
  110. var data = $spy.data()
  111. data.offset = data.offset || {}
  112. if (data.offsetBottom != null) data.offset.bottom = data.offsetBottom
  113. if (data.offsetTop != null) data.offset.top = data.offsetTop
  114. Plugin.call($spy, data)
  115. })
  116. })
  117. }(jQuery);