簡體   English   中英

SVG路徑動畫從單擊按鈕開始

[英]SVG path animation to begin on click of a button

我有一個SVG路徑動畫,此動畫目前處於無限循環中。 我希望動畫在單擊事件觸發之前是不可見的,然后一旦動畫完成(一次,不是無限次),該按鈕將不再起作用。

我添加了一個測試按鈕,但加載頁面后該動畫似乎仍然可以播放,並且該按鈕對此沒有任何作用。

 $("#button").click(function() { $('.dashed').toggleClass('path'); }); 
 .dashed{ stroke-dasharray: 10; } .path { stroke-dasharray: 1000; stroke-dashoffset: 1000; animation: dash 5s linear infinite; } @keyframes dash { from { stroke-dashoffset: 1000; } to { stroke-dashoffset: 0; } } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <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" width="612px" height="792px" viewBox="0 0 612 792" enable-background="new 0 0 612 792" xml:space="preserve"> <path class="path" fill="none" stroke="#000000" stroke-linejoin="round" stroke-miterlimit="10" d="M23.742,10.709 c-2.305,23.611-8.81,46.563-9.021,70.829c-0.252,28.966,22.237,43.666,47.06,55.482c23.642,11.255,42.368,15.766,68.461,16.631 c19.993,0.663,40.08,2.97,59.853-1.723c23.301-5.531,45.542-17.598,66.978-27.933c19.248-9.281,38.831-21.86,41.946-45.201 c5.539-41.51-54.993-47.073-81.885-42.17C159.05,47.212,89.37,104.633,77.387,164.629c-5.896,29.522-4.312,60.884,12.703,86.354 c19.17,28.697,49.512,49.927,78.596,67.591"/> <path class="dashed" fill="none" stroke="white" stroke-width="4" stroke-linejoin="round" stroke-miterlimit="10" d="M23.742,10.709 c-2.305,23.611-8.81,46.563-9.021,70.829c-0.252,28.966,22.237,43.666,47.06,55.482c23.642,11.255,42.368,15.766,68.461,16.631 c19.993,0.663,40.08,2.97,59.853-1.723c23.301-5.531,45.542-17.598,66.978-27.933c19.248-9.281,38.831-21.86,41.946-45.201 c5.539-41.51-54.993-47.073-81.885-42.17C159.05,47.212,89.37,104.633,77.387,164.629c-5.896,29.522-4.312,60.884,12.703,86.354 c19.17,28.697,49.512,49.927,78.596,67.591"/> </svg> <input type="button" id="button" value="Animate" /> 

a)清空類.path但將其保留在該位置:

.path { }

b)將從.path刪除的動畫屬性添加到新的CSS類中,並將動畫屬性中的'infinite'替換為'1'。

.path-animation {
     stroke-dasharray: 1000;
     stroke-dashoffset: 1000;
     animation: dash 5s linear 1;
}

c)使用以下jquery來獲得所需的結果:

$("#button").click(function() {
   $('.path').attr('class', 'path path-animation');
   //5 secs delay to complete the animation before removing it and disabling the button.
   setTimeout(function() {
      $('.path').attr('class', 'path');
      $("#button").attr("disabled","disabled");
   }, 5000);
});

示例代碼: http : //codepen.io/Nasir_T/pen/yVNxQG

您需要對代碼進行以下小的更改:

1)首先從第一個<path>元素中刪除已經存在的<path class="path" 您希望按鈕稍后在點擊時添加

2)從animation: dash 5s linear infinite;更改動畫迭代計數 animation: dash 5s linear infinite; -這里的“無限”表示對動畫進行無限次迭代。 將其更改為animation: dash 5s linear 1;

.path {
  stroke-dasharray: 1000;
  stroke-dashoffset: 1000;
  animation: dash 5s linear 1;
}

3)然后在第一次單擊后禁用該按鈕,您可以

$('#button').on('click', function() {
  $('.dashed').addClass('path');
  $(this).prop('disabled', true);
});

簡單..讓我知道您是否遇到任何問題。

https://jsfiddle.net/krishna9960/atg5m6ym/7723/

檢查這個..應該會幫助...

對於禁用按鈕,您可以使用此代碼

$("#button").click(function() {
          $('.path').css("animation"," dash 5s linear");
          $(this).attr("disabled","disabled");
         });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM