简体   繁体   English

允许 Gridstack 仅可拖动到项目的特定区域

[英]Allow Gridstack draggable only to a specific area of the item

I am using Gridstack js to create draggable/resizable widgets for a dashboard.我正在使用 Gridstack js 为仪表板创建可拖动/可调整大小的小部件。 The picture shows an example widget.图片显示了一个示例小部件。 I have kept the default draggable values as is and I can drag it by double clicking anywhere inside the widget.我保留了默认的可拖动值,我可以通过双击小部件内的任意位置来拖动它。 What I want to do is make the widget draggable only by clicking on the gray area.我想要做的是仅通过单击灰色区域使小部件可拖动。 Should the default draggable values be overridden for this?是否应该为此覆盖默认的可拖动值? If so how?如果是这样怎么办? I am fairly new to jquery/js, and couldn't find anything similar on the inte.net.我是 jquery/js 的新手,在 inte.net 上找不到任何类似的东西。

示例小部件

Assuming you're using gridstack's default of jQueryUI's draggable, you can re-set the draggable constructor with the cancel option on any elements you want to not be draggable. 假设您正在使用gridstack默认的jQueryUI的draggable,您可以在您想要不可拖动的任何元素上使用cancel选项重新设置可拖动构造函数。 In the code below, adding the not-draggable class any divs inside of the grid stack item will have its dragging suppressed. 在下面的代码中,将not-draggable类添加到网格堆栈项内的任何div将禁止拖动。

$('.grid-stack-item').draggable({cancel: "div.not-draggable" });

The current answer is out of date.当前答案已过时。 The way to do this in 2023 is with the draggable option when you init GridStack .在 2023 年执行此操作的方法是在初始化GridStack时使用draggable选项。

GridStack.init({
        draggable: {
            handle: '.drag-target'
        },
    });

drag-target should be the class name of where you want your window to be draggable from. drag-target应该是您希望 window 可从中拖动的位置的 class 名称。

Slightly different from your image, but if you have something like the following, you will be able to click and drag the gray section, but the pink won't be draggable.与您的图像略有不同,但如果您有类似以下内容,您将能够单击并拖动灰色部分,但粉红色部分不可拖动。

 .outer { border: solid black 2px; }.header { background: grey; height: 20px; }.content { background: pink; height: 80px; }
 <div class="outer"> <div class="header drag-target"> </div> <div class="content"></div> </div>

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

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