1

I need to animate the opacity of the text in textarea, so it fades away.This happens when someone presses enter. I have setup the code for the enter press, but can't get the code to animate to work. Here is the code I have:

$(document).keydown(function(e){
if(e.which === 13){
// here I would like to animate the text
   };
});
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
fluxus
  • 559
  • 5
  • 15
  • 29

4 Answers4

2

For only the text, I would think you would need jQueryUI, and do something like this:

$("#textarea").keydown(function(e){
    if(e.which === 13){
        $(this).animate({color: 'transparent'},1000);
   }
});

Here's a fiddle: http://jsfiddle.net/adeneo/7pkNK/

Be aware that the cursor will disappear as well, and as far as I know the only way around it is to create a cursor with an animated gif or some other custom code.

adeneo
  • 312,895
  • 29
  • 395
  • 388
1
    $(document).keydown(function(e){
     if(e.which === 13){
      $(formelement).animate({ color: "#FFFFFF" }, 600);

   };
});
Dominic Green
  • 10,142
  • 4
  • 30
  • 34
0

Would this example from the JQuery API help?

<div id="clickme">
   Click here
</div>
<img id="book" src="book.png" alt="" width="100" height="123" />

$('#clickme').click(function() {
  $('#book').fadeOut('slow', function() {
    // Animation complete.
  });
});

Check out the API for .fadeIn() and .fadeTo() as well.

In your case, you could just use $('#text' ).fadeOut();

dremme
  • 739
  • 2
  • 10
  • 18
  • Thanks, i now what FadeOut does, but i need to fade the text away, not the whole textarea. – fluxus Dec 11 '11 at 22:31
  • @suludi If you wrap text in a or

    tag, you can do something like: `$('#textarea span').fadeOut()`. This will fade only the text.

    – dremme Dec 11 '11 at 22:37
  • That's not possible, you can't wrap the text in a textarea in HTML elements. – adeneo Dec 11 '11 at 22:40
  • @suludi you can also assign the text an ID `` then... `$('#text').fadeOut(); – dremme Dec 11 '11 at 22:41
  • @adeneo Sorry I misspoke. I specified another element like a
    with the ID of "textarea". Not the HTML element
    – dremme Dec 11 '11 at 22:43
0

Try doing it using jQuery's fadeOut method.

$(document).keydown(function(e) {
    var code = (e.keyCode ? e.keyCode : e.which);
    if (code === 13) {
        //here i would like to animate the text
        $('#idofobject').fadeOut(optionalduration, optionalcallback);
    }
});

I'd retrieve the key being pressed using the second line of my code, as per this SO answer.


After reading your comment to @dremme, I don't believe there's a way to fade just the text that is simple. You'd have to place a div over top of your textarea with the text in it, delete the text that is currently in the textarea, and fadeout your covering div.

Community
  • 1
  • 1
JesseBuesking
  • 6,496
  • 4
  • 44
  • 89