Skip to content

Instantly share code, notes, and snippets.

@bluerabbit
Created June 26, 2012 13:10
Show Gist options
  • Save bluerabbit/2995714 to your computer and use it in GitHub Desktop.
Save bluerabbit/2995714 to your computer and use it in GitHub Desktop.
youRoomの書き込むテキストエリアが動的に変わればいいのに
$('.entry_content').scroll(function () {
var self = $(this);
self.css('height', (parseInt(self.css('height').slice(0,-2)) + 10) + 'px');
});
@bluerabbit
Copy link
Author

という事でググってみたら下記のようにすればいいんじゃないの?という事だった。livequeryだったらscroll対応してたりしないのかな。

$('.entry_content').live('keyup', function() {
    var el = $(this);
    if (!el.data("has-scroll")) {
        el.data("has-scroll", true);
        el.scroll(function(){
           resizeTextArea(el);
        });
    }
    resizeTextArea(el);
});

function resizeTextArea(elem) {
    elem.scrollTop(0);
    elem.height(elem[0].scrollHeight - elem[0].clientHeight + elem.height());
}

@bluerabbit
Copy link
Author

適当なliveとってイベント割り当てすればいいんじゃないのと言うことで下記になった。

$('.entry_content').live('focus', function () {
    var self = $(this);
    if (!self.data("has-scroll")) {
        self.data("has-scroll", true);
        self.scroll(function () {
            self.css('height', (parseInt(self.css('height').slice(0,-2)) + 10) + 'px');
        });
    }
});

@bluerabbit
Copy link
Author

scrollHeight, clientHeight, height()って複数ブラウザで互換性あるのかな?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment