add the get_cell_from_pixel() function

This commit is contained in:
Julien Chichignoud 2015-01-28 16:04:30 +01:00
commit c6a6ad5210
2 changed files with 21 additions and 0 deletions

View file

@ -226,6 +226,16 @@ grid.cell_height(grid.cell_width() * 1.2);
Gets current cell width.
### get_cell_from_pixel(position)
Get the position of the cell under a pixel on screen.
Parameters :
- `position` - the position of the pixel to resolve in absolute coordinates, as an object with `top` and `left`properties
Returns an object with properties `x` and `y` i.e. the column and row in the grid.
### locked(el, val)
Locks/unlocks widget.

View file

@ -714,6 +714,17 @@
return Math.ceil(o.outerWidth() / o.attr('data-gs-width'));
};
GridStack.prototype.get_cell_from_pixel = function(position) {
var containerPos = this.container.position();
var relativeLeft = position.left - containerPos.left;
var relativeTop = position.top - containerPos.top;
var column_width = Math.floor(this.container.width() / this.opts.width);
var row_height = this.opts.cell_height + this.opts.vertical_margin;
return {x: Math.floor(relativeLeft / column_width), y: Math.floor(relativeTop / row_height)};
};
scope.GridStackUI = GridStack;
scope.GridStackUI.Utils = Utils;