Fork me on GitHub

jQuery中的动画

jQuery中常用设置动画的方法

  1. $("p").hide(1000); //隐藏
  2. $("p").css("display", "none");
  3. $("p").show(1000); //显示
  4. $("p").fadeOut(); //淡出,改变透明度
  5. $("p").fadeIn(); //淡入
  6. $("p").slideUp(); //改变高度
  7. $("p").slideDown();

自定义动画方法animate()

格式

  1. animate(params, speed, callback);
  2. //params,样式及映射
  3. //speed,速度
  4. //callback,回调函数。在动画完成时执行的函数。

例子

  1. <style type="text/css">
  2. *{margin:0;padding:0;}
  3. body { padding: 60px }
  4. #panel {position: relative; width: 100px; height:100px;border: 1px solid #0050D0;background: #96E555; cursor: pointer}
  5. </style>
  6. <div id="panel"></div>
  7. <script src="../../scripts/jquery.js" type="text/javascript"></script>
  8. <script type="text/javascript">
  9. $(function(){
  10. $("#panel").click(function(){
  11. $(this).animate({left: "500px"}, 3000);
  12. })
  13. })
  14. </script>

累加动画

  1. $(this).animate({left: "+=500px"}, 3000);

多重动画1

在一个animate()方法中应用多个属性,动画是同时发生的。

  1. $(this).animate({left: "500px",height:"200px"}, 3000);

多重动画2

当以链式写法应用动画方法时,动画是按照顺序发生的。

  1. $(this).animate({left: "500px"}, 3000).animate({height: "200px"}, 3000);

综合动画

  1. $(this).animate({left: "400px", height:"200px" ,opacity: "0.5"}, 3000)
  2. .animate({top: "200px" , width :"200px"}, 3000 )
  3. .fadeOut("slow");

回调函数和queue()队列

动画执行完成后,调用回调函数

其他非动画方法会插队,如果也要按照顺序执行,要写在回调或者queue()方法中

  1. $(this).animate({left: "400px", height:"200px" ,opacity: "0.5"}, 3000)
  2. .animate({top: "200px" , width :"200px"}, 3000 ,function(){
  3. $(this).css("border","5px solid blue");
  4. }).slideUp("slow");
  5. });
  6. $(this).animate({left: "400px", height:"200px" ,opacity: "0.5"}, 3000)
  7. .animate({top: "200px" , width :"200px"}, 3000 )
  8. .queue(function(next){
  9. $(this).css("border","5px solid blue");
  10. next(); //继续下一个动画
  11. })
  12. .slideUp("slow");

判断是否动画中

  1. if(!$("p").is(":animated")) {
  2. }

延迟动画

  1. $(this).animate({left: "400px", height:"200px" ,opacity: "1"}, 3000)
  2. .delay(1000)
  3. .animate({top: "200px" , width :"200px"}, 3000 )
  4. .delay(2000)
  5. .fadeOut("slow");

停止动画

  1. stop([clearQueue],[gotoEnd]);
  2. //clearQueue,清空队列
  3. //gotoEnd,直接跳到动画的最终状态
  4. $(this).stop(); //清除一个动画队列
  5. $(this).stop(true); //清除所有动画队列
  6. $(this).stop(true, true); //清除动画队列,并到达最终状态
  1. <script>
  2. $(function () {
  3. $(".button1").click(function () {
  4. $("#panel").animate({left: "400px", height:"200px" ,opacity: "0.5"}, 3000)
  5. .animate({top: "200px" , width :"200px"}, 3000 )
  6. .fadeOut("slow");
  7. })
  8. $(".button2").click(function(){
  9. // $("#panel").stop(); //清除一个动画队列
  10. // $("#panel").stop(true); //清除所有动画队列
  11. $("#panel").stop(true, true); //清除动画队列,并到达最终状态
  12. })
  13. })
  14. </script>
  15. <div id="panel"></div>
  16. <button class="button1">button1</button>
  17. <button class="button2">button2</button>

其他动画方法

  1. toggle(); //切换可见
  2. slideToggle(); //通过高度切换可见
  3. fadeTo(600, 0.2); //到哪个透明度?
  4. fadeToggle() //通过透明度切换可见
posted @ 2018-10-25 14:10  big2cat  阅读(180)  评论(0编辑  收藏  举报