foundation.equalizer.js 1.9 KB

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