I have several absolute positioned elements. Is it possible to get all elements with a position that's to the right of the current element?
If yes, how to do this?
I have several absolute positioned elements. Is it possible to get all elements with a position that's to the right of the current element?
If yes, how to do this?
I've just written a plugin which implements this feature. Fiddle: http://jsfiddle.net/FJ5Cp/1/
The function logic should bbe similar to:
Usage and function:
// Basic usage
var allRightElements = $("element").rightOfCurrent();
// Only select ___ elements which are located right of the current element
var allRightElementsFilter = $("element").rightOfCurrent("___");
// Only select ___ elements, which are located right of the current element,
// which are childs of ####
var allRightFilterInsideSomething = $("element").rightOfCurrent("___", "###");
(function($){
$.fn.rightOfCurrent = function(selector, context){
elem = this.eq(0);
selector = selector || '*';
context = context || null;
var currentRight = elem.offset().left + elem.width();
return $(selector, context).filter(function(){
var $this = $(this);
if ($this.css('position') == 'absolute'){
return $this.offset().left + $this.width() > currentRight;
}
return false;
});
}
})(jQuery);
Use .filter()
, comparing each element's .offset().left
with that of the current element:
var l = currentElement.offset().left;
var righterElements = positionedElements.filter(function(){
return $(this).offset().left > l;
});