在 jQuery 中控制可拖动移动

发布于 2024-10-31 17:36:32 字数 207 浏览 0 评论 0原文

如何控制 jquery 中可拖动对象的移动?

我需要做一些类似于“捕捉到网格”的事情,其中​​可拖动移动每个“x”和“y”像素

我不能使用“grid:[x,y]”(2D),因为我需要进行拖动在等距网格(3D)上(我稍后将开发网格,我首先需要控制拖动)

例如:当我开始拖动时,可拖动不应该移动,当“x ”和“y”在某些条件下,

提前致谢!

how can I control de movement of a draggable in jquery?

I need to do something similar to the "snap to grid" where the draggable moves every "x" and "y" pixels

I can't use "grid: [x, y]" (2D) because I need to make the drag on an isometric grid (3D) (I will develop the grid later, I first need to controll the drag)

for example: when I start the dragging, the draggable shouldn't move, it has to snap to another position when the "x" and "y" are under certain conditions

thanks in advance!

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

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

发布评论

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

评论(2

妥活 2024-11-07 17:36:32

尝试使用 .draggable() 函数中固有的选项 http://jqueryui.com/demos/draggable/< /a>

基本上,它表示您需要将 snap 选项设置为 true,如下所示:

$( ".selector" ).draggable({ snap: true, snapMode:'outer', snapTolerance:40 });

snapMode "...确定可拖动元素将捕捉到的捕捉元素的哪些边缘。如果 snap 为 false,则忽略。可能的值:'inner'、'outer'、'both'。”
snapTolerance 是“...距应发生捕捉的捕捉元素边缘的距离(以像素为单位)”。

或者尝试使用网格选项。创建它是为了完全执行您想要的操作:
“...网格将拖动助手捕捉到网格,每个 x 和 y 像素。数组值:[x, y]
代码示例:

使用指定的网格选项初始化可拖动对象。

$( ".selector" ).draggable({ grid: [50, 20] });

在初始化后获取或设置网格选项。

//getter
var grid = $( ".selector" ).draggable( "option", "grid" );
//setter
$( ".selector" ).draggable( "option", "grid", [50, 20] );

希望这对您有帮助。

Try using the options inherent in the .draggable() function at http://jqueryui.com/demos/draggable/

basically it says you need to have the snap option set to true like this:

$( ".selector" ).draggable({ snap: true, snapMode:'outer', snapTolerance:40 });

snapMode "...determines which edges of snap elements the draggable will snap to. Ignored if snap is false. Possible values: 'inner', 'outer', 'both'."
And snapTolerance is "...The distance in pixels from the snap element edges at which snapping should occur."

Or try using the grid option. This is created to do exactly what you want:
"...Grid snaps the dragging helper to a grid, every x and y pixels. Array values: [x, y]
Code examples":

Initialize a draggable with the grid option specified.

$( ".selector" ).draggable({ grid: [50, 20] });

Get or set the grid option, after init.

//getter
var grid = $( ".selector" ).draggable( "option", "grid" );
//setter
$( ".selector" ).draggable( "option", "grid", [50, 20] );

Hopefully this helps you.

黑白记忆 2024-11-07 17:36:32

好的,我找到了我要找的东西!

在此链接中是我需要的代码片段,

代码是这样的:

/ /inside Drag: function(event, ui)

    var offset = $(this).parent().offset();
    var GRID_SPACING = 10;
    var SELECTION_OFFSET_X = 10;
    var SELECTION_OFFSET_Y = 3;            

    if (parseInt(event.clientX / GRID_SPACING) % 2 == 0)
    {
        var row = Math.floor((event.clientY - offset.top) / GRID_SPACING) + Math.floor(event.clientX / (2 * GRID_SPACING));
        var col = -Math.floor((event.clientY - offset.top) / GRID_SPACING) + Math.floor((event.clientX + GRID_SPACING) / (2 * GRID_SPACING));
    }
    else
    {
        var row = Math.floor((event.clientY + GRID_SPACING / 2 - offset.top) / GRID_SPACING) + Math.floor(event.clientX / (2 * GRID_SPACING));
        var col = -Math.floor((event.clientY + GRID_SPACING / 2 - offset.top) / GRID_SPACING) + Math.floor((event.clientX + GRID_SPACING) / (2 * GRID_SPACING));
    }

    var new_x = row * GRID_SPACING + col * GRID_SPACING - SELECTION_OFFSET_X;
    var new_y = (row * (GRID_SPACING / 2)) - (col * (GRID_SPACING / 2));                     

    if(event.clientX == new_x + GRID_SPACING * 2)
    {
        ui.position.left = new_x;
        new_x = event.clientX;
    }

    if(event.clientY == new_y + GRID_SPACING)
    {
        ui.position.top = new_y;
        new_y = event.clientY;
    }                    

    ui.position.left = new_x;
    ui.position.top = new_y;

我更改了一些常量以适应我的网格...

ok, I've found what I was looking for!

in this link was the piece of code that I needed

the code is this:

//inside drag: function(event, ui)

    var offset = $(this).parent().offset();
    var GRID_SPACING = 10;
    var SELECTION_OFFSET_X = 10;
    var SELECTION_OFFSET_Y = 3;            

    if (parseInt(event.clientX / GRID_SPACING) % 2 == 0)
    {
        var row = Math.floor((event.clientY - offset.top) / GRID_SPACING) + Math.floor(event.clientX / (2 * GRID_SPACING));
        var col = -Math.floor((event.clientY - offset.top) / GRID_SPACING) + Math.floor((event.clientX + GRID_SPACING) / (2 * GRID_SPACING));
    }
    else
    {
        var row = Math.floor((event.clientY + GRID_SPACING / 2 - offset.top) / GRID_SPACING) + Math.floor(event.clientX / (2 * GRID_SPACING));
        var col = -Math.floor((event.clientY + GRID_SPACING / 2 - offset.top) / GRID_SPACING) + Math.floor((event.clientX + GRID_SPACING) / (2 * GRID_SPACING));
    }

    var new_x = row * GRID_SPACING + col * GRID_SPACING - SELECTION_OFFSET_X;
    var new_y = (row * (GRID_SPACING / 2)) - (col * (GRID_SPACING / 2));                     

    if(event.clientX == new_x + GRID_SPACING * 2)
    {
        ui.position.left = new_x;
        new_x = event.clientX;
    }

    if(event.clientY == new_y + GRID_SPACING)
    {
        ui.position.top = new_y;
        new_y = event.clientY;
    }                    

    ui.position.left = new_x;
    ui.position.top = new_y;

I changed some of the constants to adjust to my grid...

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