自定义动画方法animate()

1.自定义简单动画

animate()方法可以使元素动起来,而且animate()方法更具有灵活性.通过animate()方法,能够实现更加精细新颖的动画效果;使用animate()方法之前,为了能影响该元素的"top","left","bottom"和"right"样式属性,必须把元素的position样式设置为"relative"或者"absolut"

<style type="text/css">
*{margin:0;padding:0;}	
body { padding: 60px }
#panel {position: relative; width: 100px; height:100px;border: 1px solid #0050D0;background: #96E555; cursor: pointer}
</style>
<script type="text/javascript">
$(function(){
//      $("#panel").click(function(){
//	   $(this).animate({left: "500px"}, 3000);
//	})      
   $("#panel").toggle(function(){
	    $(this).animate({left: "500px"}, 3000);
	},function(){
	    $(this).animate({top:"200px"}, 3000);
	})
})
</script>
</head>
<body>
<div id="panel"></div>
</body>

2.累加,累减动画

在之前的代码中,设置了{left:"500px"}作为动作参数.如果在500px之前加上"+="或者"-="符号表示在当前位置累加或者累减

3.多重动画

(1)同时执行多个动画

如果需要向右滑动的同时放大元素的高度.{left:"500px",height:"200px"}

<style type="text/css">
*{margin:0;padding:0;}	
body { padding: 60px }
#panel {position: relative; width: 100px; height:100px;border: 1px solid #0050D0;background: #96E555; cursor: pointer}
</style>
<script type="text/javascript">
$(function(){
    $("#panel").click(function(){
	    $(this).animate({left: "500px",height:"200px"}, 3000);
	})
})
</script>
</head>
<body>
<div id="panel"></div>
</body>

(2)按顺序执行多个动画

上面的是同时进行的,如果需要先向左移动然后再放大高度,按顺序写入代码就可以了$(this).animate({left:"500px"},3000);$(this).animate({height:"500px"},3000);或者直接串联$(this).animate({left:"500px"},3000).animate({height:"500px"},3000);这个称为动画队列

<style type="text/css">
*{margin:0;padding:0;}    
body { padding: 60px }
#panel {position: relative; width: 100px; height:100px;border: 1px solid #0050D0;background: #96E555; cursor: pointer}
</style>
<script type="text/javascript">
$(function(){
    $("#panel").click(function(){
         $(this).animate({left: "500px"}, 3000)
                .animate({height: "200px"}, 3000);
    })
})
</script>
</head>
<body>
<div id="panel"></div>
</body>

4.综合动画

单机div元素让它向右移动的同时增大它的高度,并将它们的不透明度从50%变换为100%,然后再让它从上到下移动,同时它的宽度变大,当完成这些效果后,淡出方式隐藏

<style type="text/css">
*{margin:0;padding:0;}	
body { padding: 60px }
#panel {position: relative; width: 100px; height:100px;border: 1px solid #0050D0;background: #96E555; cursor: pointer}
</style>
<script type="text/javascript">
    $(function(){
        $("#panel").css("opacity", "0.5");
        $("#panel").click(function(){
              $(this).animate({left: "400px", height:"200px" ,opacity: "1"}, 3000)
					 .animate({top: "200px" , width :"200px"}, 3000 )
					 .fadeOut("slow");
        });
    });
</script>
</head>
<body>
<div id="panel"></div>
</body>


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值