2017-03-07 47 views
0

的淡出我找到了一个好剧本,将我的项目 工作,但无法弄清楚如何使图像淡入淡出,并用这个脚本淡入和图像

$(document).ready(function() { 
    var activeId = $(".active").each(function(){ 
     $("#content" + $(this).attr("id").replace("tab","")).show(); 
    }); 
    $(".tabs a").click(function() { 
     var $tabs =$(this).closest(".tabs"); 
     $("#content" +$tabs.attr("data-lastContent")).hide(); 
     $(this).closest(".tabs").find(".active").removeClass("active"); 
     $(this).addClass("active") 
     var id = $(this).closest("li").attr("id").replace("tab",""); 
     $tabs.attr("data-lastContent", id); 
     $("#content" + id).show(); 

    }); 

}); 

http://jsfiddle.net/hKMFb/446/

+0

最好是你问一个问题之前进行搜索[所以]。用['.fadeIn()'](http://api.jquery.com/fadein/)替换'.show()'和用''.fadeOut()']替换'.hide()'(http:/ /api.jquery.com/fadeout/)。请参阅链接页面以了解选项语法 –

+0

你有什么尝试?您提供了一个可用的“标签”脚本,但不提供您尝试过的更改。 –

+0

http://jsfiddle.net/hKMFb/451/ –

回答

0

您需要在hide()和show()方法中保持淡入淡出速度。 它可能对你的工作如下链接例子中提到的: http://jsfiddle.net/hKMFb/450/

$(document).ready(function() { 
    var activeId = $(".active").each(function(){ 
     $("#content" + $(this).attr("id").replace("tab","")).show(1000); 
    }); 
    $(".tabs a").click(function() { 
     var $tabs =$(this).closest(".tabs"); 
     $("#content" +$tabs.attr("data-lastContent")).hide(1000); 
     $(this).closest(".tabs").find(".active").removeClass("active"); 
     $(this).addClass("active"); 
     var id = $(this).closest("li").attr("id").replace("tab",""); 
     $tabs.attr("data-lastContent", id); 
     $("#content" + id).show(1000); 

    }); 

}); 

您也可以使用fadeIn()和​​动画

$(document).ready(function() { 
    var activeId = $(".active").each(function(){ 
     $("#content" + $(this).attr("id").replace("tab","")).fadeIn(1000); 
    }); 
    $(".tabs a").click(function() { 
     var $tabs =$(this).closest(".tabs"); 
     $("#content" +$tabs.attr("data-lastContent")).fadeOut(1000).hide(); 
     $(this).closest(".tabs").find(".active").removeClass("active"); 
     $(this).addClass("active"); 
     var id = $(this).closest("li").attr("id").replace("tab",""); 
     $tabs.attr("data-lastContent", id); 
     $("#content" + id).fadeIn(1000); 

    }); 
}); 
+0

这是好的,但它调整我的div每次它改变图像,它会创建动画队列 –