Skip to content

Commit

Permalink
round to nearest column if over by 1px;
Browse files Browse the repository at this point in the history
Fixes #427 #316
  • Loading branch information
desandro committed Dec 10, 2013
1 parent 36efc00 commit d5cabef
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 3 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "masonry",
"version": "3.1.2",
"version": "3.1.3",
"description": "Cascading grid layout library",
"main": "masonry.js",
"dependencies": {
Expand Down
67 changes: 67 additions & 0 deletions examples/element-sizing.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">

<title>element sizing</title>

<link rel="stylesheet" href="examples.css" />
<style>
#container { width: 501px; }
.item, .grid-sizer { width: 20%; }
.item.w2 { width: 40%; }
.item.w3 { width: 60%; }
</style>
</head>
<body>

<h1>basic</h1>

<div id="container" class="container">
<div class="grid-sizer"></div>
<div class="item"></div>
<div class="item h4"></div>
<div class="item w2 h2"></div>
<div class="item w2"></div>
<div class="item h3"></div>
<div class="item w3"></div>
<div class="item"></div>
<div class="item h4"></div>
<div class="item"></div>
<div class="item w2 h4"></div>
<div class="item w2"></div>
<div class="item h5"></div>
<div class="item w3"></div>
<div class="item"></div>
<div class="item h4"></div>
<div class="item"></div>
<div class="item w2 h5"></div>
<div class="item w2"></div>
<div class="item h3"></div>
<div class="item w3"></div>
<div class="item"></div>
</div>

<script src="../bower_components/eventEmitter/EventEmitter.js"></script>
<script src="../bower_components/eventie/eventie.js"></script>
<script src="../bower_components/doc-ready/doc-ready.js"></script>
<script src="../bower_components/get-style-property/get-style-property.js"></script>
<script src="../bower_components/get-size/get-size.js"></script>
<script src="../bower_components/jquery-bridget/jquery.bridget.js"></script>
<script src="../bower_components/matches-selector/matches-selector.js"></script>
<script src="../bower_components/outlayer/item.js"></script>
<script src="../bower_components/outlayer/outlayer.js"></script>

<script src="../masonry.js"></script>

<script>
docReady( function() {
var container = document.querySelector('#container');
var msnry = new Masonry( container, {
columnWidth: '.grid-sizer'
});
});
</script>

</body>
</html>
7 changes: 5 additions & 2 deletions masonry.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* Masonry v3.1.2
* Masonry v3.1.3
* Cascading grid layout library
* http://masonry.desandro.com
* MIT License
Expand Down Expand Up @@ -79,7 +79,10 @@ function masonryDefinition( Outlayer, getSize ) {
Masonry.prototype._getItemLayoutPosition = function( item ) {
item.getSize();
// how many columns does this brick span
var colSpan = Math.ceil( item.size.outerWidth / this.columnWidth );
var remainder = item.size.outerWidth % this.columnWidth;
var mathMethod = remainder && remainder < 1 ? 'round' : 'ceil';
// round if off by 1 pixel, otherwise use ceil
var colSpan = Math[ mathMethod ]( item.size.outerWidth / this.columnWidth );
colSpan = Math.min( colSpan, this.cols );

var colGroup = this._getColGroup( colSpan );
Expand Down
25 changes: 25 additions & 0 deletions test/element-sizing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
( function() {

'use strict';

test( 'pixel rounding', function() {
var container = document.querySelector('#element-sizing');
var msnry = new Masonry( '#element-sizing', {
columnWidth: '.grid-sizer',
itemSelector: '.item',
transitionDuration: 0
});

var lastItem = msnry.items[3];

var containerWidth = 170;
while ( containerWidth < 190 ) {
container.style.width = containerWidth + 'px';
msnry.layout();
equal( lastItem.position.y, 0,'4th item on top row, container width = ' + containerWidth );
containerWidth++;
}

});

})();
10 changes: 10 additions & 0 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<script src="fit-width.js"></script>
<script src="empty.js"></script>
<script src="zero-column-width.js"></script>
<script src="element-sizing.js"></script>

</head>
<body>
Expand Down Expand Up @@ -121,5 +122,14 @@ <h2>zero columnWidth</h2>
<div class="item"></div>
</div>

<h2>Element sizing</h2>
<div id="element-sizing" class="container">
<div class="grid-sizer"></div>
<div class="item"></div>
<div class="item w2"></div>
<div class="item"></div>
<div class="item"></div>
</div>

</body>
</html>
11 changes: 11 additions & 0 deletions test/tests.css
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,14 @@ body {
#zero-column-width .hidden {
display: none;
}

/* ---- pixel-rounding ---- */

#element-sizing {
width: 181px;
}

#element-sizing .item,
#element-sizing .grid-sizer { width: 20%; }

#element-sizing .item.w2 { width: 40%; }

0 comments on commit d5cabef

Please sign in to comment.