jQuery之動畫animate(animate、stop、finish、delay、queue、clearQueue)

animate

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title></title>
  <style>
    .demo {
      width: 0;
      height: 0;
      position: absolute;
      background-color: #ff0000;
    }

    .demo2 {
      width: 0;
      height: 0;
      position: absolute;
      top: 100px;
      background-color: #0000ff;
    }
  </style>
</head>

<body>
  <div class="demo"></div>

  <div class="demo2"></div>
</body>
<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script>
  //animate動畫
  $('.demo').animate({ width: '+=50', height: '+=50', top: '+=100', left: '+=100' }, 1000, 'swing', function () {
    console.log('animate')
  })

  //使動畫執行兩次或者多次
  $('.demo2')
    .animate({ width: '+=50', height: '+=50', top: '+=100', left: '+=100' }, 1000, 'swing', function () { })
    .animate({ width: '+=50', height: '+=50', top: '+=100', left: '+=100' }, 1000, 'swing', function () { })
</script>

</html>

stop、finish、delay

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title></title>
  <style>
    .demo {
      width: 0;
      height: 0;
      position: absolute;
      background-color: #ff0000;
    }
  </style>
</head>

<body>
  <button id="stopBtn">stop</button>
  <button id="finishBtn">finish</button>
  <button id="startBtn">start</button>
  <div class="demo"></div>
</body>
<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script>
  //使動畫執行兩次或者多次
  //delay(2000):使動畫延遲2秒
  $('#startBtn').on('click', function () {
    $('.demo')
      .animate({ width: '+=50', height: '+=50', top: '+=100', left: '+=100' }, 1000, 'swing', function () { })
      .delay(2000)
      .animate({ width: '+=50', height: '+=50', top: '-=200', left: '+=100' }, 1000, 'swing', function () { })
  })

  //使動畫停止
  $('#stopBtn').on('click', function () {
    $('.demo').stop()
  })

  //使動畫直接完成
  $('#finishBtn').on('click', function () {
    $('.demo').finish()
  })
</script>

</html>

queue 、clearQueue

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title></title>
  <style>
    .demo {
      width: 0;
      height: 0;
      position: absolute;
      background-color: #ff0000;
    }
  </style>
</head>

<body>
  <div class="demo"></div>
</body>
<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script>
  //queue隊列方法
  $('.demo')
    .queue('chain', function (next) {
      console.log('over1')
    })
    .queue('chain', function (next) {
      console.log('over2')
    })
    .queue('chain', function (next) {
      console.log('over3')
    })

  console.log($('.demo').queue('chain'))

  //清除隊列
  console.log($('.demo').clearQueue('chain'))
  console.log($('.demo').queue('chain'))
</script>

</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章