View difference between Paste ID: LXG3hVZD and k18jLdDX
SHOW: | | - or go back to the newest paste.
1
// Make sure Object.create is available in the browser (for our prototypal inheritance)
2
// Courtesy of Papa Crockford
3
// Note this is not entirely equal to native Object.create, but compatible with our use-case
4
if (typeof Object.create !== 'function') {
5
    Object.create = function (o) {
6
        function F() {} // optionally move this outside the declaration and into a closure if you need more speed.
7
        F.prototype = o;
8
        return new F();
9
    };
10
}
11
12
;(function($, window, document, undefined) {
13
  var EqualHeights = {
14
    init: function(options, elems) {
15
      var self = this;
16
      self.elems = elems;
17
      self.$elems = $(elems);
18
      self.options = $.extend( {}, $.fn.equalHeights.options, options );
19
20
      $(window).on('load resize orientationchange', function() {
21
22
        if (self.checkExecution()) {
23
24
          // We need to reset the heights no matter what
25
          self.resetHeights();
26
27
          self.hh = 0;
28
          self.$elems.each(function(){
29
            if ($(this).innerHeight() > self.hh) {
30
              self.hh = $(this).innerHeight();
31
            }
32
          });
33
          self.setHeights(self.hh);
34
        }
35
        else {
36
          self.resetHeights();
37
        }
38
      });
39
    },
40
41
    // Based on min and max values we figure out if we should execute
42
    checkExecution: function() {
43
      var self = this;
44
      var ww = $(window).width();
45
      return (ww >= self.options.min && ww < self.options.max);
46
    },
47
48
    resetHeights: function() {
49
      var self = this;
50
      var height = {};
51
      height[self.options.property] = '';
52
      self.$elems.css(height);
53
    },
54
55
    setHeights: function(hh) {
56
      var self = this;
57
      var height = {};
58
      height[self.options.property] = hh;
59
      self.$elems.css(height);
60
    }
61
62
  };
63
64
  $.fn.equalHeights = function(options) {
65
    var equalHeights = Object.create(EqualHeights);
66
    equalHeights.init(options, this);
67
    return this;
68
  };
69
70
  $.fn.equalHeights.options = {
71
    min: 0,
72
    max: 10000,
73
    property: 'min-height'
74
  };
75
76
})(jQuery, window, document);