summaryrefslogtreecommitdiffstats
path: root/jquery.equalheights.js
blob: cc9421268101be3b147fd4a5cee47f648cc249e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*!
 * Simple jQuery Equal Heights
 *
 * Copyright (c) 2015 Matt Banks
 * Dual licensed under the MIT and GPL licenses.
 * Uses the same license as jQuery, see:
 * http://docs.jquery.com/License
 *
 * @version 1.5.3
 */
(function($) {

    var watched = [],
    calcMaxHeight = function($elems) {
        var height, maxHeight = 0;
        $elems.each(function () {
            height = $(this).innerHeight();
            if ( height > maxHeight ) { maxHeight = height; }
        });
        return maxHeight;
    },
    height = 'height',
    docEl = document.documentElement;

    $(window).on('resize', function () {
        // Bundle reading and writing styles to reduce synchronous layout / jank

        // Reset heights
        for (var i = 0, l = watched.length, elems, _w = [], m = []; i < l; i++) {
            elems = watched[i];
            // Don't waste time on elements that aren't in the DOM
            if (elems.length && $.contains(docEl, elems[0]) ) {
                _w.push(elems);
                elems.css(height, 'auto');
            }
        }

        // Calc max height
        for (i = 0, l = _w.length; i < l; i++) { m[i] = calcMaxHeight(_w[i]); }

        // Set max height
        for (i = 0; i < l; i++) { _w[i].css(height, m[i]); }
    });

    $.fn.equalHeights = function(options) {
        var maxHeight, $this = $(this), i, l, isContained, res, loop;
        options = options || {};

        maxHeight = calcMaxHeight($this);

        if (options.watch) {
            for (i = 0, l = watched.length, isContained; i < l; i++) {
                if ($this.is(watched[i])) {
                    isContained = true;
                    break;
                }
            }
            if (!isContained) {
                watched.push($this);
            }
        }

        if (options.unwatch) {
            for (i = 0, l = watched.length, res = []; i < l; i++) {
                if (!$this.is(watched[i])) { res.push(watched[i]); }
            }
            watched = res;
            return this;
        }

        if (options.wait) {
            loop = setInterval(function() {
                if(maxHeight > 0) {
                    clearInterval(loop);
                    return $this.css(height, maxHeight);
                }
                maxHeight = calcMaxHeight($this);
            }, 100);
            return this;
        } else {
            return $this.css(height, maxHeight);
        }
    };

    // Auto-initialize plugin
    $(document).on('ready', function() {
       $('[data-equal]').each(function(){
            var $this = $(this),
                options = $this.data(),
                target = options.equal;
            $this.find(target).equalHeights(options);
        });
    });

})(jQuery);