foundation.magellan.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. ;(function ($, window, document, undefined) {
  2. 'use strict';
  3. Foundation.libs.magellan = {
  4. name : 'magellan',
  5. version : '5.0.0',
  6. settings : {
  7. active_class: 'active',
  8. threshold: 0
  9. },
  10. init : function (scope, method, options) {
  11. this.fixed_magellan = $("[data-magellan-expedition]");
  12. this.set_threshold();
  13. this.last_destination = $('[data-magellan-destination]').last();
  14. this.events();
  15. },
  16. events : function () {
  17. var self = this;
  18. $(this.scope)
  19. .off('.magellan')
  20. .on('arrival.fndtn.magellan', '[data-magellan-arrival]', function (e) {
  21. var $destination = $(this),
  22. $expedition = $destination.closest('[data-magellan-expedition]'),
  23. active_class = $expedition.attr('data-magellan-active-class')
  24. || self.settings.active_class;
  25. $destination
  26. .closest('[data-magellan-expedition]')
  27. .find('[data-magellan-arrival]')
  28. .not($destination)
  29. .removeClass(active_class);
  30. $destination.addClass(active_class);
  31. });
  32. this.fixed_magellan
  33. .off('.magellan')
  34. .on('update-position.fndtn.magellan', function() {
  35. var $el = $(this);
  36. })
  37. .trigger('update-position');
  38. $(window)
  39. .off('.magellan')
  40. .on('resize.fndtn.magellan', function() {
  41. this.fixed_magellan.trigger('update-position');
  42. }.bind(this))
  43. .on('scroll.fndtn.magellan', function() {
  44. var windowScrollTop = $(window).scrollTop();
  45. self.fixed_magellan.each(function() {
  46. var $expedition = $(this);
  47. if (typeof $expedition.data('magellan-top-offset') === 'undefined') {
  48. $expedition.data('magellan-top-offset', $expedition.offset().top);
  49. }
  50. if (typeof $expedition.data('magellan-fixed-position') === 'undefined') {
  51. $expedition.data('magellan-fixed-position', false);
  52. }
  53. var fixed_position = (windowScrollTop + self.settings.threshold) > $expedition.data("magellan-top-offset");
  54. var attr = $expedition.attr('data-magellan-top-offset');
  55. if ($expedition.data("magellan-fixed-position") != fixed_position) {
  56. $expedition.data("magellan-fixed-position", fixed_position);
  57. if (fixed_position) {
  58. $expedition.addClass('fixed');
  59. $expedition.css({position:"fixed", top:0});
  60. } else {
  61. $expedition.removeClass('fixed');
  62. $expedition.css({position:"", top:""});
  63. }
  64. if (fixed_position && typeof attr != 'undefined' && attr != false) {
  65. $expedition.css({position:"fixed", top:attr + "px"});
  66. }
  67. }
  68. });
  69. });
  70. if (this.last_destination.length > 0) {
  71. $(window).on('scroll.fndtn.magellan', function (e) {
  72. var windowScrollTop = $(window).scrollTop(),
  73. scrolltopPlusHeight = windowScrollTop + $(window).height(),
  74. lastDestinationTop = Math.ceil(self.last_destination.offset().top);
  75. $('[data-magellan-destination]').each(function () {
  76. var $destination = $(this),
  77. destination_name = $destination.attr('data-magellan-destination'),
  78. topOffset = $destination.offset().top - $destination.outerHeight(true) - windowScrollTop;
  79. if (topOffset <= self.settings.threshold) {
  80. $("[data-magellan-arrival='" + destination_name + "']").trigger('arrival');
  81. }
  82. // In large screens we may hit the bottom of the page and dont reach the top of the last magellan-destination, so lets force it
  83. if (scrolltopPlusHeight >= $(self.scope).height() && lastDestinationTop > windowScrollTop && lastDestinationTop < scrolltopPlusHeight) {
  84. $('[data-magellan-arrival]').last().trigger('arrival');
  85. }
  86. });
  87. });
  88. }
  89. },
  90. set_threshold : function () {
  91. if (typeof this.settings.threshold !== 'number') {
  92. this.settings.threshold = (this.fixed_magellan.length > 0) ?
  93. this.fixed_magellan.outerHeight(true) : 0;
  94. }
  95. },
  96. off : function () {
  97. $(this.scope).off('.fndtn.magellan');
  98. $(window).off('.fndtn.magellan');
  99. },
  100. reflow : function () {}
  101. };
  102. }(jQuery, this, this.document));