Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

160 řádky
4.8 KiB

  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. define([
  6. 'jquery',
  7. 'jquery-ui-modules/widget'
  8. ], function ($) {
  9. 'use strict';
  10. $.widget('mage.sticky', {
  11. options: {
  12. /**
  13. * Element selector, who's height will be used to restrict the
  14. * maximum offsetTop position of the stuck element.
  15. * Default uses document body.
  16. * @type {String}
  17. */
  18. container: '',
  19. /**
  20. * Spacing in pixels above the stuck element
  21. * @type {Number|Function} Number or Function that will return a Number
  22. */
  23. spacingTop: 0,
  24. /**
  25. * Allows postponing sticking, until element will go out of the
  26. * screen for the number of pixels.
  27. * @type {Number|Function} Number or Function that will return a Number
  28. */
  29. stickAfter: 0,
  30. /**
  31. * CSS class for active sticky state
  32. * @type {String}
  33. */
  34. stickyClass: '_sticky'
  35. },
  36. /**
  37. * Retrieve option value
  38. * @param {String} option
  39. * @return {*}
  40. * @private
  41. */
  42. _getOptionValue: function (option) {
  43. var value = this.options[option] || 0;
  44. if (typeof value === 'function') {
  45. value = this.options[option]();
  46. }
  47. return value;
  48. },
  49. /**
  50. * Bind handlers to scroll event
  51. * @private
  52. */
  53. _create: function () {
  54. $(window).on({
  55. 'scroll': $.proxy(this._stick, this),
  56. 'resize': $.proxy(this.reset, this)
  57. });
  58. this.element.on('dimensionsChanged', $.proxy(this.reset, this));
  59. this.reset();
  60. // Application of the workaround for IE11 and Edge
  61. this.normalizeIE11AndEdgeScroll();
  62. },
  63. /**
  64. * float Block on windowScroll
  65. * @private
  66. */
  67. _stick: function () {
  68. var offset,
  69. isStatic,
  70. stuck,
  71. stickAfter;
  72. isStatic = this.element.css('position') === 'static';
  73. if (!isStatic && this.element.is(':visible')) {
  74. offset = $(document).scrollTop() -
  75. this.parentOffset +
  76. this._getOptionValue('spacingTop');
  77. offset = Math.max(0, Math.min(offset, this.maxOffset));
  78. stuck = this.element.hasClass(this.options.stickyClass);
  79. stickAfter = this._getOptionValue('stickAfter');
  80. if (offset && !stuck && offset < stickAfter) {
  81. offset = 0;
  82. }
  83. this.element
  84. .toggleClass(this.options.stickyClass, offset > 0)
  85. .css('top', offset);
  86. }
  87. },
  88. /**
  89. * Defines maximum offset value of the element.
  90. * @private
  91. */
  92. _calculateDimens: function () {
  93. var $parent = this.element.parent(),
  94. topMargin = parseInt(this.element.css('margin-top'), 10),
  95. parentHeight = $parent.height() - topMargin,
  96. height = this.element.innerHeight(),
  97. maxScroll = document.body.offsetHeight - window.innerHeight;
  98. if (this.options.container.length > 0) {
  99. maxScroll = $(this.options.container).height();
  100. }
  101. this.parentOffset = $parent.offset().top + topMargin;
  102. this.maxOffset = maxScroll - this.parentOffset;
  103. if (this.maxOffset + height >= parentHeight) {
  104. this.maxOffset = parentHeight - height;
  105. }
  106. return this;
  107. },
  108. /**
  109. * Facade method that places sticky element where it should be.
  110. */
  111. reset: function () {
  112. this._calculateDimens()
  113. ._stick();
  114. },
  115. /**
  116. * Workaround for IE11 and Edge that solves the IE known rendering issue
  117. * that prevents sticky element from jumpy movement on scrolling the page.
  118. *
  119. * Alternatively, undesired jumpy movement can be eliminated by changing the setting in IE:
  120. * Settings > Internet options > Advanced tab > inside 'Browsing' item > set 'Use smooth scrolling' to False
  121. */
  122. normalizeIE11AndEdgeScroll: function () {
  123. if (navigator.userAgent.match(/Trident.*rv[ :]*11\.|Edge\//)) {
  124. document.body.addEventListener('mousewheel', function () {
  125. event.preventDefault();
  126. window.scrollTo(0, window.pageYOffset - event.wheelDelta);
  127. });
  128. }
  129. }
  130. });
  131. return $.mage.sticky;
  132. });