我有一个简单的SVG,其中有一个元素围绕圆形路径旋转。当用户悬停时,通过更改 dur 属性可以加快旋转速度。问题是元素在发生时跳转到不同的位置。怎么解决这个问题?
$('g').hover(
function() {
$(this).find('animateMotion').attr('dur', '3s');
},
function() {
$(this).find('animateMotion').attr('dur', '7s');
}
);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<svg width='500' height='500'>
<g>
<path id='circle' d='M 250, 250 m -200, 0 a 200,200 0 1,0 400,0 a 200,200 0 1,0 -400,0' fill='none' stroke='red' stroke-width='10' />
<circle cx="50" cy="50" r="40" fill="red" transform='translate(-50, -50)'>
<animateMotion dur="7s" repeatCount="indefinite">
<mpath xlink:href="#circle" />
</animateMotion>
</circle>
</g>
</svg>
&#13;
答案 0 :(得分:3)
调整动画时间轴,使动画处于同一点。
var firstTime = true;
$('g').hover(
function() {
$(this).find('animateMotion').attr('dur', '3s');
if (firstTime) {
firstTime = false;
return;
}
document.getElementById("root").setCurrentTime(
document.getElementById("root").getCurrentTime() * 3 / 7);
},
function() {
$(this).find('animateMotion').attr('dur', '7s');
document.getElementById("root").setCurrentTime(
document.getElementById("root").getCurrentTime() * 7 / 3); }
);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<svg id="root" width='500' height='500'>
<g>
<path id='circle' d='M 250, 250 m -200, 0 a 200,200 0 1,0 400,0 a 200,200 0 1,0 -400,0' fill='none' stroke='red' stroke-width='10' />
<circle cx="50" cy="50" r="40" fill="red" transform='translate(-50, -50)'>
<animateMotion dur="3s" repeatCount="indefinite">
<mpath xlink:href="#circle" />
</animateMotion>
</circle>
</g>
</svg>
&#13;