仅爱此一生 2019-05-23 22:56 采纳率: 0%
浏览 517

jQuery里面有stop()方法停止当前的动画,那么怎样才能开启原来停止的动画呢。超级超级感谢大佬们

我按下按钮之后,动画正常进行,我把鼠标放在动画上面的时候,动画就停止了。我现在的问题就是,我把鼠标移开动画区域的时候,怎样才能让动画继续运行,是从停止时的那个状态继续运行,不是重新开始。

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>jQuery特效</title>
  <script src="jquery-3.4.1.js"></script>
  <style>
    div{
      border: 1px solid blue;
      padding: 20px 50px;
      width: 300px;
      height: 200px;
      color: red;
      position: relative;
    }
  </style>
</head>
<body>
<button type="button" id="btn8" class="btn">动画效果</button>
<br>
<div >这是动画栏</div>
<script>
  $(document).ready(function () {
    //更改动画
    //一定要在格式中添加位置属性 默认为:  position:relative
    $("#btn8").click(function () {
      $("div").animate({
        left:"+=200",
        top:"+=200"
      },2000);
      $("div").animate({
        left:"+=200",
        top: "-=200"
      },2000);
      $("div").animate({
        left:"-=200",
        top: "-=200"
      },2000);
      $("div").animate({
        left:"-=200",
        top: "+=200"
      },2000);
    })

    //当鼠标停在动画框时停止动画
    $("div").hover(function () {
      $("div").stop(true,false)
      $("div").text("当前动画框静止!")
    })

    //鼠标离开动画框之后继续播放动画
    $("div").mouseleave(function () {
      $("div").text("这是动画栏!")
    })
  })
</script>
</body>
</html>

  • 写回答

2条回答

  • 听楼一夜雨 2019-05-24 10:09
    关注
    <script>
      $(document).ready(function () {
        //更改动画
        //一定要在格式中添加位置属性 默认为:  position:relative
        $("#btn8").click(function () {
            animate_test();
        //当鼠标停在动画框时停止动画
            $("div").hover(function () {
              $("div").stop(true,false);
              $("div").text("当前动画框静止!")
            },function(){
                    if(!$("div").is(":animated")){    //判断元素是否正处于动画状态
                        $("div").text("这是动画栏!");
                        animate_test();     
                    }
            })      
        })
            function animate_test(){
              $("div").animate({
                left:"+=200",
                top:"+=200"
              },2000);
              $("div").animate({
                left:"+=200",
                top: "-=200"
              },2000);
              $("div").animate({
                left:"-=200",
                top: "-=200"
              },2000);
              $("div").animate({
                left:"-=200",
                top: "+=200"
              },2000);
         }
    
    
      })
    </script>
    
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题