jQuery UI 可调整大小 - 操作方向

发布于 2024-07-27 21:47:09 字数 77 浏览 4 评论 0原文

如何确定resize操作的方向? 我尝试谷歌搜索并在 jquery ui 上找到了一张票,上面说它已修复。 但没有关于如何使用它的文档。

How can I determine the direction of resize operation? I tried googling that and found a ticket on jquery ui, that said it is fixed. But there is no doc on how to use it.

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

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

发布评论

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

评论(7

握住你手 2024-08-03 21:47:09

不确定您是否想将调整大小操作限制到特定轴,或者您是否想知道调整大小实际发生的方向。

如果您想前者,RaYell 的答案有效。 我将讨论后者。

如果您设置如下所示的可调整大小:

var r = $("#resizable");
r.resizable();

假设您想知道用户释放鼠标按钮后发生调整大小的方向。 让我们将一个函数绑定到调整大小停止事件:

r.bind('resizestop', function(event, ui) {
    // determine resize deltas
    var delta_x = ui.size.width - ui.originalSize.width;
    var delta_y = ui.size.height - ui.originalSize.height;
}

使用传递的 ui 对象,我们可以通过从旧大小中减去新大小来确定大小的相对变化。 之后,您可以根据需要使用 delta_x 和 delta_y 。 让我们构建一个与其中一个句柄相对应的字符串。

r.bind('resizestop', function(event, ui) {

    // determine resize deltas
    var delta_x = ui.size.width - ui.originalSize.width;
    var delta_y = ui.size.height - ui.originalSize.height;

    // build direction string
    var dir = '';

    if (delta_y > 0) { 
        dir += 's';
    } else if (delta_y < 0) { 
        dir += 'n';         
    }      

    if (delta_x > 0) { 
        dir += 'e';
    } else if (delta_x < 0) { 
        dir += 'w';
    }

    // do something with this string
    alert(dir);        
});

请注意,如果元素的两侧都有句柄,则这不一定会返回实际用于执行调整大小的句柄,而仅返回其净方向。

Not sure if you meant that you'd like to constrain the resize operation to a particular axis, or if you'd like to know what direction the resize actually occurred in.

RaYell's answer works if you wanted to former. I'll discuss the latter.

If you set up a resizable like this:

var r = $("#resizable");
r.resizable();

Let's say you'd like to know what direction the resize occurred in after the user releases the mouse button. Let's bind a function to the resize stop event:

r.bind('resizestop', function(event, ui) {
    // determine resize deltas
    var delta_x = ui.size.width - ui.originalSize.width;
    var delta_y = ui.size.height - ui.originalSize.height;
}

Using the passed ui object, we can determine the relative change in size by subtracting the new size from the old size. After that, you can use delta_x and delta_y however you wish. Let's build a string that corresponds to one of the handles.

r.bind('resizestop', function(event, ui) {

    // determine resize deltas
    var delta_x = ui.size.width - ui.originalSize.width;
    var delta_y = ui.size.height - ui.originalSize.height;

    // build direction string
    var dir = '';

    if (delta_y > 0) { 
        dir += 's';
    } else if (delta_y < 0) { 
        dir += 'n';         
    }      

    if (delta_x > 0) { 
        dir += 'e';
    } else if (delta_x < 0) { 
        dir += 'w';
    }

    // do something with this string
    alert(dir);        
});

Note that this will not necessarily return which handle was actually used to perform the resize if you have handles on both sides of the element, only it's net direction.

无远思近则忧 2024-08-03 21:47:09

在启动、调整大小和停止回调中:

$(this).data('resizable').axis

In the start, resize, and stop callbacks:

$(this).data('resizable').axis
黑色毁心梦 2024-08-03 21:47:09

我知道这是很久以前的问题,但给出的解决方案并不能解决我的问题。 我找到了一种方法来访问启动函数中的处理程序,并且它可以在 FF 和 chrome 中工作。 我的组织不支持 IE,因此未经测试,但在这种情况下,FF 方式应该可以工作,因为它是更标准的事件处理。

$(".resizable").resizable({
    start: function (event, ui) {
        var dir = event.toElement ? event.toElement : event.originalEvent.target;
    }
});

然后 dir 是实际的处理程序,您必须从中找到类和方向(从类列表中解析,如果我到达那里,我将发布我所做的事情)。

由于很多原因,在调整大小之前了解调整大小发生的方向是很有用的,而且我真的认为 jQuery 应该包含一种方法来知道哪个句柄已被抓住。

稍后编辑:
jQueryUI 为其 API 提供了 getter,在本例中是为了获取您刚刚执行的句柄列表

var handles = $( ".selector" ).resizable( "option", "handles" );

(直接来自马口)。 就我而言,我只是检查它是“n”还是“s”(向上还是向下),然后通过在开始函数中获取原始大小并使用像循环一样的调整大小函数来计算对象大小的变化继续计算它。 如果尺寸减小,则从南手柄开始,方向为向上;如果从北手柄开始,方向为向下;如果尺寸增大,则意味着方向从南手柄向下,或从北手柄向上。

为了确定是“n”还是“s”被拉动,我只是从上面抓取了 dir 并将返回的类名中除了最后一个字母之外的所有内容切片,因为该类类似于 ui-handle-s

这实际上相当幽默,因为我最终没有使用其中的大部分内容,因为我将其更改为仅从底部调整大小。 因为我没有使用它,所以我希望其他人发现这有帮助。

I know this is a question from a long time ago, but the given solutions don't solve my problem. I found a way to get to the handler in the start function and it's working in FF and chrome. My organization doesn't support IE so it's untested, but in this case the FF way should work since it's the more standard event handling.

$(".resizable").resizable({
    start: function (event, ui) {
        var dir = event.toElement ? event.toElement : event.originalEvent.target;
    }
});

dir is then the actual handler, you'll have to find the class and direction from that (parse from the class list, if I get there I'll post what I do).

It is useful to know the direction in which the resizing is happening before the resize for a lot of reasons, and I really think jQuery should have included a way to know which handle had been grabbed.

Later edit:
jQueryUI provides getters for their API, in this case to get the handle list you'd just do

var handles = $( ".selector" ).resizable( "option", "handles" );

(which is straight from the horses mouth). In my case, I just checked if it was 'n' or 's' (up or down) and then calculated the change of size in the object by grabbing the original size in the start function, and using the resize function like a loop to keep calculating it. If the size was decreasing the direction was up if from the south handle and down if from the north handle, and if it was increasing it meant the direction was down from the south handle or up from the north handle.

To get whether it was 'n' or 's' being pulled, I just grabbed dir from above and sliced everything but the last letter off of the class name returned, since the class is something like ui-handle-s.

It's actually rather humorous as I didn't end up using most of this, since I changed it to only resize from the bottom. Since I'm not using it I hope someone else finds this helpful.

娇柔作态 2024-08-03 21:47:09

我认为选项 handles 就是您所追求的:

$(selector).resizable({handles: 'n, s, e, w'})

其中字母代表地理方向:

  • n - 北(上)
  • s - 南(下)
  • e - 东(右)
  • w - 西(左)
  • ne -东北(左上)
  • se - 东南(左下)
  • NW - 西北(左上)
  • sw - 西南(左下)

使用您喜欢的上述任何子集。

您可以在此处找到该选项的文档

I think the option handles is what you are after:

$(selector).resizable({handles: 'n, s, e, w'})

Where letters represent geographic directions:

  • n - north (top)
  • s - south (bottom)
  • e - east (right)
  • w - west (left)
  • ne - north east (top left)
  • se - south east (bottom left)
  • nw - north west (top left)
  • sw - south west (bottom left)

Use any subset of the above that you like.

You can find the documentation for that option here

君勿笑 2024-08-03 21:47:09
$(".ui-resizable-handle").live("mousedown", function(e) {
    $.ui.curResizeDirection = this.className.split("-").pop();
    console.log($.ui.curResizeDirection);
});
$(".ui-resizable-handle").live("mousedown", function(e) {
    $.ui.curResizeDirection = this.className.split("-").pop();
    console.log($.ui.curResizeDirection);
});
尝蛊 2024-08-03 21:47:09

尝试以下代码:

$( "#resizable" ).resizable({
    handles: "n, e, s, w",
    resize: function( event, ui ) {
       //your codes here
    }
});

使用 handles 属性在四个方向上调整 div 的大小。

在这里查看:

http://www.landcoder .com/4-dynamic-interactive-code-snippets-jquery-705#ressized

Try this code:

$( "#resizable" ).resizable({
    handles: "n, e, s, w",
    resize: function( event, ui ) {
       //your codes here
    }
});

The div is resized in four directions by using the handles property.

Check it out here:

http://www.landcoder.com/4-dynamic-interactive-code-snippets-jquery-705#resizable

风柔一江水 2024-08-03 21:47:09

jsonx 有一个很好的解决方案,但您也可以利用内置事件:

$('#resizable').resizable({
    handles: 'n,s',
    start: function( event, ui ) {
        var handleDirection = event.srcElement.className.split("-").pop();
    }
});

该代码使“#ressized”元素可调整大小,并将句柄放在元素的北侧和南侧。 如果单击并拖动北手柄,则 handleDirection 为“n”。
我一直无法找到类似的方法来处理“停止”事件。

jsonx has got a good solution but you can also utilise the built-in events:

$('#resizable').resizable({
    handles: 'n,s',
    start: function( event, ui ) {
        var handleDirection = event.srcElement.className.split("-").pop();
    }
});

The code makes the '#resizable' element resizable and puts handles on the north and south sides of the element. If the north handle is clicked and dragged then handleDirection is 'n'.
I have been unable to find a similar way to do this with the 'stop' event.

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