且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

JQuery UI中的自定义可调整大小的句柄

更新时间:2023-02-05 21:49:30

从一点挖掘我做了jquery-ui代码,我想我可以确认可调整大小的元素外部的自定义句柄不起作用。

问题在于鼠标事件是针对可调整大小的元素(或textarea情况下的包装元素)初始化的。如果句柄不是处理鼠标事件的地方,它不会触发句柄上的mousedown事件,因此不会拖动它。



作为概念验证我在jquery-ui中进行了一些修补,直到我得到了某种工作。没有关于代码的保证,但它应该指出需要发生什么样的更改才能使其工作。



在ui.resizable.js中的第140行左右,我更改了:

pre $ this._handles = $('。ui-resizable-handle',this.element)
.disableSelection ();

  this._handles = $('。ui-resizable-handle')
.disableSelection();

然后,我添加了下面的代码(从ui.core.js的mouseInit的一部分,可能应该使用整个函数)在ui.resizable.js(大约180行)之后调用mouseInit来调整大小的元素:


//初始化鼠标交互
this._mouseInit();

//为手柄添加鼠标事件 - ylebre
(this.handles中的i){
$(this.handles [i])。bind(' mousedown.resizable',函数(event){
return self._mouseDown(event);
});


$


这允许我创建一个不是可调整大小的子元素的调整大小的句柄元件。我的句柄是id为'south'的div,并附加到可调整大小的元素。这里是HTML snippit:

 < textarea id =resizableclass =ui-widget-content> 
< h3 class =ui-widget-header>可调整大小< / h3>
< / textarea>
< div id =southclass =ui-resizable-handle> south< / div>

以及javascript代码:

{handles:{s:document.getElementById(south)}}
); $ {code> $(#resize

希望这有助于您!


I've been attempting to create a resizable textbox (ASP.NET multiline TextBox / HTML textarea) and use JQuery UI to make it resizable, but seem to be running into a few issues involving custom drag handles.

The JQuery documentation on the Resizable method (specifically that on the handles option) suggests that I can set any of the handles to use a custom HTML element. I have managed to get the resizable textbox working perfectly well using the default resizable handles, but trying to set custom ones (i.e. an HTML element that I have defined in markup) creates the following issue: the resizable container leaves the correct amount of space for the div handle at the bottom (for the South handle), yet leaves the space empty and shows the element below (outside of) the resizable container (verified using Firebug).

Here is my markup (ASP.NET/XHTML) and JavaScript code:

<asp:TextBox runat="server" ID="codeTextBox" TextMode="MultiLine" Wrap="false" Rows="15">
</asp:TextBox>
<div class="resizable-s" style="width: 100%; height: 22px; background: red;">
</div>

<script type="text/javascript">

    $(function() {
        var codeTextBox = $("#<%= codeTextBox.ClientID %>");

        codeTextBox.resizable({
            handles: { s : $(".resizable-s") },
            minHeight: 80,
            maxHeight: 400
        });
    });

</script>

Of course, I can't make the resizable handle div (class resizable-s) a child of the TextBox/textarea, but this should not be a problem according to the JQuery docs.

Judging by the searching I've done, I'm not the only person who's had this problem. (Unfortunately, the JQuery docs fail to give an example of using custom resizable handles, so we're all left unsure of precisely the right code.) If anyone could suggest a fix to this, or indeed confirmation that it is a JQuery bug, that would be very much appreciated.

From a bit of digging I did in the jquery-ui code, I think I can confirm that a custom handle outside the resizable element doesn't work.

The problem is that the mouse events are initialized for the resizable element (or a wrapper element in the case of textarea). If the handle is not a child of where the mouse events are handled, it won't trigger the mousedown event on the handle, thus not making it draggable.

As a proof of concept I patched around a bit in jquery-ui until I got things to (sort of) work. No guarantees about the code, but it should point out what kind of changes would need to happen to make it work.

Around line 140 in ui.resizable.js I changed:

this._handles = $('.ui-resizable-handle', this.element)
    .disableSelection();

to

this._handles = $('.ui-resizable-handle')
    .disableSelection();

Then I added the following code (part of mouseInit from ui.core.js, probably should use the whole function) in ui.resizable.js (around line 180) after the mouseInit is called for the resizable element:

   ...
   //Initialize the mouse interaction
   this._mouseInit();

   // Add mouse events for the handles as well - ylebre
   for (i in this.handles) {
      $(this.handles[i]).bind('mousedown.resizable', function(event) {
        return self._mouseDown(event);
      });
   }

This allows me to make a resize handle that is not a child of the resizable element. My handle is a div with id 'south' and is attached to the resizable element. Here is the HTML snippit:

<textarea id="resizable" class="ui-widget-content">
    <h3 class="ui-widget-header">Resizable</h3>
</textarea>
<div id="south" class="ui-resizable-handle">south</div>

And the javascript code:

    $("#resizable").resizable(
        {handles: {s: document.getElementById("south")}}
    );

Hope this helps!