Contenteditable height animation:仅在删除一行后进行动画处理
Contenteditable height animation: animate only after a line is deleted
这是我之前 的延续。
我设法通过添加条件 e.which === 13
修复了 "animation pauses every continuous keystrokes",该条件会在按下 Enter 键时检测并设置动画。
这是上一个的工作原理:
如您所见,在输入换行符和连续击键后,动画会滞后,这意味着动画会在每次击键时执行。
这些是修改后的,只有在按下 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);
}
});
.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 */
}
<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>
如何在删除换行时实现流畅的动画,或者如何仅在删除换行时制作动画?
我不知道这是否适用于跨浏览器,但 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);
});
所以你只需要调整你的动画:)
所以我根据Alex的回答设法解决了问题。
在他的回答中,它只在按下 Enter 键时有效。但我的目标是使用 Shift + Enter。按 Enter 将在 contenteditable
div 中插入新的 <div>
,而 Shift + Enter 插入一个新的 <br>
而不是 <div>
。所以我修改了代码,会计数<br>
,将值存储到数据中,并比较当前行数是否小于新行数,并监听Backspace 键。然后,我将 e.which === 8
替换为 newLineDeleted
变量,以告知何时删除新行。并删除 input
函数,这样它将 return 精确的布尔值(因为它似乎不适用于其中的 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);
}
});
.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 */
}
<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>
谢谢。
这是我之前
我设法通过添加条件 e.which === 13
修复了 "animation pauses every continuous keystrokes",该条件会在按下 Enter 键时检测并设置动画。
这是上一个的工作原理:
如您所见,在输入换行符和连续击键后,动画会滞后,这意味着动画会在每次击键时执行。
这些是修改后的,只有在按下 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);
}
});
.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 */
}
<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>
如何在删除换行时实现流畅的动画,或者如何仅在删除换行时制作动画?
我不知道这是否适用于跨浏览器,但 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);
});
所以你只需要调整你的动画:)
所以我根据Alex的回答设法解决了问题。
在他的回答中,它只在按下 Enter 键时有效。但我的目标是使用 Shift + Enter。按 Enter 将在 contenteditable
div 中插入新的 <div>
,而 Shift + Enter 插入一个新的 <br>
而不是 <div>
。所以我修改了代码,会计数<br>
,将值存储到数据中,并比较当前行数是否小于新行数,并监听Backspace 键。然后,我将 e.which === 8
替换为 newLineDeleted
变量,以告知何时删除新行。并删除 input
函数,这样它将 return 精确的布尔值(因为它似乎不适用于其中的 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);
}
});
.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 */
}
<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>
谢谢。