在div中淡入和淡出文本

时间:2022-11-25 14:32:26

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

我有一个CSS样式的div来显示它作为一个按钮:

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

And CSS styles:

和CSS样式:

.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.

我想淡出div中的文本,更改文本,然后再次淡入文本。但我不想淡出淡出div。

If I use this javascript code:

如果我使用这个javascript代码:

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

I will fade out div and text inside.

我会在里面淡出div和文字。

How can I do that?

我怎样才能做到这一点?

6 个解决方案

#1


29  

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:

并且您不想使用fadeOut,因为这会改变按钮的大小,因为一旦fadeOut结束并且不再占用空间,文本将消失。而是为不透明度设置动画:

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

http://jsfiddle.net/8Dtr6/

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.

编辑:轻微修正,只要你立即淡出它似乎不是一个问题使用fadeIn和fadeOut,至少在chrome中。我希望可能在较小的浏览器中看到轻微的闪烁,但可能是错误的。

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

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

#2


3  

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

尝试使用contents()函数。并使用另一个元素wrap()内容。如

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

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

这是一个简单的演示http://jsfiddle.net/7jjNq/

#3


1  

html

HTML

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

Word 1

js

JS

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

$( '#word555')淡出( '快')。

hope it helps

希望能帮助到你

#4


1  

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

您可以将文本“Word 1”放在span或div中,如下所示:

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

Then, inside of your WordClicked() function do:

然后,在WordClicked()函数内部执行:

$("#Word1Text").fadeOut("fast", function()
{
  $("#Word1Text").html("New Text").fadeIn("fast");
});

#5


0  

You'll need an element inside the #Word1 element that is faded out/in. It doesn't need to be a <div>.

你需要一个淡入/淡入的#Word1元素中的元素。它不需要是

#6


0  

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.

promise()在这里很有用,但你也需要包装内容。此示例创建一个span,但也会在动画后删除它。因此,您不必更改标记,也不会在动画完成后更改标记。

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/

示例: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/

例如:http://jsfiddle.net/niklasvh/jkaYr/15/

#1


29  

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:

并且您不想使用fadeOut,因为这会改变按钮的大小,因为一旦fadeOut结束并且不再占用空间,文本将消失。而是为不透明度设置动画:

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

http://jsfiddle.net/8Dtr6/

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.

编辑:轻微修正,只要你立即淡出它似乎不是一个问题使用fadeIn和fadeOut,至少在chrome中。我希望可能在较小的浏览器中看到轻微的闪烁,但可能是错误的。

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

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

#2


3  

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

尝试使用contents()函数。并使用另一个元素wrap()内容。如

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

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

这是一个简单的演示http://jsfiddle.net/7jjNq/

#3


1  

html

HTML

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

Word 1

js

JS

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

$( '#word555')淡出( '快')。

hope it helps

希望能帮助到你

#4


1  

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

您可以将文本“Word 1”放在span或div中,如下所示:

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

Then, inside of your WordClicked() function do:

然后,在WordClicked()函数内部执行:

$("#Word1Text").fadeOut("fast", function()
{
  $("#Word1Text").html("New Text").fadeIn("fast");
});

#5


0  

You'll need an element inside the #Word1 element that is faded out/in. It doesn't need to be a <div>.

你需要一个淡入/淡入的#Word1元素中的元素。它不需要是

#6


0  

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.

promise()在这里很有用,但你也需要包装内容。此示例创建一个span,但也会在动画后删除它。因此,您不必更改标记,也不会在动画完成后更改标记。

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/

示例: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/

例如:http://jsfiddle.net/niklasvh/jkaYr/15/