如何使用jQuery基于xy坐标而不是使用鼠标拖动来选择元素? [英] How to select elements based on xy coordinates, not using mouse drag, with jquery?

查看:100
本文介绍了如何使用jQuery基于xy坐标而不是使用鼠标拖动来选择元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何基于XY坐标而不是通过使用jquery拖动鼠标来选择元素?

How to select elements based on XY coordinates and not with mouse drag with jquery?

推荐答案

jQuery插件,如下所示,将选择(部分)位于> = x和> = y位置的所有元素.

The jQuery plugin, as shown below, will select all elements which are (partially) at position >= x and >= y.

(function($){
    $.fn.filterXY = function(x, y){
        return this.filter(function(){
            var $this = $(this),
                offset = $this.offset(),
                width = $this.width(),
                height = $this.height();
            return offset.left + width >= x
                && offset.top + height >= y;
        });
    }
})(jQuery);

示例:

$("*").filterXY(0,0);
$("div").filterXY(100, 0);


演示: http://jsfiddle.net/yx4sN/7/

Demo: http://jsfiddle.net/yx4sN/7/

(function($){
    $.fn.inRangeX = function(x1, x2){
        // Accepting selectors and DOM elements
        if (typeof x1 == "string" && +x1 != x1 || x1 instanceof Element) {
            x1 = $(x1);
        }
        if (typeof x2 == "string" && +x1 != x1 || x1 instanceof Element) {
            x2 = $(x2);
        }
        // Accepting jQuery objects
        if (x1 instanceof $) {
            x1 = x1.offset().left;
        }
        if (x2 instanceof $) {
            x2 = x2.offset().left;
        }
        x1 = +x1;
        x2 = +x2;

        // Make sure that x1 < x2
        if (x1 > x2) {
            var x = x1;
            x1 = x2;
            x2 = x;
        }
        return this.filter(function(){
            var $this = $(this),
                offset = $this.offset(),
                rightSide = $this.width() + offset.left;
            return offset.left >= x1
                  && rightSide <= x2;
        });
    }
})(jQuery);

这篇关于如何使用jQuery基于xy坐标而不是使用鼠标拖动来选择元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆