Skip to content

Instantly share code, notes, and snippets.

@znz
Created February 15, 2013 05:17
Show Gist options
  • Save znz/4958688 to your computer and use it in GitHub Desktop.
Save znz/4958688 to your computer and use it in GitHub Desktop.
jQuery Mobile を参考にして作った textarea 自動拡大
do ($ = jQuery) ->
jQuery.fn.autogrow = (options) ->
settings = $.extend {
extraLineHeight: 15
timeoutBuffer: 100
}, options
self = this
timerId = self.removeData('timerId')
if timerId
clearTimeout timerId
handler = ->
self.each (i,e) ->
scrollHeight = e.scrollHeight
clientHeight = e.clientHeight
if clientHeight < scrollHeight
$(e).height(scrollHeight + settings.extraLineHeight)
self.removeData('timerId')
self.data('timerId', setTimeout(handler, settings.timeoutBuffer))
$(document).on 'keyup.autogrow', 'textarea', ->
$(this).autogrow()
$('textarea').autogrow()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment