调整文本区域的事件大小?

IT技术 javascript jquery html events
2021-02-11 11:15:54

当前版本的 Firefox 和 Chrome 包含一个拖动处理程序来调整<textarea>框的大小我需要捕获调整大小事件,我认为使用 jQueryresize()事件会很容易,但它不起作用!

我也尝试过正常onResize事件,但结果是一样的。你可以在 JSFiddle 上试试

有没有办法捕获它?

6个回答

Chrome 不会捕获 resize 事件并且 Chrome 不会捕获 mousedown,因此您需要设置 init 状态,然后通过 mouseup 处理更改:

jQuery(document).ready(function(){
   var $textareas = jQuery('textarea');

   // store init (default) state   
   $textareas.data('x', $textareas.outerWidth());
   $textareas.data('y', $textareas.outerHeight()); 

   $textareas.mouseup(function(){

      var $this = jQuery(this);

      if (  $this.outerWidth()  != $this.data('x') 
         || $this.outerHeight() != $this.data('y') )
      {
          // Resize Action Here
          alert( $this.outerWidth()  + ' - ' + $this.data('x') + '\n' 
               + $this.outerHeight() + ' - ' + $this.data('y')
               );
      }
  
      // store new height/width
      $this.data('x', $this.outerWidth());
      $this.data('y', $this.outerHeight()); 
   });

});

HTML

<textarea></textarea>
<textarea></textarea>

您可以在JSFiddle上进行测试

笔记:

  1. 您可以像 Hussein 那样附加自己的可调整大小,但如果您想要原始的,则可以使用上面的代码
  2. 正如 Bryan Downing 所提到的,当您将鼠标放在 textarea 顶部时,这会起作用;但是,在某些情况下可能不会发生这种情况,例如浏览器未最大化并且您继续拖动超出浏览器的范围或用于resize:vertical锁定移动。

对于更高级的东西,您需要添加其他侦听器,可能是队列和间隔扫描器;或者使用 mousemove,正如我相信 jQuery resizable 所做的那样 - 那么问题就变成了你对性能与抛光的重视程度如何?


更新:此后浏览器的 UI 发生了变化。现在双击角落可以将文本框收缩到其默认大小。因此,您可能还需要在此事件之前/之后捕获更改。

@BryanDowning 我忘了给你的评论投票,因为这是一个特别值得注意的用例,我也会在答案中提到它
2021-03-17 11:15:54
在我的快速测试中,这似乎只有在鼠标位于textarea.
2021-03-21 11:15:54
是的,只是认为它值得一提:) 这个问题在使用时最普遍resize: vertical,它只允许textarea高度增长。鼠标很容易textarea在右侧结束。
2021-04-07 11:15:54
它仍然不完美,但如果您将“mouseleave”与“mouseup”一起使用,效果会更好。我实际上最终使用了“mousemove”并限制了事件。
2021-04-07 11:15:54
@BryanDowning 是正确的。大多数情况下,用户不会在释放之前开始更改文本区域的大小并拖动到窗口/文档的范围之外;但这并不是说它不能/不会发生。对于更高级的东西,你需要其他监听器,可能还有队列和间隔扫描器;或使用 mousemove,因为我相信 jQuery resizable 确实如此
2021-04-11 11:15:54

处理元素大小调整的标准方法是 Resize Observer api,可在所有现代 Web 浏览器版本中使用。

function outputsize() {
 width.value = textbox.offsetWidth
 height.value = textbox.offsetHeight
}
outputsize()

new ResizeObserver(outputsize).observe(textbox)
Width: <output id="width">0</output><br>
Height: <output id="height">0</output><br>
<textarea id="textbox">Resize me.</textarea>

如果需要处理旧版本的 Chrome 和 Firefox(其他未测试),可以使用 Mutation Observer 来检测样式属性的变化。

function outputsize() {
 width.value = textbox.offsetWidth
 height.value = textbox.offsetHeight
}
outputsize()

new MutationObserver(outputsize).observe(textbox, {
 attributes: true, attributeFilter: [ "style" ]
})
Width: <output id="width">0</output><br>
Height: <output id="height">0</output><br>
<textarea id="textbox">Resize me.</textarea>

调整观察者大小

文档:https : //developer.mozilla.org/en-US/docs/Web/API/Resize_Observer_API

规格:https : //wicg.github.io/ResizeObserver

当前支持:http : //caniuse.com/#feat=resizeobserver

Polyfills:https : //github.com/pelotoncycle/resize-observer https://github.com/que-etc/resize-observer-polyfill https://github.com/juggle/resize-observer

这个解决方案实际上应该固定在顶部。简而言之,精湛的方法。我讨厌其他答案中提到的其他 hacky 方式,我绝对认为使用 jQuery UI 是不必要的开销。
2021-03-18 11:15:54
今天,我认为这将是最好的解决方案,因为大多数浏览器都支持它。MutationObserver 就是这么强大的工具!这个答案是如何使用它的一个很好的例子。
2021-03-20 11:15:54
Resize Observer 是可行的方法,因为它适用于 Chrome、FF 和 Safari。Mutation Observer 在 Safari 中不起作用。
2021-03-24 11:15:54
自 3以来,Safari 已支持 @RaineRevere Resize 观察者:caniuse.com/?search=resize%20observer
2021-04-04 11:15:54
MutationObserver 似乎有更多的浏览器支持,包括 Safari:caniuse.com/mutationobserver
2021-04-10 11:15:54

我稍微混合了 vol7ron 的回答,并用普通“resize”事件的简单触发器替换了“Resize Action Here”,这样您就可以“像往常一样”将您想要发生的任何事情附加到 resize 事件:

$(document).ready(function(){
    $('textarea').bind('mouseup mousemove',function(){
        if(this.oldwidth  === null){this.oldwidth  = this.style.width;}
        if(this.oldheight === null){this.oldheight = this.style.height;}
        if(this.style.width != this.oldwidth || this.style.height != this.oldheight){
            $(this).resize();
            this.oldwidth  = this.style.width;
            this.oldheight = this.style.height;
        }
    });
});

我添加了 mousemove 事件,因此在调整大小时拖动鼠标时调整大小也会触发,但请记住,当您四处移动鼠标时,它会经常触发。

在这种情况下,您可能希望在实际触发或处理调整大小事件时稍微延迟,例如替换上述内容:

$(this).resize();

和:

if(this.resize_timeout){clearTimeout(this.resize_timeout);}
this.resize_timeout = setTimeout(function(){$(this).resize();},100);

示例用法,使第二个 textarea 与第一个一起增长和缩小:

$('textarea').eq(0).resize(function(){
    var $ta2 = $('textarea').eq(1);
    $('textarea').eq(1).css('width',$ta2.css('width')).css('height',$ta2.css('height'));
});

另一种方法是绑定到 textarea 上的 mouseup 事件。然后你可以检查大小是否改变。

文本区域不存在调整大小事件。

可调整大小的 jQueryPlugin 看起来不是原生的,所以我们必须使用替代。

模拟它的一种方法是使用 mousedown/click 事件。如果你想要实时事件触发,你可以这样做:

2013 年 11 月 11 日更新:

// This fiddle shows how to simulate a resize event on a
// textarea
// Tested with Firefox 16-25 Linux / Windows
// Chrome 24-30 Linux / Windows

var textareaResize = function(source, dest) {
    var resizeInt = null;

    // the handler function
    var resizeEvent = function() {
        dest.outerWidth( source.outerWidth() );
        dest.outerHeight(source.outerHeight());
    };

    // This provides a "real-time" (actually 15 fps)
    // event, while resizing.
    // Unfortunately, mousedown is not fired on Chrome when
    // clicking on the resize area, so the real-time effect
    // does not work under Chrome.
    source.on("mousedown", function(e) {
        resizeInt = setInterval(resizeEvent, 1000/15);
    });

    // The mouseup event stops the interval,
    // then call the resize event one last time.
    // We listen for the whole window because in some cases,
    // the mouse pointer may be on the outside of the textarea.
    $(window).on("mouseup", function(e) {
        if (resizeInt !== null) {
            clearInterval(resizeInt);
        }
        resizeEvent();
    });
};

textareaResize($("#input"), $("#output"));

演示:http : //jsfiddle.net/gbouthenot/D2bZd/

出于某种原因,您的 jsfiddle 示例不会调整右侧和左侧的文本区域的大小。
2021-03-24 11:15:54
我刚刚测试了它。你使用的是什么浏览器 ?我在一个网站上使用它,我没有收到任何投诉。
2021-03-24 11:15:54
我编辑了我的答案以支持 Chrome 最新的“错误”。我在 Linux/Windows 7 下用 Firefox/Chrome 进行了测试。
2021-04-08 11:15:54
我在 Chrome 上最新,并且在调整大小时没有 mousedown 事件......只有一个 mouseup 火。太奇怪了。
2021-04-11 11:15:54