如何暂停鼠标悬停在图像上的动画

时间:2017-12-05 09:46:01

标签: javascript jquery

我需要帮助才能在鼠标悬停时停止动画并继续鼠标移出。请参阅下面的网址以查看代码。感谢

http://jsfiddle.net/AbdiasSoftware/F8x4p/

        $(window).load(function(){
        var pos = $('#center').position(),
        radiusSat = $('#sat1').width() * 0.5,
        radius = $('#center').width() * 0.5,
        cx = pos.left + radius,
        cy = pos.top + radius,
        x, y, angle = 0, 
        angles = [],
        spc = 360 / 25,
        deg2rad = Math.PI / 180,
        i = 0;

    for(;i < 25; i++) {
        angles.push(angle);
        angle += spc;
    }

    /// space out radius
    radius += (radiusSat + 25);

    loop();

    function loop() {
    for(var i = 0; i < angles.length; i++) {

        angle = angles[i];

        x = cx + radius * Math.cos(angle * deg2rad);
        y = cy + radius * Math.sin(angle * deg2rad);

        $('#sat' + i).css({left:x - radiusSat, top:y - radiusSat});


    angles[i] += 0.1;
        if (angles[i] > 360) angles[i] = 0;
    }

    requestAnimationFrame(loop);

}
});

2 个答案:

答案 0 :(得分:1)

您可以创建一个变量来跟踪鼠标是否在内部,并使用事件来更改变量。

首先,将标记包装在包含div中,以便可以轻松地将事件监听器应用于所有元素:

<div class="container">
  <div id="center"></div>
  <div id="sat0"></div>
  <div id="sat1"></div>
  <div id="sat2"></div>
  <div id="sat3"></div>
  <div id="sat4"></div>
</div>

然后添加一个名为mouseEntered的新变量,并将其设置为false

然后创建两个事件侦听器 - 一个用于在鼠标进入容器时将mouseEntered变量设置为true,另一个用于在鼠标离开时将其设置为false

document.querySelector('.container').addEventListener('mouseenter', function() {
    mouseEntered = true
})
document.querySelector('.container').addEventListener('mouseleave', function() {
    mouseEntered = false
})

最后,将loop函数中的所有内容(requestAnimationFrame除外)包含在if语句中,以检查mouseEnteredfalse

function loop() {
    if (mouseEntered === false) {
      for(var i = 0; i < angles.length; i++) {

          angle = angles[i];

          x = cx + radius * Math.cos(angle * deg2rad);
          y = cy + radius * Math.sin(angle * deg2rad);

          $('#sat' + i).css({left:x - radiusSat, top:y - radiusSat});

          angles[i] = angles[i] + 1;
          if (angles[i] > 360) angles[i] = 0;
      }
    }

    requestAnimationFrame(loop);
}

摆弄变化:http://jsfiddle.net/pbobbr5h/

答案 1 :(得分:0)

试试这个 -

&#13;
&#13;
var pos = $('#center').position(),
    radiusSat = $('#sat1').width() * 0.5,
    radius = $('#center').width() * 0.5,
    cx = pos.left + radius,
    cy = pos.top + radius,
    x, y, angle = 0, angles = [],
    spc = 360 / 5,
    deg2rad = Math.PI / 180,
    i = 0;

for(;i < 5; i++) {
    angles.push(angle);
    angle += spc;
}
stop();
/// space out radius
radius += (radiusSat + 10);
$("#center").mouseover(function(){
   start();
});
$("#center").mouseout(function(){
   stop();
});

var requestId;

function loop(time) {
    requestId = undefined;

   for(var i = 0; i < angles.length; i++) {

        angle = angles[i];
        
        x = cx + radius * Math.cos(angle * deg2rad);
        y = cy + radius * Math.sin(angle * deg2rad);

        $('#sat' + i).css({left:x - radiusSat, top:y - radiusSat});
    
        angles[i] = angles[i] + 1;
        if (angles[i] > 360) angles[i] = 0;
    }
    
    //requestAnimationFrame(loop);

    stop();
}

function stop() {
    if (!requestId) {
       requestId = window.requestAnimationFrame(loop);
    }
}

function start() {
    if (requestId) {
       window.cancelAnimationFrame(requestId);
       requestId = undefined;
    }
}
&#13;
div {
    border-radius:50%;
    border:2px solid #000;
    position:fixed;
}
#center {
    width:200px;
    height:200px;
    left:100px;
    top:100px;
    
}
#sat0, #sat1, #sat2, #sat3, #sat4 {
    width:50px;
    height:50px;
    
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div id="center"></div>
<div id="sat0"></div>
<div id="sat1"></div>
<div id="sat2"></div>
<div id="sat3"></div>
<div id="sat4"></div>
&#13;
&#13;
&#13;