jQuery ui:可拖动,如何确定图像在 x 轴上移动了多少?
我在 div 中有一个图像,如下所示:
<div><img src="" class="draggable"></div>
这是一个可通过 jQuery UI 拖动的图像。现在我想确定图像在 x 轴上移动了多少,因此我可以使用该信息来保存图像的位置。
IE如果图像向左移动100px,x应该是“-100px”,如果向右移动它应该是“100px”..
我可以使用这个,但它只能确定图像移动了多少,而不是在具体方向:
drag: function() {
counts[ 1 ]++;
updateCounterStatus( $drag_counter, counts[ 1 ] );
},
解决方案是什么?
谢谢, 雅各布
I have an image inside a div, like so:
<div><img src="" class="draggable"></div>
It's a draggable image, through jQuery UI. Now I want to determin how much the image is moved on the x-axis, so I can use that info to save the position of the image.
I.E. If the image is moved 100px left, x should be "-100px", and if it's moved to the right it should be "100px"..
I can use this, but it only determin how much the image was moved, not in a specific direction:
drag: function() {
counts[ 1 ]++;
updateCounterStatus( $drag_counter, counts[ 1 ] );
},
Whats the solution ?
Thanks,
Jakob
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
评论(2)
墨小墨2024-12-22 17:41:42
我将存储开始时的开始位置并计算停止时的差异,如下所示:
$( "#drag" ).draggable({
start: function(event, ui) {
$(this).data("startx",$(this).offset().left)
},
stop: function(event, ui) {
var change = $(this).offset().left - $(this).data("startx");
this.innerHTML = "change: " + change + "px";
}
});
编辑:使用数据是 mblase75 的想法。
~没有更多了~
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
很简单,如果您想在停止拖动图像时保存图像位置。只需在停止时检查图像的左侧和顶部:
jQuery UI 的可拖动功能并没有做任何魔法,如果您在 Firebug 或 Chrome 开发工具栏中检查它,您会发现它所做的只是更改元素的左侧和顶部属性。
有关 Draggable 具有的不同事件的更多信息: http://jqueryui.com/demos/draggable/#events
Easy, if you want to save the image position when you stop dragging the image. Simply just check left and top of the image on stop:
jQuery UI's draggable isn't doing anything magic, if you inspect it in Firebug or Chrome Dev Toolbar you will see that all it does is to alter the left and top property of the element.
More information about the different events that Draggable have: http://jqueryui.com/demos/draggable/#events