【发布时间】:2018-05-10 08:01:26
【问题描述】:
当我将鼠标悬停在某个元素上时,我正在尝试为该元素周围的 SVG 路径设置动画。
我用这样的 CSS 编写动画:
#circle-1 {
fill-opacity: 0;
fill: transparent;
stroke: #000;
stroke-width: 1;
stroke-dasharray: 163px;
stroke-dashoffset: 163px;
animation-name: circle;
animation-duration: 4000ms;
animation-iteration-count: 1;
animation-play-state: paused;
}
@keyframes circle {
to {
stroke-dashoffset: 0;
}
}
效果很好。当我尝试像这样在兄弟元素悬停时触发动画时,它会停止工作:
HTML
<h1 id="link-1">#1</h1>
<svg width="57px" height="46px" viewBox="0 0 57 46" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<g id="circle-1">
<path d="M1252.39102,182.674299 C1222.56333,182.674299 1186.23852,210.300373 1230.21581,223.998154 C1278.92632,239.170208 1268.93364,185.408886 1243.06946,182.674299" id="Path-3"></path>
</g>
</svg>
CSS
#circle-1 {
fill-opacity: 0;
fill: transparent;
stroke: #000;
stroke-width: 1;
stroke-dasharray: 163px;
stroke-dashoffset: 163px;
animation-name: circle;
animation-duration: 4000ms;
animation-iteration-count: 1;
animation-play-state: paused;
}
#circle-1.active {
animation-play-state: running;
}
@keyframes circle {
to {
stroke-dashoffset: 0;
}
}
JS
window.onload = function() {
var circle1 = document.getElementById("circle-1");
var link1 = document.getElementById("link-1");
for (var i = 0; i < circle1.length; i++) {
link1[i].addEventListener('mouseover', function() {
circle1.classList.add('active');
return false;
});
}
};
在此处查看代码: https://codepen.io/louden/pen/YLeBXB
我不想使用 jQuery。
【问题讨论】:
-
点击不是悬停,对吧?
-
为什么还要为 getElementById 使用 for 循环?
-
@TemaniAfif
mouseover而不是click,我会修复它。我不是很擅长 JS 这就是为什么哈哈 -
你的 SVG 有问题,路径中使用的值很大并且超出了视图框......形状是什么?
-
我添加了原始代码,现在应该可以使用了
标签: javascript html css svg