问题描述
我想切换外边小球转动速度,但实际效果在切换过程中并不会从当前位置切换速度,而是在环上的某个位置
自己尝试过的方法
我觉得是在切换速度时,原来的动画队列并没有停止,仍在运行,因此想用jq中stop()
方法清除动画队列,但动画是那种死循环的旋转,根本无法清除,并没有效果。
相关代码
// 请把代码文本粘贴到下方(请勿用图片代替代码)
<div class="contain">
<div class="cir">
</div>
<div class="bigcir">
<div class="shanye" id="shanye"></div>
</div>
<button id="slow">低速</button>
<button id="middle">中速</button>
<button id="high">高速</button>
</div>
@keyframes zhuan{
100%{
transform: rotate(360deg)
}
}
.shanye{
animation:zhuan 4s infinite linear forwards;
transform-origin:15px 115px;
border-radius: 50%;
width: 30px;
height: 30px;
position: absolute;
background:skyblue;
left: calc(50% - 15px);
top: -15px
}
var slow = document.getElementById('slow')
var middle = document.getElementById('middle')
var high = document.getElementById('high')
var shanye = document.getElementById('shanye')
slow.onmouseenter=function(){
$('#shanye').stop(true)
shanye.style.animation='zhuan 8s infinite linear';
}
middle.onmouseenter=function(){
$('#shanye').stop(true)
shanye.style.animation = 'zhuan 4s infinite linear'
}
high.onmouseenter=function(){
$('#shanye').stop(true)
shanye.style.animation = 'zhuan 2s infinite linear'
}
css动画和jquery动画基本上是两套东西,jquery的stop只能停止jquery的动画(至于为什么不能停你得了解js动画原理)
你的需求是没法用css动画实现的,控制不了那么细,得完全用js动画
查了下mdn,好像火狐上有些api可以控制css动画
以下代码仅火狐可用,且在about:config中开启dom.animations-api-getAnimations.enabled和dom.animations-api.timelines.enabled
随便找了个js动画库写了个js动画,这个应该大部分浏览器都能正常展示(研究了下jquey动画没有想到简单方法做这个需求)