My personal site (brandoncornejo.name) (binaryatrocity.name)
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.

140 lines
4.2 KiB

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