[英]How to pause the animation on mouse hover on image
Please I need help to stop the animation on mouse hover and to continue on mouse out. 请我需要帮助,以使鼠标悬停时停止动画并继续将鼠标移出。 Please see the url below to view the code.
请查看下面的网址以查看代码。 thanks
谢谢
http://jsfiddle.net/AbdiasSoftware/F8x4p/ 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);
}
});
You could create a variable to keep track on whether the mouse is inside, and use events to change the variable. 您可以创建一个变量来跟踪鼠标是否在其中,并使用事件来更改该变量。
Firstly, wrap your markup in a containing div so the event listener can easily be applied to all the elements: 首先,将您的标记包装在一个包含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>
Then add a new variable, called mouseEntered
, and set it to false
. 然后添加一个名为
mouseEntered
的新变量,并将其设置为false
。
Then create two event listeners - one to set the mouseEntered
variable to true
when the mouse enters the container, and one to set it to false
when the mouse leaves. 然后创建两个事件侦听器-一个在鼠标进入容器时将
mouseEntered
变量设置为true
,另一个在鼠标离开容器时将其设置为false
。
document.querySelector('.container').addEventListener('mouseenter', function() {
mouseEntered = true
})
document.querySelector('.container').addEventListener('mouseleave', function() {
mouseEntered = false
})
Finally, wrap everything in your loop
function, apart from requestAnimationFrame
, in an if
statement that checks if mouseEntered
is false
. 最后,将除了
requestAnimationFrame
之外的所有内容包装在loop
函数中,并通过if
语句检查mouseEntered
是否为false
。
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);
}
Fiddle with the changes: http://jsfiddle.net/pbobbr5h/ 摆弄这些变化: http : //jsfiddle.net/pbobbr5h/
try this- 尝试这个-
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; } }
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; }
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.