可信高度动画:仅在删除一行后进行动画处理

Zan*_*han 9 html javascript jquery animation contenteditable

这是我上一个问题的延续.

我设法通过添加一个条件来修复"动画暂停每个连续击键",该条件e.which === 13Enter按下键时检测并设置动画.


这是前一个工作方式:

在此输入图像描述

如您所见,在输入换行符然后继续击键时,动画滞后,这意味着动画会在每次击键时执行.


这些是经过修改的,仅在Enter按下后才会生成动画:

在此输入图像描述

它工作顺利(虽然在录制过程中有些滞后).


这些是删除每个字符时的工作方式(不长按):

在此输入图像描述

正如您所看到的,它没有动画效果,因为当您连续删除每个角色时,动画会暂停,就像第一次尝试一样.


所以我现在想要实现的是反向的,在删除换行后平滑动画.

这是一个实时代码:

var kAnimationSpeed = 250;
var kPadding = 10;

$('div[contenteditable]').on('blur keyup paste input', function(e) {
  var styleElement = $(this).prev();

  var editorHeight = $(this).height();
  var styleElementHeight = styleElement.height();

  if (editorHeight !== styleElementHeight - kPadding * 2 && e.which === 13 || e.which === 8) {
    styleElement.stop().animate({ height: editorHeight + kPadding * 2 }, kAnimationSpeed);
  }
});
Run Code Online (Sandbox Code Playgroud)
.autogrowWrapper {
  position: relative;
}

.autogrow {
  border: 1px solid rgb(0, 0, 0);
  height: 40px; /* line-height + 2 * padding */
}

div[contenteditable] {
  outline: none;
  line-height : 20px;
  position: absolute;
  top: 10px; /* padding */
  left: 10px; /* padding */
  right: 10px; /* padding */
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="autogrowWrapper">
  <div class="autogrow"></div>
  <div contenteditable="true"></div>
</div>
Run Code Online (Sandbox Code Playgroud)

如何在删除换行符时实现平滑动画,或者仅在删除换行符时如何设置动画?

Ale*_*lex 6

我不知道这是否适用于crossbrowser,但Chrome <div>为行添加了s,因此您可以实际计算它们:

工作小提琴

var kAnimationSpeed = 250;
var kPadding = 10;
var keyCodeDelete = 8;
var keyCodeReturn = 13;

$('div[contenteditable]').on('blur keyup paste', function(e) {
  var el = $(this);
  var styleElement = el.prev();
  var editorHeight = el.height();
  var styleElementHeight = styleElement.height();

  var divCount = parseInt(el.data('divcount') || 0 );
  var newDivCount = el.children('div').not(':contains("br")').length;
  var newLineDeleted = (divCount - 1 == newDivCount && e.which == keyCodeDelete);

  var heightCheck = (editorHeight !== styleElementHeight - kPadding * 2);
  var keyCodeCheck = (e.which === keyCodeReturn || e.which === keyCodeDelete);


  if (heightCheck && keyCodeCheck) {
    if (newLineDeleted) {
      $('body').append('<p>nl removed!</p>');
    }
    styleElement.stop().animate({ height: editorHeight + kPadding * 2 }, kAnimationSpeed);
  }

  el.data('divcount', newDivCount);
});
Run Code Online (Sandbox Code Playgroud)

所以你只需要调整你的动画:)


Zan*_*han 4

所以我根据Alex的回答设法解决了这个问题。

在他的回答中,只有在按下该Enter键时才起作用。但我的目标是使用Shift+ Enter。按Enter将在 div<div>内插入 new contenteditable,而Shift+Enter将插入一个 new<br>而不是<div>。所以我修改了 count 的代码<br>,将值存储到数据中,并比较当前行数是否小于新行数,并监听 key Backspace。然后我将 替换e.which === 8newLineDeleted变量以告知何时删除新行。并删除该input函数,以便它将返回精确的布尔值(因为它似乎不适用于input其中的函数。我还添加了剪切和粘贴侦听器。

var kAnimationSpeed = 250;
var kPadding = 10;

$('div[contenteditable]').on('blur keyup paste', function(e) {
  var styleElement = $(this).prev();
  var editorHeight = $(this).height();
  var styleElementHeight = styleElement.height();
  
  var lineCount = parseInt($(this).data('linecount') || 0);
  var newLineCount = $(this).children('br').length;
  $(this).data('linecount', newLineCount);
  var newLineDeleted = (lineCount - 1 === newLineCount && e.which === 8);
  
  var cutPasteListener = (e.ctrlKey && e.which === 86 || e.which === 88);

  if (editorHeight !== styleElementHeight - kPadding * 2 && e.which === 13 || newLineDeleted || cutPasteListener) {
    styleElement.stop().animate({ height: editorHeight + kPadding * 2 }, kAnimationSpeed);
  }
});
Run Code Online (Sandbox Code Playgroud)
.autogrowWrapper {
  position: relative;
}

.autogrow {
  border: 1px solid rgb(0, 0, 0);
  height: 40px; /* line-height + 2 * padding */
}

div[contenteditable] {
  outline: none;
  line-height : 20px;
  position: absolute;
  top: 10px; /* padding */
  left: 10px; /* padding */
  right: 10px; /* padding */
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="autogrowWrapper">
  <div class="autogrow"></div>
  <div contenteditable="true"></div>
</div>
Run Code Online (Sandbox Code Playgroud)

谢谢。