-
Notifications
You must be signed in to change notification settings - Fork 10
/
jquery.eqheight.coffee
97 lines (75 loc) · 3.33 KB
/
jquery.eqheight.coffee
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
96
97
###
eqHeight.coffee v1.3.5
http://jsliang.github.com/eqHeight.coffee
Copyright (c) 2013-2014, Jui-Shan Liang <[email protected]>
All rights reserved.
Licensed under GPL v2.
###
# Reference jQuery
$ = jQuery
# Adds plugin object to jQuery
$.fn.extend
eqHeight: (column_selector, option = {equalize_interval: null, break_point: null}) ->
this.each ()->
# each row needs a timer
timer = null
columns = $(this).find(column_selector)
if columns.length is 0
columns = $(this).children()
# Stop if there is no column selected
if columns.length is 0 then return
#
# _equalize_marked_columns: a function that equalize
# height values of marked column elements
#
_equalize_marked_columns = () ->
marked_columns = $(".eqHeight_row")
# Get max height of marked_columns
max_col_height = 0
marked_columns.each () ->
max_col_height = Math.max($(this).outerHeight(), max_col_height)
# Set all marked_columns to max_col_height
marked_columns.height(max_col_height)
# Unmark column
$(".eqHeight_row").removeClass("eqHeight_row")
#
# equalizer: a function that equalizes column heights
#
equalizer = () ->
# Reset column height to default
columns.height("auto")
# do not equalize columns when browser display size is under break_point
if typeof option.break_point is "number" and $(window).width() <= option.break_point
return
# Group columns by rows
row_top_value = columns.first().position().top
columns.each () ->
current_top = $(this).position().top
if current_top isnt row_top_value
# Equalize heights of marked columns
_equalize_marked_columns()
# Update row_top_value
row_top_value = $(this).position().top
# Mark the column element
$(this).addClass("eqHeight_row")
# Equalize heights of marked columns
_equalize_marked_columns()
# Lets prevent a repaint proces on every resize event and only
# equalize if we are done resizing.
start_equalizing = () ->
clearTimeout timer
timer = setTimeout(equalizer, 100)
infinite_equalizing = () ->
equalizer()
timer = setTimeout(infinite_equalizing, option.equalize_interval)
#
# Call equalizer()
#
# Equalize column heights after all contents on the page have been loaded
$(window).load(equalizer)
if typeof option.equalize_interval is "number"
# Equalize column heights on every option.equalize_interval ms
infinite_equalizing()
else
# Equalize column heights on resize
$(window).resize(start_equalizing)