代码之家  ›  专栏  ›  技术社区  ›  psychotik

查找最靠近位置的HTML元素(相对或绝对)

  •  22
  • psychotik  · 技术社区  · 16 年前

    给定绝对或相对位置(顶部和左侧),是否有任何方法可以使最接近这些坐标的HTML元素?

    或者,是否有任何方法来构造一个选择器(或者使用一些jquery构造)来枚举元素,然后找到哪个接近于所提供的坐标?假设元素集是小的和有限的。

    4 回复  |  直到 9 年前
        1
  •  20
  •   Raffi    9 年前

    我构建了一个jquery方法,它在集合中返回最接近偏移的元素:

    jQuery.fn.closestToOffset = function(offset) {
        var el = null,
            elOffset,
            x = offset.left,
            y = offset.top,
            distance,
            dx,
            dy,
            minDistance;
        this.each(function() {
            var $t = $(this);
            elOffset = $t.offset();
            right = elOffset.left + $t.width();
            bottom = elOffset.top + $t.height();
    
            if (
                x >= elOffset.left &&
                x <= right &&
                y >= elOffset.top &&
                y <= bottom
            ) {
                el = $t;
                return false;
            }
    
            var offsets = [
                [elOffset.left, elOffset.top],
                [right, elOffset.top],
                [elOffset.left, bottom],
                [right, bottom],
            ];
            for (var off in offsets) {
                dx = offsets[off][0] - x;
                dy = offsets[off][1] - y;
                distance = Math.sqrt(dx * dx + dy * dy);
                if (minDistance === undefined || distance < minDistance) {
                    minDistance = distance;
                    el = $t;
                }
            }
        });
        return el;
    };
    

    笔记:

    1. 如果偏移量为 里面 其中一个元素,它将被返回。

    这样使用:

    $('div.myCollection').closestToOffset({left: 5, top: 5});
    
        2
  •  1
  •   chustar    16 年前

    我能想到的最好的方法是有一个搜索函数循环,循环遍历所有现有的元素并比较坐标,始终保留最近变量的副本。

    我可以这样想,如果我在你的约束下,我会怎么做。

        3
  •  1
  •   Felix    16 年前

    循环遍历所有元素非常简单:

    function getClosestElement(x, y) {
        var elements, el, offset, dist, i, minDist, closestEl;
    
        elements = $("body *");
        closestEl = elements.eq(i);
        offset = closestEl.offset();
        offset.left += el.outerWidth()/2; // center of object
        offset.top += el.outerHeight()/2; // middle of object
        minDist = Math.sqrt((offset.left - x)*(offset.left - x) + (offset.top - y)*(offset.top - y));
    
        for (var i=0; i < elements.length; i++) {
            el = elements.eq(i);
            offset = el.offset();
            offset.left += el.outerWidth()/2; // center of object
            offset.top += el.outerHeight()/2; // middle of object
            dist = Math.sqrt((offset.left - x)*(offset.left - x) + (offset.top - y)*(offset.top - y));
            if (dist < minDist) {
                minDist = dist;
                closestEl = el;
            }
        }
    
        return closestEl;
    
        4
  •  -1
  •   lmeurs    12 年前

    或者尝试原来的jquery函数 offsetparent() . 下一个例子是:

    $( "li.item-a" ).offsetParent().css( "background-color", "red" );