0

我想淡化 div 的文本更改,我做了一个小功能,但它不起作用。它改变了价值,但没有褪色。

$(function(){   
    $('.jqTransformSelectWrapper').find('ul').find('a').click(function(){
        var cityVal= $('.jqTransformSelectWrapper').find('ul').find('.selected').text();
        var capCity= cityVal.toLowerCase();

        if(capCity == "first"){
            $('#flightPrice').text('1').fadeOut('slow');            
        }
    })
})

//html

<a index="1" href="#" class="selected">first</a>

<span id="flightPrice">val</span>
4

1 回答 1

1

您想将文本包装在一个跨度中,然后将其淡出:

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

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

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

http://jsfiddle.net/8Dtr6/

编辑:稍微更正,只要你立即淡入它似乎不是使用问题,fadeIn并且fadeOut至少在 chrome 中是这样。我希望在较小的浏览器中可能会看到轻微的闪烁,但可能是错误的。

使用队列来避免回调可能会更干净一些:

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

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

于 2012-04-16T09:52:51.320 回答