I would suggest that the best way to do this is to extend the jQuery selector. Something like this works well:
$.extend($.expr[':'],{
offsetLeft: function(a,i,m) {
if(!m[3]||!(/^(<|>|=)\d+$/).test(m[3])) {return false;}
var offsetLeft = $(a).offset().left;
return m[3].substr(0,1) === '>' ?
offsetLeft > parseInt(m[3].substr(1),10) :
m[3].substr(0,1) === '<' ? offsetLeft < parseInt(m[3].substr(1),10) :
offsetLeft == parseInt(m[3].substr(1),10);
}
});
This will allow you to select items using syntax like
$('span:offsetLeft(>10)')
or
$('.someClass:offsetLeft(<10)')
or even
$('.someClass:offsetLeft(=10)')
Real-time example: http://jsfiddle.net/X4CkC/
I must add that this intercepts jQuery selectors, which are usually pretty fast, but there is no doubt somewhere in the depths there is a loop. There is no way to avoid this.
source
share