无法在事件调用内使用jquery Dragstop和resize stop

发布于 2024-10-02 01:48:27 字数 878 浏览 1 评论 0原文

我有一个应用程序,我通过在画布上拖动来创建 div。我需要这些 div 可拖动且可调整大小。这是我的函数:

function createDiv( $startX, $startY, $stopX, $stopY ) {
    if ($startX==$stopX || $startY==$stopY)
        return;

    var zona = $('<div class="zona"></div>');
    $(zona).css({position: "absolute", background: "#f81", overflow: "hidden", top: Math.min($startY,$stopY)+"px", left: Math.min($startX, $stopX)+"px",width: Math.abs($stopX-$startX)+"px", height: Math.abs($stopY-$startY)+"px" });
    $("#demo").append(zona);

    $(zona).draggable({
        stop: function(){
            alert("should store dimensions in a form here");
        }
    }).resizable({
        stop: function(){
            alert("should store dimensions in a form here");
        }
    });

    $nr_zone++;
}

问题是,虽然创建的 div 可拖动且可调整大小,但停止函数中的包含代码将不起作用(即警报不起作用)。

如果有人能发现这个问题,我将非常感激。谢谢。

i have an application where i'm creating divs by dragging across a canvas. I need these divs to be draggable and resizable. Here's my function:

function createDiv( $startX, $startY, $stopX, $stopY ) {
    if ($startX==$stopX || $startY==$stopY)
        return;

    var zona = $('<div class="zona"></div>');
    $(zona).css({position: "absolute", background: "#f81", overflow: "hidden", top: Math.min($startY,$stopY)+"px", left: Math.min($startX, $stopX)+"px",width: Math.abs($stopX-$startX)+"px", height: Math.abs($stopY-$startY)+"px" });
    $("#demo").append(zona);

    $(zona).draggable({
        stop: function(){
            alert("should store dimensions in a form here");
        }
    }).resizable({
        stop: function(){
            alert("should store dimensions in a form here");
        }
    });

    $nr_zone++;
}

The problem is that, while the created div will be draggable and resizable, the containing code in the stop functions won't work (i.e. the alerts won't work).

If anyone can spot the problem I'll be very grateful. Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

久伴你 2024-10-09 01:48:27

找出问题所在:我过于依赖 $nr_zone ,它在任何事件触发之前都会增加;我的解决方案是将 $nr_zone 的值存储在元素的 id 中(例如 $(zona).attr("id","zona_"+$nr_zone) 和将其在代码中的使用替换为 $(zona).attr("id").split("_")[1] 如果有更优雅的解决方案,我很想听听。

Found out what the problem was: i was depending too much on $nr_zone which gets incremented before any of the events get fired; My solution is to store the value of $nr_zone in the element's id (sth like $(zona).attr("id","zona_"+$nr_zone)and replace its use in code with $(zona).attr("id").split("_")[1]. If there's a moer elegant solution i'd love to hear it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文