1. 程式人生 > >jQuery 動畫效果,消失,顯現,漸出,漸入

jQuery 動畫效果,消失,顯現,漸出,漸入

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <script type="text/javascript" src="js/jquery-3.1.1.min.js" ></script>
        <title></title>
        <script>
            $(function(){
                $(but1).on("click",function(){
                    $("img").hide(500) ; //消失
                }) ;
                $(but2).on("click",function(){
                    $("img").show(5000) ; //顯現
                }) ;
                $(but3).on("click",function(){
                    $("img").slideUp(5000) ; //滑動消失
                }) ;
                $(but4).on("click",function(){
                    $("img").slideDown(5000) ; //滑動顯現
                }) ;
                $(but5).on("click",function(){
                    $("img").slideToggle(5000) ; //滑動切換(消失後顯現,顯現後消失)
                }) ;
                $(but6).on("click",function(){
                    $("img").fadeOut(5000) ; //淡出
                }) ;
                $(but7).on("click",function(){
                    $("img").fadeIn(5000) ; //淡入
                }) ;
                $(but8).on("click",function(){
                    $("img").fadeTo(500,0.5) ; //淡化
                }) ;
                $(but9).on("click",function(){
                    $("div").animate({left:"800px"},5000) ; //移動(需要調整物件的style屬性中position的值absolute)
                }) ;
            }) ;    
        </script> 
    </head>
    <body> 
        <input type="button" id="but1" value="消失"/>
        <input type="button" id="but2" value="顯現"/>
        <input type="button" id="but3" value="滑動消失"/>
        <input type="button" id="but4" value="滑動顯現"/>
        <input type="button" id="but5" value="滑動切換"/>
        <input type="button" id="but6" value="淡出"/>
        <input type="button" id="but7" value="淡入"/>
        <input type="button" id="but8" value="淡化"/>
        <input type="button" id="but9" value="移動"/>
        <div style="position: absolute;"><img src="001.jpg"  height="200"></div>
    </body>
</html> 

這裡的方法都可以再巢狀方法。

物件.方法名(延遲時間,function(){新的方法});

比如,消失後顯現

$(but1).on("click",function(){
	$("img").hide(500,function(){
		$("img").show(5000) ;
	}) ;
}) ;
比如,移動後再移回
$(but9).on("click",function(){
	$("div").animate({left:"800px"},5000,function(){
		$("div").animate({left:"0px"},5000) ;
	}) ;
}) ;