標準のjQuery自動拡張/拡張textareaプラグインをiPhoneのWebアプリに実装しました。2つの問題(以下にリスト)を除いて、正常に動作しています。まず、私がgoogledを試し、さまざまなチュートリアルを試したことを強調し、これが私のニーズに最適であるという結論に達しました。
問題1.テキストエリアonKeyUpの拡張を遅らせます。どのように?キーアップ時に関数expandが呼び出されます。
$(this).keyup(update);
CSS3アニメーション(-webkit-transition)を使用して拡張をアニメーション化し、site / "app"はiPhone用に構築されているため、このアクションをたとえば500ミリ秒遅らせて、入力が遅れないようにする必要があります。コードのさまざまな部分でsetTimeOutなどのさまざまなソリューションを試しましたが、Delayなどは機能しません。限目。
問題2:テキストエリアをパディングすると、テキストエリアがややランダムに拡張され、必要な2倍に拡張されます。
padding:10px 10px;
これは既知の問題です。私は知っていますが、これまでのところ、適切に対処する方法をまだ理解していないようです。パディングを削除すると、すべてが正常に機能します。別のプラグインを使用するように提案したり、単にパディングを削除したりせずに、パディングで機能するようにコードを変更するにはどうすればよいですか?
拡張を処理するJSコード:
(function($) {
/*
* Auto-growing textareas; technique ripped from Facebook
*/
$.fn.autogrow = function(options) {
this.filter('textarea').each(function() {
var $this = $(this),
minHeight = $this.height(),
lineHeight = $this.css('lineHeight');
var shadow = $('<div></div>').css({
position: 'absolute',
top: -10000,
left: -10000,
width: $(this).width(),
fontSize: $this.css('fontSize'),
fontFamily: $this.css('fontFamily'),
lineHeight: $this.css('lineHeight'),
resize: 'none'
}).appendTo(document.body);
var update = function() {
var val = this.value.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/&/g, '&')
.replace(/\n/g, '<br/>');
shadow.html(val);
$(this).css('height', Math.max(shadow.height() + 15, minHeight));
$("#guestInfoNameLable").css('height', Math.max(shadow.height() + 15, minHeight));
}
var fix = function() {
var val = this.value.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/&/g, '&')
.replace(/\n/g, '<br/>');
shadow.html(val);
$(this).css('height', minHeight);
$("#guestInfoNameLable").css('height', minHeight);
}
$(this).keyup(update);
$(this).change(fix);
//$(this).change(update).keyup(update).keydown(update);
update.apply(this);
});
return this;
}
})(jQuery);
HTMLフォーム:
<div class="guestInfoLabel" id="guestInfoNameLable">guest</div>
<textarea id="guestInfoName" autocomplete="off" autocorrect="off"></textarea>