【发布时间】:2015-06-28 00:01:55
【问题描述】:
我有一个灰色虚线的 SVG。我想要做的是将它覆盖在绿色 SVG 虚线的顶部,然后将灰色动画化以显示绿色。有点像从右到左移动的米。
我看到了这个如何制作虚线的例子:
并且能够做到,但我的线已经虚线了。
我最终这样做了:
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 666.9 123.8" enable-background="new 0 0 666.9 123.8" xml:space="preserve">
<path opacity="0.4" stroke-width="3" fill="none" stroke="#66CD00" stroke-linecap="round" stroke-miterlimit="5" stroke-dasharray="1,6" d="
M656.2,118.5c0,0-320.4-251-645.9-0.7" />
<path id="top" opacity="0.4" fill="none" stroke="#AEAEAE" stroke-linecap="round" stroke-miterlimit="5" stroke-dasharray="1,6" d="
M656.2,118.5c0,0-320.4-251-645.9-0.7"/>
</svg>
var path = document.querySelector('#top');
var length = path.getTotalLength();
// Clear any previous transition
path.style.transition = path.style.WebkitTransition =
'none';
// Set up the starting positions
path.style.strokeDasharray = 1 + ' ' + 6;
path.style.strokeDashoffset = length;
// Trigger a layout so styles are calculated & the browser
// picks up the starting position before animating
path.getBoundingClientRect();
// Define our transition
path.style.transition = path.style.WebkitTransition =
'stroke-dashoffset 20s linear';
// Go!
path.style.strokeDashoffset = '0';
https://jsfiddle.net/ps5yLyab/
如何叠加两条虚线并为灰色设置动画?
【问题讨论】:
-
很多来自教程。我目前正试图通过动画灰线来解决这个问题。
-
你不是很清楚你想要达到什么。你是说你想要一条从右向左收缩的动画灰色虚线,显示一条动画绿色虚线?
-
是的,我希望灰色虚线消失并显示绿色虚线。
标签: javascript html svg svg-animate