foundation.equalizer.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. ;(function ($, window, document, undefined) {
  2. 'use strict';
  3. Foundation.libs.equalizer = {
  4. name : 'equalizer',
  5. version : '5.1.1',
  6. settings : {
  7. use_tallest: true,
  8. before_height_change: $.noop,
  9. after_height_change: $.noop
  10. },
  11. init : function (scope, method, options) {
  12. this.bindings(method, options);
  13. this.reflow();
  14. },
  15. events : function () {
  16. this.S(window).off('.equalizer').on('resize.fndtn.equalizer', function(e){
  17. this.reflow();
  18. }.bind(this));
  19. },
  20. equalize: function(equalizer) {
  21. var isStacked = false,
  22. vals = equalizer.find('[' + this.attr_name() + '-watch]'),
  23. firstTopOffset = vals.first().offset().top,
  24. settings = equalizer.data(this.attr_name(true)+'-init');
  25. if (vals.length === 0) return;
  26. settings.before_height_change();
  27. equalizer.trigger('before-height-change');
  28. vals.height('inherit');
  29. vals.each(function(){
  30. var el = $(this);
  31. if (el.offset().top !== firstTopOffset) {
  32. isStacked = true;
  33. }
  34. });
  35. if (isStacked) return;
  36. var heights = vals.map(function(){ return $(this).outerHeight() });
  37. if (settings.use_tallest) {
  38. var max = Math.max.apply(null, heights);
  39. vals.height(max);
  40. } else {
  41. var min = Math.min.apply(null, heights);
  42. vals.height(min);
  43. }
  44. settings.after_height_change();
  45. equalizer.trigger('after-height-change');
  46. },
  47. reflow : function () {
  48. var self = this;
  49. this.S('[' + this.attr_name() + ']', this.scope).each(function(){
  50. self.equalize($(this));
  51. });
  52. }
  53. };
  54. }(jQuery, this, this.document));