tags:

views:

42

answers:

2

OK rememebr how I wanted an IMAGE to go in and out according to a word? I don't know what I was thinking but I was wrong. I wanted TEXT to go in and out.

Now what's the problem? Everything is the way I want it except for ONE thing... is it possible to be able to update the DIV HTML in a way that it FADES during the transition? Maybe using jQuery?

Here is what I have so far:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
    <meta name="author" content="Dan Jasnowski" />

    <title>Untitled 2</title>

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;&lt;/script&gt;

    <script type="text/javascript">
    $(function() {
   $('#textfield').keyup(function () {
     switch($(this).val()) {
       case 'dan':
         $('#dan').html("<b>Dan is the name!</b>");
         break;
       case 'apple':
         $('#dan').html("<b>Apples are good!</b>");
         break;
     }
   });
});

</script>

</head>

<body>

<input type="text"  name="dan" id="textfield" />
<div id="dan">Lorem impulse dolor...</div>
</body>
</html>
A: 

No, it's not possible to do it on one <div> only, you should have two <div> elements sitting one above the other, using z-index, one containing <b>Dan is the name!</b> and the other containing <b>Apples are good!</b> and then fade the one out while fading the next one in.

Hope I understood your question correctly.

Anriëtte Combrink
+2  A: 

How about something like this instead of cross fading:

<script type="text/javascript">
$(function() {
    $('#textfield').keyup(function () {
     switch($(this).val()) {
       case 'dan':
         $('#dan').fadeOut("fast",function() { $(this).html("<b>Dan is the name!</b>").fadeIn("fast"); });
         break;
       case 'apple':
         $('#dan').fadeOut("fast",function() { $(this).html("<b>Apples are good!</b>").fadeIn("fast"); });
         break;
     }
    });
});

</script>

Fades the old text out, swaps to the new text and fades in.

You could achieve a cross fade by absolute positioning elements and as suggested using z-index. It is tricky but can be achieved (theoretically). This is simple and more or less achieves the desired effect.

Josh Stuart
BRILLIANT! Only nitpick I have is that the text re-fades when I press keys like CTRL , SHIFT, ALT, etc. I just want it to NOT fade when those kind of keys are pressed but I can live. THANKS!!
Dan
The reason it does that is because you are checking the case each time a key is released. So if you have entered "dan" and then hit "esc", "shift", "alt" etc if will re-check. You could tackle this in a bunch of ways. eg. exclude those characters http://api.jquery.com/keyup/ - which is probably the best way. You could also check if you have already set that case. I'll revise my answer if you want to handle this?
Josh Stuart