我想在文本更改上淡出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>
答案 0 :(得分: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});
})
});
编辑:轻微修正,只要你立即淡入它,使用fadeIn
和fadeOut
似乎不是问题,至少在chrome中。我希望可能在较小的浏览器中看到轻微的闪烁,但可能是错误的。
使用队列可能会更清洁以避免回调:
$(".button").click(function(){
$(this).find("span")
.animate({opacity:0})
.queue(function(){
$(this).text("new text");
$(this).dequeue()
})
.animate({opacity:1});
});