代码之家  ›  专栏  ›  技术社区  ›  Jake Wilson

jquery文本从一个文本到另一个文本的淡入淡出/转换?

  •  56
  • Jake Wilson  · 技术社区  · 14 年前

    jquery显然可以轻松地淡入/淡出文本。但是如果你想把文本从一件事改成另一件事呢?这种情况会随着过渡而发生吗?

    例子:

    <div id='container'>Hello</div>
    

    你能改一下文字吗 你好 世界 但是,它是否会随着过渡(如渐变或某种效果)而改变,而不是立即改变?

    6 回复  |  直到 11 年前
        1
  •  102
  •   Nick Craver    14 年前

    您可以使用回调,如下所示:

    $("#container").fadeOut(function() {
      $(this).text("World").fadeIn();
    });
    

    You can give it a try here 或者由于队列在这种特定情况下的工作方式,例如 this :

    $("#container").fadeOut(function() {
      $(this).text("World")
    }).fadeIn();
    

    这将执行 .text() 调用时 .fadeOut() 是完整的,就在再次淡入。

        2
  •  39
  •   Viktor Stískala    14 年前

    如果使用hide/show或fadein/fadeout,可能会遇到一些“跳跃”效果,因为它会更改CSS的显示属性。我建议使用不透明度动画。

    这样地:

    $('#container').animate({'opacity': 0}, 1000, function () {
        $(this).text('new text');
    }).animate({'opacity': 1}, 1000);
    
        3
  •  21
  •   ASGM    11 年前

    这里是一个 live example .

    (function() {
    
    var quotes = $(".quotes");
    var quoteIndex = -1;
    
    function showNextQuote() {
        ++quoteIndex;
        quotes.eq(quoteIndex % quotes.length)
            .fadeIn(2000)
            .delay(2000)
            .fadeOut(2000, showNextQuote);
    }
    
    showNextQuote();
    
    })();
    

    它运作良好。

        4
  •  4
  •   Moin Zaman    14 年前

    我能想到的一种方法是让子元素具有文本,并且只显示一个开始,然后逐个淡入其他元素。

    看看这里: http://jsfiddle.net/VU4CQ/

        5
  •  2
  •   Kiernan Burke    12 年前

    使用数组查找文本和颜色更改、转换速度和鼠标输入、鼠标保存事件 this menu 这样地:

    $('#id a').mouseenter(function() {
        $(this).fadeOut(
        eSpeed, function() {
            $(this).text(sayThis[0]);
            $(this).css({
                color: eColor
            });
        }).fadeIn(eSpeed);
    });
    
    
    $('#id a').mouseleave(function() {
        $(this).fadeOut(
        eSpeed, function() {
            $(this).text(sayThat[0]);
            $(this).css({
                color: lColor
            });
        }).fadeIn(eSpeed);
    });
    
        6
  •  -1
  •   Primoz Rome    11 年前

    我建议你用 basic slider jQuery plugin . 非常轻(6K),可以做你想做的,有两个定制选项,没有大多数Slider插件的混乱。我一直在使用它,它很好。