Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c6a6ad5

Browse files
committed
add the get_cell_from_pixel() function
1 parent 3f992b9 commit c6a6ad5

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,16 @@ grid.cell_height(grid.cell_width() * 1.2);
226226

227227
Gets current cell width.
228228

229+
### get_cell_from_pixel(position)
230+
231+
Get the position of the cell under a pixel on screen.
232+
233+
Parameters :
234+
235+
- `position` - the position of the pixel to resolve in absolute coordinates, as an object with `top` and `left`properties
236+
237+
Returns an object with properties `x` and `y` i.e. the column and row in the grid.
238+
229239
### locked(el, val)
230240

231241
Locks/unlocks widget.

src/gridstack.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,17 @@
714714
return Math.ceil(o.outerWidth() / o.attr('data-gs-width'));
715715
};
716716

717+
GridStack.prototype.get_cell_from_pixel = function(position) {
718+
var containerPos = this.container.position();
719+
var relativeLeft = position.left - containerPos.left;
720+
var relativeTop = position.top - containerPos.top;
721+
722+
var column_width = Math.floor(this.container.width() / this.opts.width);
723+
var row_height = this.opts.cell_height + this.opts.vertical_margin;
724+
725+
return {x: Math.floor(relativeLeft / column_width), y: Math.floor(relativeTop / row_height)};
726+
};
727+
717728
scope.GridStackUI = GridStack;
718729

719730
scope.GridStackUI.Utils = Utils;

0 commit comments

Comments
 (0)