Skip to content

Commit d497130

Browse files
Adjusted Gruntfile and restructured code
1 parent 1b30cd7 commit d497130

8 files changed

+226
-29
lines changed

Gruntfile.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ module.exports = function( grunt ) {
1212
" * <%= pkg.description %>\n" +
1313
" * <%= pkg.homepage %>\n" +
1414
" *\n" +
15-
" * Made by <%= pkg.author.name %>\n" +
15+
" * Author <%= pkg.author.name %>\n" +
16+
" * Website <%= pkg.author.url %>\n" +
1617
" * Under <%= pkg.license %> License\n" +
1718
" */\n"
1819
},
@@ -22,35 +23,34 @@ module.exports = function( grunt ) {
2223
banner: "<%= meta.banner %>"
2324
},
2425
dist: {
25-
src: [ "src/jquery.boilerplate.js" ],
26-
dest: "dist/jquery.boilerplate.js"
26+
src: [ "src/jquery.col-same-height.js" ],
27+
dest: "dist/jquery.col-same-height.js"
2728
}
2829
},
2930

3031
uglify: {
3132
dist: {
32-
src: [ "dist/jquery.boilerplate.js" ],
33-
dest: "dist/jquery.boilerplate.min.js"
33+
src: [ "src/jquery.col-same-height.js" ],
34+
dest: "dist/jquery.col-same-height.min.js"
3435
},
3536
options: {
3637
banner: "<%= meta.banner %>"
3738
}
3839
},
39-
40+
jshint: {
41+
all: ["src/jquery.col-same-height.js"]
42+
},
4043
watch: {
41-
files: [ "src/*", "test/**/*" ],
42-
tasks: [ "default" ]
44+
files: [ "src/*.js" ],
45+
tasks: [ "build" ]
4346
}
4447

4548
} );
4649

4750
grunt.loadNpmTasks( "grunt-contrib-concat" );
4851
grunt.loadNpmTasks( "grunt-contrib-jshint" );
49-
grunt.loadNpmTasks( "grunt-jscs" );
5052
grunt.loadNpmTasks( "grunt-contrib-uglify" );
51-
grunt.loadNpmTasks( "grunt-contrib-coffee" );
5253
grunt.loadNpmTasks( "grunt-contrib-watch" );
53-
grunt.loadNpmTasks( "grunt-karma" );
5454

5555
grunt.registerTask( "build", [ "jshint", "concat", "uglify" ] );
5656
};

README.md

+35-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ If you have some columns you wish to set as the same height as each other just g
2323

2424
In your javascript file, target those elements with that class and initialize the plugin for those elements
2525

26+
## JavaScript
27+
2628
```javascript
2729
$(document).ready(function() {
2830
$('.col-same-height').colSameHeight();
@@ -33,6 +35,8 @@ $(document).ready(function() {
3335

3436
You might not want the columns to be the same height on certain screen sizes. Lets say the columns stack on top of each other when the width of the screen is less than 800px. We can define our own callback breakpoint functions. Lets take our example from before and add a `data-col-break` attribute. The value you set will be the name of the callback function. In this case we will name it 'lg':
3537

38+
## HTML
39+
3640
```html
3741
<div class="col-same-height" data-col-break="lg">
3842
some text
@@ -47,6 +51,8 @@ You might not want the columns to be the same height on certain screen sizes. Le
4751

4852
Now lets define our own callback method. It should return true if we want the columns to be the same height or false if we don't.
4953

54+
## JavaScript
55+
5056
```javascript
5157
$(document).ready(function() {
5258
$('.col-same-height').colSameHeight({
@@ -64,4 +70,32 @@ $(document).ready(function() {
6470
})
6571
```
6672

67-
Now when you play around with the size of the window you can see that the columns will only be the same height when the window is 800px or larger.
73+
Now when you play around with the size of the window you can see that the columns will only be the same height when the window is 800px or larger.
74+
75+
# Grouping Columns
76+
77+
While its easy to separate your columns from eachother by just doing this in your script file:
78+
79+
```javascript
80+
$('.col-same-height').colSameHeight();
81+
$('.col-same-height-alt').colSameHeight();
82+
```
83+
84+
You can reuse the same class if needed and just add them to a group with the `data-col-group` attribute like so
85+
86+
```html
87+
<!-- row 1 -->
88+
<div class="col-same-height" data-col-group="row-1"></div>
89+
<div class="col-same-height" data-col-group="row-1"></div>
90+
<div class="col-same-height" data-col-group="row-1"></div>
91+
<!-- row 2 -->
92+
<div class="col-same-height" data-col-group="row-2"></div>
93+
<div class="col-same-height" data-col-group="row-2"></div>
94+
<div class="col-same-height" data-col-group="row-2"></div>
95+
```
96+
97+
This allows you to group your columns within your HTML.
98+
99+
# Known Issues
100+
101+
- Columns within an element with css `display: flex` have some issues when resizing the window. The height is slightly off

demo/demo.html

+26-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
55
</head>
66
<link rel="stylesheet" type="text/css" href="styles.css">
77
<body>
8-
<div class="container">
8+
<div class="container clearfix">
99
<div class="col col-same-height">
1010
<h1>Text</h1>
1111
<p>this is just going to have a little bit of text</p>
12-
<img src="http://img08.deviantart.net/c3f4/i/2012/308/6/7/super_metroid_by_thiago_almeida-d5jz8cg.jpg" class="img-responsive">
1312
<a class="button">A Button</a>
1413
</div>
1514
<div class="col col-same-height">
@@ -29,6 +28,31 @@ <h1>Last column</h1>
2928
<a class="button">A Button</a>
3029
</div>
3130
</div>
31+
32+
<hr class="clear">
33+
34+
<div class="container clearfix">
35+
<div class="col col-same-height" data-col-group="img">
36+
<h1>Text</h1>
37+
<p>this is just going to have a little bit of text but the image should make the height larger</p>
38+
<img src="http://img08.deviantart.net/c3f4/i/2012/308/6/7/super_metroid_by_thiago_almeida-d5jz8cg.jpg" class="img-responsive">
39+
40+
<a class="button">A Button</a>
41+
</div>
42+
<div class="col col-same-height" data-col-group="img">
43+
44+
<h1>Some text</h1>
45+
<p>When the page loads this column should be the tallest. However, the left column has an image which will need to be loaded. This will then force a refresh of the column sizes after it loads making the left column taller and the rest the same size.
46+
</p>
47+
48+
<a class="button">A Button</a>
49+
</div>
50+
<div class="col col-same-height" data-col-group="img">
51+
<h1>Last column</h1>
52+
<p>this will also have a little bit of text</p>
53+
<a class="button">A Button</a>
54+
</div>
55+
</div>
3256
</body>
3357

3458
<script type="text/javascript" src="../node_modules/jquery/dist/jquery.min.js"></script>

demo/styles.css

+19
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,23 @@
3030
.img-responsive {
3131
max-width: 100%;
3232
height: auto;
33+
}
34+
35+
.clear {
36+
margin: 20px 0;
37+
clear: both;
38+
}
39+
40+
.clearfix:after {
41+
clear: both;
42+
content: ".";
43+
display: block;
44+
height: 0;
45+
visibility: hidden;
46+
}
47+
.clearfix {
48+
display: inline-block;
49+
}
50+
.clearfix {
51+
display: block;
3352
}

dist/jquery.col-same-height.js

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* col-same-height - v1.0.0
3+
* A jQuery plugin to keep columns at the same height
4+
*
5+
*
6+
* Author Joseph McMurray (pixel programmer)
7+
* Website http://pixelprogrammer.net
8+
* Under ISC License
9+
*/
10+
(function($) {
11+
12+
var settings = null;
13+
14+
$.fn.colSameHeight = function(options) {
15+
16+
var el = this;
17+
18+
var defaults = {
19+
breakpointCBs: {}, // breakpoint callback functions
20+
};
21+
22+
settings = $.extend(defaults, options);
23+
24+
setColSameHeight(el);
25+
$(window).resize(function() {
26+
setColSameHeight(el);
27+
});
28+
};
29+
30+
function getCols(el) {
31+
32+
var rows = {}; // we will use key maps
33+
el.each(function() {
34+
35+
// check if there is a break point to stop setting its height
36+
var breakpoint = $(this).attr('data-col-break');
37+
38+
if( $.isFunction(settings.breakpointCBs[breakpoint]) ) {
39+
// if false then we don't want to do anything
40+
if( !settings.breakpointCBs[breakpoint].call(this) ) {
41+
return;
42+
}
43+
}
44+
45+
var group = $(this).attr('data-col-group');
46+
if( !(group in rows) ) {
47+
rows[group] = [];
48+
}
49+
50+
rows[group].push(this);
51+
});
52+
53+
return rows;
54+
}
55+
56+
function setColSameHeight(el) {
57+
// reset the height so we can calculate it
58+
el.css('height', 'auto');
59+
60+
var rows = getCols(el);
61+
var max_height;
62+
var height;
63+
64+
for(var group in rows) {
65+
66+
max_height = 0;
67+
68+
if( rows.hasOwnProperty(group) ) {
69+
70+
for( var i=0; i<rows[group].length; i++) {
71+
var element = $(rows[group][i]);
72+
73+
var imgs = element.find('img');
74+
for(var j=0; j < imgs.length; j++) {
75+
attachImgHandler(imgs[j], rows[group]);
76+
}
77+
78+
height = element.outerHeight();
79+
80+
if( height > max_height ) {
81+
max_height = height;
82+
}
83+
}
84+
85+
$(rows[group]).css('height', max_height);
86+
}
87+
}
88+
}
89+
90+
function colEqualImgLoad(e) {
91+
setColSameHeight(e.data.el);
92+
93+
$(e.currentTarget).off('load', colEqualImgLoad);
94+
}
95+
96+
function attachImgHandler(img, group) {
97+
// attach a load event for when the image has not fully loaded.
98+
// We want to fire setColSameHeight() again as the img height from a loaded img can change the column height after the page has loaded
99+
if(!$(img).prop('complete')) {
100+
// pass in a reference to the group elements for the load object
101+
$(img).on('load', {el: $(group)}, colEqualImgLoad);
102+
}
103+
}
104+
}(jQuery));

dist/jquery.col-same-height.min.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/*
2+
* col-same-height - v1.0.0
3+
* A jQuery plugin to keep columns at the same height
4+
*
5+
*
6+
* Author Joseph McMurray (pixel programmer)
7+
* Website http://pixelprogrammer.net
8+
* Under ISC License
9+
*/
10+
!function(a){function b(b){var c={};return b.each(function(){var b=a(this).attr("data-col-break");if(!a.isFunction(f.breakpointCBs[b])||f.breakpointCBs[b].call(this)){var d=a(this).attr("data-col-group");d in c||(c[d]=[]),c[d].push(this)}}),c}function c(c){c.css("height","auto");var d,f,g=b(c);for(var h in g)if(d=0,g.hasOwnProperty(h)){for(var i=0;i<g[h].length;i++){for(var j=a(g[h][i]),k=j.find("img"),l=0;l<k.length;l++)e(k[l],g[h]);f=j.outerHeight(),f>d&&(d=f)}a(g[h]).css("height",d)}}function d(b){c(b.data.el),a(b.currentTarget).off("load",d)}function e(b,c){a(b).prop("complete")||a(b).on("load",{el:a(c)},d)}var f=null;a.fn.colSameHeight=function(b){var d=this,e={breakpointCBs:{}};f=a.extend(e,b),c(d),a(window).resize(function(){c(d)})}}(jQuery);

package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
"height",
1212
"jquery"
1313
],
14-
"author": "Joseph McMurray (pixel programmer)",
14+
"author": {
15+
"name": "Joseph McMurray (pixel programmer)",
16+
"url": "http://pixelprogrammer.net"
17+
},
1518
"license": "ISC",
1619
"devDependencies": {
1720
"grunt": "^1.0.1",

src/jquery.col-same-height.js

+17-14
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
$(window).resize(function() {
1717
setColSameHeight(el);
1818
});
19-
}
19+
};
2020

2121
function getCols(el) {
2222

@@ -61,14 +61,10 @@
6161
for( var i=0; i<rows[group].length; i++) {
6262
var element = $(rows[group][i]);
6363

64-
element.find('img').each(function() {
65-
// attach a load event for when the image has not fully loaded.
66-
// We want to fire setColSameHeight() again as img height can change on load
67-
if(!$(this).prop('complete')) {
68-
// pass in a reference to the element for the load object
69-
$(this).on('load', {el: $(rows[group])}, col_equal_img_load);
70-
}
71-
});
64+
var imgs = element.find('img');
65+
for(var j=0; j < imgs.length; j++) {
66+
attachImgHandler(imgs[j], rows[group]);
67+
}
7268

7369
height = element.outerHeight();
7470

@@ -77,16 +73,23 @@
7773
}
7874
}
7975

80-
$(rows[group]).each(function() {
81-
$(this).css('height', max_height);
82-
});
76+
$(rows[group]).css('height', max_height);
8377
}
8478
}
8579
}
8680

87-
function col_equal_img_load(e) {
81+
function colEqualImgLoad(e) {
8882
setColSameHeight(e.data.el);
8983

90-
$(e.currentTarget).off('load', col_equal_img_load);
84+
$(e.currentTarget).off('load', colEqualImgLoad);
85+
}
86+
87+
function attachImgHandler(img, group) {
88+
// attach a load event for when the image has not fully loaded.
89+
// We want to fire setColSameHeight() again as the img height from a loaded img can change the column height after the page has loaded
90+
if(!$(img).prop('complete')) {
91+
// pass in a reference to the group elements for the load object
92+
$(img).on('load', {el: $(group)}, colEqualImgLoad);
93+
}
9194
}
9295
}(jQuery));

0 commit comments

Comments
 (0)