简体   繁体   English

如何在jQuery中选择所有与可拖动div重叠的div?

[英]How to select all divs that overlay with a draggable div in jquery?

I'm building a graph displaying events happening over time. 我正在建立一个图表,显示随着时间推移发生的事件。 Each of my event is an horizontal bar that is created using a <div> as shown below: 我的每个事件都是使用<div>创建的单杠,如下所示:

<div class="aaaa" style="margin-left: 0.00%; width:100.00%;">aaaa</div>
<div class="aaaa" style="margin-left: 5.00%; width: 20.00%;">bbbb</div>
<div class="aaaa" style="margin-left:25.00%; width: 50.00%;">cccc</div>
<div class="aaaa" style="margin-left:55.00%; width: 45.00%;">dddd</div>

At the top of this graph, I have a "ruler" which is a draggable and resizable <div> 在此图的顶部,我有一个“ ruler”,它是可拖动且可调整大小的<div>

<div id="draggable2" class="draggable ui-widget-content">
    <p>Ruler</p>
</div>

My intention is to drag this ruler horizontally and to use it to select all bars of my graphs that are below the ruler. 我的意图是水平拖动此标尺,并使用它来选择图形中标尺下方的所有条形。 When bars are selected, the background color should change. 选择条形时,背景色应改变。

So far I manage to drag and to resize my ruler as I want with the following code: 到目前为止,我可以使用以下代码根据需要拖动并调整标尺的大小:

$(function () {
    $("#draggable2").draggable({
        axis: "x"
    });
});

$(function () {
    $("#draggable2").resizable({
        helper: "ui-resizable-helper",
        handles: "se, sw, e, w"
    });
});

I also manage to select the bars I want using the following code: 我还使用以下代码设法选择了我想要的酒吧:

$(".fake").selectable({
    filter: "div"
});

You can see on the fiddle referenced below that when you do a "lasso selection", my bars become pink. 您可以在下面引用的小提琴中看到,当您执行“套索选择”时,我的条变为粉红色。

However, I would like to combine my ruler with this "lasso selection", or in other words, I would like that once my ruler is dragged, all <div> that are below become selected. 但是,我想将标尺与此“套索选择”结合使用,换句话说,我希望一旦拖动标尺,下面的所有<div>将被选中。

I've created a fiddle to illustrate my problem: http://jsfiddle.net/Lge93/ 我创建了一个小提琴来说明我的问题: http : //jsfiddle.net/Lge93/

As I'm new to Javascript and Jquery, I would really appreciate any solution or suggestion. 由于我是Javascript和Jquery的新手,因此,我非常感谢任何解决方案或建议。

Thanks for your time 谢谢你的时间

Edit post answers: My final solution based on the code provided by Jtromans is available here: http://jsfiddle.net/Lge93/5/ 编辑帖子答案:基于Jtromans提供的代码的最终解决方案可在以下位置找到: http : //jsfiddle.net/Lge93/5/

Here's my solution: 这是我的解决方案:

$(function() {
  $( "#draggable2" ).draggable({ 
    axis: "x",
    drag: function(e) {
        var selectableElements = $(".fake").data().uiSelectable.selectees;
        var ruler = $(e.target);

        $.each(selectableElements, function(i, v) {
            if(ruler.offset().left >= $(v).offset().left) {
                $(v).addClass("ui-selected");
              }else {
                 $(v).hasClass("ui-selected") && $(v).removeClass("ui-selected");                    
            }
           });
        }

    });
});

$(function() {
  $( "#draggable2" ).resizable({
    helper: "ui-resizable-helper",
    handles: "se, sw, e, w"
  });
});

$(".fake").selectable({filter:"div"});

http://jsfiddle.net/Lge93/3/ http://jsfiddle.net/Lge93/3/

If I understand your question correctly, it sounds like you could solve the problem in the following way. 如果我正确理解了您的问题,听起来您可以通过以下方式解决问题。

Upon interacting with the ruler, you will be able to calculate the new width, x- and y-coordinates. 与标尺进行交互后,您将能够计算新的宽度,x和y坐标。 This is pretty straight forward and requires only a few lines of jQuery code in the callback after any form of interaction with your ruler. 这很简单,在与标尺进行任何形式的交互之后,在回调中仅需要几行jQuery代码。

You could then check for each item in your (gant?) chart to see whether it occupies any of the same area that the ruler does. 然后,您可以检查(gant?)图表中的每个项目,以查看它是否与标尺占据相同的区域。 This would be relative to the parent container. 这将相对于父容器。

The devil is in the detail for how you would like to handle whether the ruler is 'in' the Gant chart div item. 细节在于您如何处理标尺是否在Gant图表div项中的细节。 And below is some code to get your started on working out whether the Ruler occupies the some of the same location as the other divs: 下面是一些代码,可让您开始了解标尺是否与其他div处于相同的位置:

function checkOverlays (x, width, y, height) {
    $(".aaaa").each (function (i,e) {
        var thisWidth = $(this).width();
        var thisHeight = $(this).height();
        var offsetTop = $(this).offset().top - $(this).parent().offset().top;
        var offsetLeft = $(this).offset().left - $(this).parent().offset().left;
        console.log('x coverage: ' + offsetLeft + " to " + parseFloat(thisWidth + offsetLeft));
        console.log('y coverage: ' + offsetTop + " to " + parseFloat(thisHeight + offsetTop));

        if (offsetLeft > x && offsetLeft < parseFloat(x + width) ) {
            console.log("I'm in the same x space");
            $(this).css('background-color', 'red');
        }

        if (offsetTop > y && offsetTop < parseFloat(y + height) ) {
            console.log("I'm in the same y space");
            $(this).css('background-color', 'red');
        }
    })
}



$("#draggable2").on("resize drag", function () {
    var thisWidth = $(this).width();
    var thisHeight = $(this).height();
    var offsetTop = $(this).offset().top - $(this).parent().offset().top;
    var offsetLeft = $(this).offset().left - $(this).parent().offset().left;
    console.log('x coverage: ' + offsetLeft + " to " + parseFloat(thisWidth + offsetLeft));
    console.log('y coverage: ' + offsetTop + " to " + parseFloat(thisHeight + offsetTop));

    checkOverlays (offsetLeft, thisWidth, offsetTop, thisHeight)
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM