jQuery ui:可拖动,如何确定图像在 x 轴上移动了多少?

发布于 12-15 17:41 字数 474 浏览 4 评论 0原文

我在 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 技术交流群。

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

发布评论

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

评论(2

拥抱没勇气2024-12-22 17:41:42

很简单,如果您想在停止拖动图像时保存图像位置。只需在停止时检查图像的左侧和顶部:

stop: function(event, ui) { 
    var topValue = $(this).position().top;
    var leftValue = $(this).position().left;
    //Save them to wherever you want
}

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:

stop: function(event, ui) { 
    var topValue = $(this).position().top;
    var leftValue = $(this).position().left;
    //Save them to wherever you want
}

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

墨小墨2024-12-22 17:41:42

我将存储开始时的开始位置并计算停止时的差异,如下所示:

http://jsfiddle.net/FMC56/ 1

$( "#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 的想法。

I would store the start position on start and compute the difference on stop like so:

http://jsfiddle.net/FMC56/1

$( "#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";
    }
});

EDIT: using data was mblase75's idea btw.

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