我有几个绝对定位元素。是否有可能使所有元素的位置都在当前元素的右侧?
如果是,该怎么做?
答案 0 :(得分:2)
我刚刚编写了一个实现此功能的插件。 小提琴:http://jsfiddle.net/FJ5Cp/1/
功能逻辑应该类似于:
用法和功能:
// 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);
答案 1 :(得分:0)
使用.filter()
,将每个元素的.offset().left
与当前元素的元素进行比较:
var l = currentElement.offset().left;
var righterElements = positionedElements.filter(function(){
return $(this).offset().left > l;
});