Fade In and Fade Out text inside a div

44,081

Solution 1

You want to wrap the text in a span then fade that out:

<div class="button"><span>TEXT!</span></div>

and you don't want to use fadeOut because that will change the size of your button as the text will disappear once fadeOut ends and no longer take up space. Instead animate the opacity:

$(".button").click(function(){
    $(this).find("span").animate({opacity:0},function(){
        $(this).text("new text")
            .animate({opacity:1});  
    })
});

http://jsfiddle.net/8Dtr6/

EDIT: Slight correction, as long as you immediately fade back in it does not seem to be an issue to use fadeIn and fadeOut, at least in chrome. I would expect maybe in lesser browsers to see a slight flicker, but could be wrong.

Possibly a bit cleaner using queue to avoid callbacks:

$(".button").click(function(){
    $(this).find("span")
        .animate({opacity:0})
        .queue(function(){
             $(this).text("new text")
                    .dequeue()
        })
        .animate({opacity:1});  
});

http://jsfiddle.net/8Dtr6/2

Solution 2

Try using the contents() function. And wrap() the Contents with another element. such as

$('#Word1').contents().wrap('<div class="temporary">').parent().fadeOut('fast');

Here is a simple demo http://jsfiddle.net/7jjNq/

Solution 3

html

<div id="Word1" class="btn green" onclick="WordClicked(1);"><span id="word555">Word 1</span></div>

js

$('#word555').fadeOut('fast');

hope it helps

Solution 4

You could put the text "Word 1" inside a span or div, like so:

<div id="Word1" class="btn green" onclick="WordClicked(1);"><span id="Word1Text">Word 1</span></div>

Then, inside of your WordClicked() function do:

$("#Word1Text").fadeOut("fast", function()
{
  $("#Word1Text").html("New Text").fadeIn("fast");
});
Share:
44,081
VansFannel
Author by

VansFannel

I'm software architect, entrepreneur and self-taught passionate with new technologies. At this moment I am studying a master's degree in advanced artificial intelligence and (in my free time ) I'm developing an immersive VR application with Unreal Engine. I have also interested in home automation applying what I'm learning with Udacity's nanodegree course in object and voice recognition.

Updated on July 12, 2022

Comments

  • VansFannel
    VansFannel almost 2 years

    I have a div with a CSS style to show it as a button:

    <div id="Word1" class="btn green" onclick="WordClicked(1);">Word 1</div>
    

    And CSS styles:

    .btn {
        display: inline-block;
        background: url(btn.bg.png) repeat-x 0px 0px;
        padding: 5px 10px 6px 10px;
        font-weight: bold;
        text-shadow: 1px 1px 1px rgba(255,255,255,0.5);
        text-align: center;
        border: 1px solid rgba(0,0,0,0.4);
        -moz-border-radius: 5px;
        -moz-box-shadow: 0px 0px 2px rgba(0,0,0,0.5);
        -webkit-border-radius: 5px;
        -webkit-box-shadow: 0px 0px 2px rgba(0,0,0,0.5);
    }
    
    .green      {background-color: #CCCCCC; color: #141414;}
    

    I want to fade out text inside div, change text, and then fade in text again. But I don't want to fade in and fade out div.

    If I use this javascript code:

    $('#Word1').fadeOut('fast');
    

    I will fade out div and text inside.

    How can I do that?

  • Niklas
    Niklas almost 13 years
    you can't animate textnodes like that.
  • VansFannel
    VansFannel almost 13 years
    If I do that, I get an exception.
  • John Hartsock
    John Hartsock almost 13 years
    @Niklas yes I see that you cant animate textnodes like that. However I have edited my answer to provide a simple solution using the contents() function and the wrap() function
  • John Hartsock
    John Hartsock almost 13 years
    @VansFannel...I modified the answer. It works now and I provided a simple demo
  • Niklas
    Niklas almost 13 years
    That's lot better, but it still doesn't work as the fadeOut() is applied on the contents() and not the parent wrap(). $('#Word1').contents().wrap('<div class="temporary">').parent().fadeOut('fast'); would hide the wrapper.
  • Wex
    Wex almost 13 years
    +1 for animate instead of FadeOut. It might make sense to set the text dynamically thought, instead of hardcoding it into the jQuery statement. Something along the lines of: var new_text = $(".button .new-text").text(); -> $(this).text(new_text);
  • James Montagne
    James Montagne almost 13 years
    @Wex Thanks, yeah I'm assuming he's going to have some way of deciding what the text will be. This was just to demonstrate the animation part of it.
  • VansFannel
    VansFannel almost 13 years
    @kingjiv: I'm using this code to do the same with an <img> tag but I'm getting a 'flick' while I'm changing. Any advice?
  • James Montagne
    James Montagne almost 13 years
    With an img it would probably look nicer to have 2 images and fade one in while the other fades out. The problem is probably that you're changing the src and once you do the new image has to load. Before it is loaded it is not taking up space.
  • VansFannel
    VansFannel almost 13 years
    @kingjiv: You can find the code I'm using here: stackoverflow.com/questions/6277427/…