暂无描述

affix.js 4.8KB

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