无法在事件调用内使用jquery Dragstop和resize stop
我有一个应用程序,我通过在画布上拖动来创建 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
找出问题所在:我过于依赖
$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.