其他分享
首页 > 其他分享> > jQuery的animate动画方法及动画排队问题解决

jQuery的animate动画方法及动画排队问题解决

作者:互联网

animate()动画方法

 <style>
       *{
                margin: 0;
                padding: 0;
            }
            p{
                position: relative;
                left: 0;
                margin-bottom: 10px;
                background-color: skyblue;
                width: 50px;
                height: 50px;
            }
        </style>
    <!--------------------------------------->
    <body>
        <p>1</p>
        <p>2</p>
        <p>3</p>
        <p>4</p>
        <p>5</p>
        <p>6</p>
        <p>7</p>
        <p>8</p>
     <script src="../jq/jquery-1.12.4.min.js"></script>
     <script>
            var $ps = $("p");
            //实际操作中,将时间这种易变的存储到一个变量中更好
            var during = 1000;
            //所有有数值的属性值都可以运动
            $ps.click(function(){
                $(this).animate({"width":500,"opacity":0.5},during,"swing")
            })
        </script>
    </body>

动画排队

<style>
  div{
       width: 100px;
       height: 100px;
       border: 8px solid #ccc;
       position: absolute;
       left: 0;
       top: 0;
       background: url(../../imgs/1.jpg) no-repeat center center;
     }
.box2{
      border-radius: 50%;
      overflow: hidden;
       }
div span{
       position: absolute;
       left: 0;
       top: 0;
       width: 100%;
       height: 100%;
       background-color: rgba(221, 130, 130, 0.4);
     }
</style>
  <!-------------css样式------------------->
<body>
  <div class="box1"><span></span></div>
  <div class="box2"><span></span></div>
  
<script src="../jq/jquery-1.12.4.min.js"></script>
<script>
 var $box1 = $(".box1");
 var $box2 = $(".box2");
 var during = 2000;
 //动画排队对比
 $box2.animate({"left": 400,"top":400},during)
 //box1、box2上各自进行各自的动画
 //同一元素上的多个动画排队
 $box1.animate({"left": 400},during)//排队
 $box1.animate({"top": 400}, during)
 // 动画的底部就是一个定时器,异步加载
 // 非运动的代码,关于同一个元素对象的,不会排队
 //$box1.css("height",200)
 </script>
</body>
//其他的运动方法,如果设置给同一个元素,也会发生排队
  $box2.mouseenter(function(){
      $(this).children().slideUp(during)
  })
  $box2.mouseleave(function(){
      $(this).children().slideDown(during)
  })
  //鼠标快速的移上移下,之后box2的灰色span就一直在滑进滑出,直到执行完所有次数
//同一个元素进行多个运动,可以简化通过链式调用实现
//但是还是要进行排队         
$box1.children().fadeOut(500).fadeIn(500).fadeOut(500).fadeIn(500).fadeOut(500).fadeIn(500)
delay()延迟方法
   //延迟
$box1.children().delay(3000).fadeOut(500).fadeIn(500).fadeOut(500).fadeIn(500).fadeOut(500).fadeIn(500)
stop()停止动画方法
<style>
 div {
      width: 100px;
      height: 100px;
      border: 8px solid #ccc;
      position: absolute;
      left: 0;
      top: 40;
      background: url(../../imgs/1.jpg) no-repeat center center;
   }  
 div span {
      position: absolute;
      left: 0;
      top: 0;
      width: 100%;
      height: 100%;
      background-color: rgba(221, 130, 130, 0.4);
    }
 </style>
</head>
  
<body>
 <input type="button" value="true true" id="btn1">
 <input type="button" value="true false" id="btn2">
 <input type="button" value="false false" id="btn3">
 <input type="button" value="false true" id="btn4"><br>
  
 <div class="box1"><span></span></div>
  
  
<script src="../jq/jquery-1.12.4.min.js"></script>
<script>
   var $box1 = $(".box1");
  
   var during = 2000;
  
   //同一元素上的多个动画排队
   $box1.animate({ "left": 400 }, during)
   $box1.animate({ "top": 400 }, during)
   $box1.animate({"left":0},during)
   $box1.animate({"top":40},during)
 
  // 停止动画
  //添加按钮点击事件
  var $btn1 = $("#btn1")
  var $btn2 = $("#btn2")
  var $btn3 = $("#btn3")
  var $btn4 = $("#btn4")
  
  //true true  清空后面排队动画  且  当前动画立即完成,停到结束位置
  $btn1.click(function () {
       $box1.stop(true, true);
  })
  //true false  清空动画  停在当前
  $btn2.click(function () {
     $box1.stop(true, false);
  })
  //false false  不清空后面动画,停在当前
  //然后执行下一步动画
  $btn3.click(function () {
     $box1.stop(false, false);
  })
  //false true  不清空后面动画,当前运动立即到结尾
  $btn4.click(function () {
       $box1.stop(false, true);
  })
 </script>
</body>

清空动画排队

动画排队问题
<style>
 div {
     width: 100px;
     height: 100px;
     border: 8px solid #ccc;
     position: absolute;
     left: 0;
     top: 0;
     background: url(../../imgs/1.jpg) no-repeat center center;
   } 
div span {
     position: absolute;
     left: 0;
     top: 0;
     width: 100%;
     height: 100%;
     background-color: rgba(221, 130, 130, 0.4);
  }
  </style>
<body>
  <input type="button" value="true true" id="btn1">
  <input type="button" value="true false" id="btn2">
  <input type="button" value="false false" id="btn3">
  <input type="button" value="false true" id="btn4"><br>

  <div class="box1"><span></span></div>

  <script src="../jq/jquery-1.12.4.min.js"></script>
  <script>
     var $box1 = $(".box1");

     var during = 2000;
    //清空动画
    $box1.mouseenter(function(){
        $(this).children().stop(true).slideUp(during)
    })

   $box1.mouseleave(function(){
       $(this).children().stop(true).slideDown(during)
   })
    </script>
</body>
方法二:利用函数节流方法

如果元素在运动,直接return,不要执行后面的运动代码。

每个jQuery对象,都能调用一个is()方法,作用是显示元素对象的某种状态
如果参数位置是is(":animated"),animated是正在运动的意思,返回值是布尔值,true表示正在运动,false表示没有运动

//函数节流方法
$box1.mouseenter(function(){
    if(is(":animated")){
        //如果判断是正在运动的话,直接return返回,不再执行其他动画
        return;
    }
    //没有运动的话,则继续执行后面的新动画
    $(this).children().slideup(during);

})

$box1.mouseenter(function(){
    if(is(":animated")){
        //如果判断是正在运动的话,直接return返回,不再执行其他动画
        return;
    }
    //没有运动的话,则继续执行后面的新动画
    //有时候为了保险起见,会与stop(true)结合使用
    $(this).children().stop(true).slideup(during);

})

标签:jQuery,动画,true,排队,stop,during,animate,box1
来源: https://www.cnblogs.com/dreamtown/p/14613289.html