2017-10-25 1 views
0

그래서 현재 SVG 아이콘에 애니메이션을 적용하려고합니다. 나는 선과 같은 것들로 그 주위에 몇 줄의 마음과 마음을 가지고있다. 기본적으로 마음에 윤곽선을 지정하고 클릭 할 때 배경색을 변경하고 싶습니다 (나는 작동합니다).애니메이션 하트 아이콘

내가 고심하고있는 부분은 클릭 할 때 마음 주위의 선들 만 보여줍니다. 나는 이것이 단순하다는 것을 알고 있지만, 나는 그저 내 머리를 감싸고있는 것처럼 보일 수 없다. 내 CSS에서는 기본적으로 .lines{display: none}을 설정하고 클래스 .lines-show{fill: red}을 만들었고 하트가 클릭되면 표시되도록합니다. 그런 다음 setTimeout() 함수를 사용하여 X 초 후에 행을 숨길 것입니다. 이 여기 https://codepen.io/Brushel/pen/xXvqgK?editors=1111을하는 데 도움이 코드의 경우 여기에 내 codepen에 대한 링크는 내가 지금까지 가지고 있습니다 :

const lines = document.querySelectorAll(".lines"); 
 
const heart = document.querySelector('.heart'); 
 

 
heart.addEventListener('click', function(){ 
 
    heart.classList.toggle("heart-fill-up"); 
 
});
svg { 
 
    max-height: 100px; 
 
} 
 

 
.heart { 
 
    fill: transparent; 
 
    transition: all .5s; 
 
} 
 

 
.heart-fill-up { 
 
    fill: red; 
 
} 
 

 
.lines { 
 
    display: none; 
 
} 
 

 
.lines-show { 
 
    fill: red; 
 
}
<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" 
 
\t viewBox="0 0 298 281" style="enable-background:new 0 0 298 281;" xml:space="preserve"> 
 
<style type="text/css"> 
 
\t .heart{ 
 
    stroke:#FF0606; 
 
    stroke-miterlimit:10; 
 
    stroke-width: 5px; 
 
    } 
 
</style> 
 
<title>icon_wishlist</title> 
 
<g id="icon_wishlist"> 
 
\t <path class="heart" d="M66.3,69.4c-19.1,20-19.1,51.5,0,71.5l84.6,87.9l84.7-87.8c19.1-20,19.1-51.5,0-71.5 
 
\t \t c-18-19.1-48.1-19.9-67.2-1.8c-0.6,0.6-1.2,1.2-1.8,1.8L151,85.7l-15.6-16.2c-18-19.1-48.1-19.9-67.2-1.8 
 
\t \t C67.6,68.2,67,68.8,66.3,69.4L66.3,69.4z"/> 
 
</g> 
 
<path class="lines" d="M58.1,60.6L9.4,22.1c-1.5-1.2-1.8-3.4-0.6-4.9l0,0c1.2-1.5,3.4-1.8,4.9-0.6l48.6,38.5c1.5,1.2,1.8,3.4,0.6,4.9 
 
\t l0,0C61.8,61.5,59.6,61.8,58.1,60.6z"/> 
 
<path class="lines" d="M265.4,237l-48.6-38.5c-1.5-1.2-1.8-3.4-0.6-4.9v0c1.2-1.5,3.4-1.8,4.9-0.6l48.6,38.5c1.5,1.2,1.8,3.4,0.6,4.9 
 
\t l0,0C269.1,237.9,266.9,238.2,265.4,237z"/> 
 
<path class="lines" d="M40.1,230.6L88,191.3c1.5-1.2,3.7-1,4.9,0.5v0c1.2,1.5,1,3.7-0.5,4.9l-47.9,39.3c-1.5,1.2-3.7,1-4.9-0.5l0,0 
 
\t C38.4,234.1,38.6,231.9,40.1,230.6z"/> 
 
<path class="lines" d="M236.7,54.8l46-41.6c1.4-1.3,3.7-1.2,4.9,0.2v0c1.3,1.4,1.2,3.7-0.2,4.9l-46,41.6c-1.4,1.3-3.7,1.2-4.9-0.2v0 
 
\t C235.2,58.3,235.3,56.1,236.7,54.8z"/> 
 
<path class="lines" d="M16.9,139.2h34c1.9,0,3.5,1.6,3.5,3.5v0c0,1.9-1.6,3.5-3.5,3.5h-34c-1.9,0-3.5-1.6-3.5-3.5v0 
 
\t C13.4,140.7,15,139.2,16.9,139.2z"/> 
 
<path class="lines" d="M249.8,139.2h34c1.9,0,3.5,1.6,3.5,3.5v0c0,1.9-1.6,3.5-3.5,3.5h-34c-1.9,0-3.5-1.6-3.5-3.5v0 
 
\t C246.3,140.7,247.8,139.2,249.8,139.2z"/> 
 
<path class="lines" d="M154.5,237.3v34c0,1.9-1.6,3.5-3.5,3.5l0,0c-1.9,0-3.5-1.6-3.5-3.5v-34c0-1.9,1.6-3.5,3.5-3.5l0,0 
 
\t C152.9,233.8,154.5,235.4,154.5,237.3z"/> 
 
<path class="lines" d="M154.5,20.3v34c0,1.9-1.6,3.5-3.5,3.5l0,0c-1.9,0-3.5-1.6-3.5-3.5v-34c0-1.9,1.6-3.5,3.5-3.5l0,0 
 
\t C152.9,16.8,154.5,18.4,154.5,20.3z"/> 
 
</svg>

click

enter image description here

답변

1

로 당신의 심장에 적용 할 수 있습니다 둘 이상의 요소 "선"을 개별적으로 정렬해야합니다 (각 루프를 사용하여 for 루프를 사용하여 클래스를 토글 할 때마다). 또한 setTimeOut을 추가하여 1000 밀리 초 후에 제거했습니다.

http://jsfiddle.net/jnqzoq1g/

const lines = document.querySelectorAll(".lines"); 
const heart = document.querySelector('.heart'); 

heart.addEventListener('click', function(){ 
    heart.classList.toggle("heart-fill-up"); 
    for (var x = 0; x < lines.length; x++){ 
     lines[x].classList.toggle("lines-show"); 
    } 
    setTimeout(function(){ 
     for (var x = 0; x < lines.length; x++){ 
     lines[x].classList.toggle("lines-show"); 
     } 
    }, 1000); 
}); 

.lines { 
    fill: transparent; 
    transition: all .5s; 
} 

.lines-show { 
    fill: red; 
+0

고맙습니다. 나는 어떻게해서든지 다른 경로를 얻을 루프를 만들어야한다고 생각했습니다. 이것은 많은 도움이 !! – ThomasBrushel

+0

당신은 이것을 지나치게 복잡하게했습니다. 하트와 모든 라인을 개별적으로 토글 할 필요가 없습니다. 그냥 클래스를''자체로 토글하고 그에 따라 모든 아이들을 스타일링하십시오. [이처럼] (http://jsfiddle.net/jnqzoq1g/1/) –

0

편집에 내 원하는 결과입니다 원하는 결과를 얻으려면 :

ov document.getElementsByClassName("lines");으로 그들 모두를 얻고 당신이 배열로 당신이

Array.from(document.getElementsByClassName("lines")).forEach(line => { 
    line.style.display = "block"; 
}); 

을 할 수있는 변환하면 당신은 setInterval을에 토글 포장에 의해 반복적으로 토글을 가질 수에 의해 배열의 모든 라인 어. 당신은 자바 스크립트를 사용하지 않을 경우

let interval = setInterval(() => { 
    heart.classList.toggle("heart-fill-up"); 
}, 1000); 

, 당신은

@keyframes heart { 
    0% { 
    fill: transparent; 
    } 
    100% { 
    fill: red; 
    } 
} 

를 반복하는 CSS 애니메이션 세트를 사용하고 당신이 가지고 있기 때문에

animation: heart 1s infinite 
+0

난 그냥 내 게시물을 편집했다. 내 원하는 결과 그림을 볼 수 있습니다. 나는 맥박이 있기를 기대하고 있지는 않지만 기본적으로 클릭 할 때 심장 주위의 선들을 보여줄 것입니다. – ThomasBrushel

관련 문제