Jquery 可拖动和可调整大小
function makeResourceDrag(arrIndexID) {
$('#imgA' + arrIndexID).resizable();
$('#imgA' + arrIndexID).draggable({
start: function(event, ui) {
isDraggingMedia = true;
},
stop: function(event, ui) {
isDraggingMedia = false;
// Set new x and y
resourceData[arrIndexID][4] = Math.round($('#imgA' + arrIndexID).position().left / currentScale);
resourceData[arrIndexID][5] = Math.round($('#imgA' + arrIndexID).position().top / currentScale);
}
});
}
如果取出可调整大小的线,则效果很好,但我希望这些图像可拖动且可调整大小,如果我尝试使同一元素具有这两个属性,我会得到有趣的行为,有谁知道如何实现此工作?
谢谢!
function makeResourceDrag(arrIndexID) {
$('#imgA' + arrIndexID).resizable();
$('#imgA' + arrIndexID).draggable({
start: function(event, ui) {
isDraggingMedia = true;
},
stop: function(event, ui) {
isDraggingMedia = false;
// Set new x and y
resourceData[arrIndexID][4] = Math.round($('#imgA' + arrIndexID).position().left / currentScale);
resourceData[arrIndexID][5] = Math.round($('#imgA' + arrIndexID).position().top / currentScale);
}
});
}
This works fine if the resizeable line is taken out, but I want these images to be draggable and resizeable, I get funny behaviours if I try and make the same element have both attributes, does anyone know a way to make this work?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
看起来这是因为您在
上执行此操作,jqueryui 将其包装在
中,然后图像的可拖动组件发生在包装
。
尝试将
包装在
中(如果样式为
display:inline-block
,将“拥抱”尺寸x 轴和 y 轴上的图像),使可拖动(因此包含的
也将如此),并使
可调整大小(并且由于 div 拥抱图像,所以一切都很好)。
工作示例: http://jsfiddle.net/vrUgs/2/
Looks like it's because you're doing it on an
<img>
, which jqueryui wraps in a<div>
, and then the draggable component of the image happens within the wrapping<div>
.Try wrapping the
<img>
in a<div>
(which if styleddisplay:inline-block
, will "hug" the size of the image in both x and y axes), make the<div>
draggable (and therefore the enclosed<img>
will be as well), and make the<img>
resizable (and since the div hugs the image, it all sits nicely).Working example: http://jsfiddle.net/vrUgs/2/
可调整大小和可拖动给我带来了各种 DOM 移动问题。 针对此行为提交了一个错误;临时修复是应用以下 CSS,这对我有用:
Resizable and draggable were causing all kinds of DOM shifting problems for me. There's a bug filed for this behavior; the temporary fix is to apply the following CSS, which worked for me:
我很好奇,这是可拖动和调整大小的工作代码。
jQuery:
html:
我注意到的其他东西,接受或保留它,因为我不知道改变你的 JS 涉及的工作。
首先,所有被拖动的“draggables”都会获得“.ui-draggable-dragging”类,您可以将其用于“isDraggingMedia”逻辑。
其次,为了准确获取当前位置,我建议使用 ui.offset{top:"",left:""} ,可以使用描述位置的 ui.position{top:"",left:""} 进行更改相对于被拖动的项目的“helper”对象。
I was curious, here is working code that is draggable and resizable.
jQuery:
html:
other stuff i noticed, take or leave it, as I do not know the work involved in changing your JS.
first, all 'draggables' that are being dragged get a class of '.ui-draggable-dragging' which, you can use for your 'isDraggingMedia' logic potentially.
second, to get the current position accurately, I recommend using the ui.offset{top:"",left:""}, possible altered with the ui.position{top:"",left:""} describing the position of the 'helper' object relative to the item being dragged.
确保您的项目中引用了
jquery-ui.css
!根据这个问题: jquery 中可调整大小、可拖动的对象。可能吗? 如果你只包含 jquery-ui.css 它就会工作。当这些都没有解决时,它解决了我的问题。我的代码很简单:
这是可拖动的,但不可调整大小。其他的都没起作用。添加 CSS 可以调整大小,无需额外的 div 或代码。
Make sure
jquery-ui.css
is referenced in your project!Per this question: Resizable, draggable object in jquery. Possible? if you simply include jquery-ui.css it will work. It solved my problem when none of these did. My code is simple:
This was draggable but not resizable. Nothing else worked. Adding the CSS allowed resizing with no extra divs or code.
如果您首先应用可调整大小,则可以将可拖动应用到 img 的父级;这是通过应用调整大小创建的新 div。
If you apply the resizable first, you can then apply the draggable to the img's parent; which is the new div created by applying resizable.
负责此问题的代码是其他问题的解决方法: Github 链接,指向 Bug 1749。
该修复本身从一开始就存在: Jquery 的 Github Bug 修复链接< /a>
实际上仅在 7 年前更新过一次:更新的链接
参考: jquery 链接
The code responsible for this was a workaround for something else: Github link which point to Bug 1749.
The fix itself exists since the beginning of time: Github Bug Fixed link for Jquery
And was effectively just updated once, 7 years ago: Updated Link
Reference : jquery link