Reputation: 46011
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?
Upvotes: 8
Views: 44889
Reputation: 78770
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});
})
});
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});
});
Upvotes: 31
Reputation: 86902
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/
Upvotes: 2
Reputation: 8270
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");
});
Upvotes: 1
Reputation: 30012
promise() is useful here, but you'll need to wrap the contents as well. This example creates a span
but also removes it after animation. So you won't have to change your markup, nor does it change after the animation is complete.
var v = $('#Word1');
$('<span />').text(v.text()).appendTo($(v).empty()).fadeOut('fast').promise().done(function() {
$(this).text('Word 2').fadeIn().promise().done(function() {
v.text($(this).text());
$(this).remove();
});
});
example: http://jsfiddle.net/niklasvh/jkaYr/
edit or... could have just done it with the fade callbacks. Still useful if you intend to add more animation effects to it though.
var v = $('#Word1');
$('<span />').text(v.text()).appendTo($(v).empty()).fadeOut('fast',function() {
$(this).text('Word 2').fadeIn('fast',function() {
v.text($(this).text());
$(this).remove();
});
});
example: http://jsfiddle.net/niklasvh/jkaYr/15/
Upvotes: 0
Reputation: 8336
You'll need an element inside the #Word1
element that is faded out/in. It doesn't need to be a <div>
.
Upvotes: 0
Reputation: 50982
html
<div id="Word1" class="btn green" onclick="WordClicked(1);"><span id="word555">Word 1</span></div>
js
$('#word555').fadeOut('fast');
hope it helps
Upvotes: 1