DotaNoobs main site.
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.

147 lines
4.3 KiB

  1. /*! UIkit 2.5.0 | http://www.getuikit.com | (c) 2014 YOOtheme | MIT License */
  2. (function(addon) {
  3. if (typeof define == "function" && define.amd) { // AMD
  4. define("uikit-sticky", ["uikit"], function(){
  5. return jQuery.fn.uksticky || addon(window, window.jQuery, window.jQuery.UIkit);
  6. });
  7. }
  8. if(window && window.jQuery && window.jQuery.UIkit) {
  9. addon(window, window.jQuery, window.jQuery.UIkit);
  10. }
  11. })(function(global, $, UI){
  12. var defaults = {
  13. top : 0,
  14. bottom : 0,
  15. clsactive : 'uk-active',
  16. clswrapper : 'uk-sticky',
  17. getWidthFrom : ''
  18. },
  19. $window = $(window),
  20. $document = $(document),
  21. sticked = [],
  22. windowHeight = $window.height(),
  23. scroller = function() {
  24. var scrollTop = $window.scrollTop(),
  25. documentHeight = $document.height(),
  26. dwh = documentHeight - windowHeight,
  27. extra = (scrollTop > dwh) ? dwh - scrollTop : 0;
  28. for (var i = 0; i < sticked.length; i++) {
  29. if(!sticked[i].stickyElement.is(":visible")) {
  30. continue;
  31. }
  32. var s = sticked[i], elementTop = s.stickyWrapper.offset().top, etse = elementTop - s.top - extra;
  33. if (scrollTop <= etse) {
  34. if (s.currentTop !== null) {
  35. s.stickyElement.css({"position":"", "top":"", "width":""}).parent().removeClass(s.clsactive);
  36. s.currentTop = null;
  37. }
  38. } else {
  39. var newTop = documentHeight - s.stickyElement.outerHeight() - s.top - s.bottom - scrollTop - extra;
  40. newTop = newTop<0 ? newTop + s.top : s.top;
  41. if (s.currentTop != newTop) {
  42. s.stickyElement.css({"position": "fixed", "top": newTop, "width": s.stickyElement.width()});
  43. if (typeof s.getWidthFrom !== 'undefined') {
  44. s.stickyElement.css('width', $(s.getWidthFrom).width());
  45. }
  46. s.stickyElement.parent().addClass(s.clsactive);
  47. s.currentTop = newTop;
  48. }
  49. }
  50. }
  51. },
  52. resizer = function() {
  53. windowHeight = $window.height();
  54. },
  55. methods = {
  56. init: function(options) {
  57. var o = $.extend({}, defaults, options);
  58. return this.each(function() {
  59. var stickyElement = $(this);
  60. if(stickyElement.data("sticky")) return;
  61. var stickyId = stickyElement.attr('id') || ("s"+Math.ceil(Math.random()*10000)),
  62. wrapper = $('<div></div>').attr('id', 'sticky-'+stickyId).addClass(o.clswrapper);
  63. stickyElement.wrapAll(wrapper);
  64. if (stickyElement.css("float") == "right") {
  65. stickyElement.css({"float":"none"}).parent().css({"float":"right"});
  66. }
  67. stickyElement.data("sticky", true);
  68. var stickyWrapper = stickyElement.parent();
  69. stickyWrapper.css('height', stickyElement.outerHeight());
  70. sticked.push({
  71. top: o.top,
  72. bottom: o.bottom,
  73. stickyElement: stickyElement,
  74. currentTop: null,
  75. stickyWrapper: stickyWrapper,
  76. clsactive: o.clsactive,
  77. getWidthFrom: o.getWidthFrom
  78. });
  79. });
  80. },
  81. update: scroller
  82. };
  83. // should be more efficient than using $window.scroll(scroller) and $window.resize(resizer):
  84. window.addEventListener('scroll', scroller, false);
  85. window.addEventListener('resize', resizer, false);
  86. $.fn.uksticky = function(method) {
  87. if (methods[method]) {
  88. return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
  89. } else if (typeof method === 'object' || !method ) {
  90. return methods.init.apply( this, arguments );
  91. } else {
  92. $.error('Method ' + method + ' does not exist on jQuery.sticky');
  93. }
  94. };
  95. $(document).on("uk-domready", function(e) {
  96. setTimeout(function(){
  97. scroller();
  98. $("[data-uk-sticky]").each(function(){
  99. var $ele = $(this);
  100. if(!$ele.data("sticky")) $ele.uksticky(UI.Utils.options($ele.attr('data-uk-sticky')));
  101. });
  102. }, 0);
  103. });
  104. return $.fn.uksticky;
  105. });