gilmoreorless / jquery-nearest

Find the elements in a page that are closest to a particular point or element, based on pixel dimensions
http://gilmoreorless.github.io/jquery-nearest/
MIT License
98 stars 23 forks source link

Is there a way to only get the nearest element which is only above the origin point? #20

Closed chanpory closed 9 years ago

chanpory commented 10 years ago

In other words, how do I ignore everything below the origin point? For example, if my mouse's Y position is 400px, I'd like to get the nearest element that is above it.

gilmoreorless commented 10 years ago

At the moment there's no built-in way to do that.

As a workaround, perhaps you could pre-process the elements above your example 400px mark to add a special class to them, then use that class as the selector to the call to nearest. That's just a suggestion off the top of my head, but obviously I don't know if it will work for your use case.

chanpory commented 10 years ago

Thanks, I'll try to figure something out. As a feature request, it'd be great to specify a region relative to the origin point for $.nearest to look in when getting elements. Something like:

var closestElements = $.nearest({x: e.pageX, y: e.pageY, constrainTo:"above" }, $('div'));
jaysalvat commented 10 years ago

Hello. Thanks a lot for this plugin. :)

Same thing here, it would be perfect to set a direction to get the nearest object. "above", "below", "right", "left".

For example I need to achieve a keyboard navigation. Pressing Right arrow goes the nearest element to the right of the current element. Pressing Up arrow goes to the nearest element above the current element, etc...

gilmoreorless commented 9 years ago

Thanks to @dogoku this is now available with the directionConstraints option in 1.4.0.