javascript - 将 SVG 转换为 jQuery 函数

标签 javascript jquery svg

我有一个 svg

<svg class="svg_el" viewbox="0 0 100 100" preserveaspectratio="none">
  <path class="overlay_path">
    <animate attributeName="d" values="M 0 0 V 0 C 50 0 50 0 100 0 V 0 H 0;                         M 0 25 V 25 C 50 15 50 60 100 50 V 0 H 0;                         M 0 50 V 50 C 50 50 50 85 100 80 V 0 H 0;                         M 0 100 V 100 C 50 100 50 100 100 100 V 0 H 0" dur="0.4s" fill="freeze" repeatCount="1"></animate>
    <animate attributeName="d" values="M 0 0 C 50 0 50 0 100 0 V 100 H 0;                         M 0 25 C 50 15 50 60 100 50 V 100 H 0;                         M 0 50 C 50 50 50 85 100 80 V 100 H 0;                         M 0 100 C 50 100 50 100 100 100 V 100 H 0" dur="0.4s" begin="0.73s" fill="freeze" repeatCount="1"></animate>
  </path>
  <path class="overlay_path">
    <animate attributeName="d" values="M 0 0 V 0 C 50 0 50 0 100 0 V 0 H 0;                         M 0 25 V 25 C 50 15 50 60 100 50 V 0 H 0;                         M 0 50 V 50 C 50 50 50 85 100 80 V 0 H 0;                         M 0 100 V 100 C 50 100 50 100 100 100 V 0 H 0" dur="0.4s" begin="0.1s" fill="freeze" repeatCount="1"></animate>
    <animate attributeName="d" values="M 0 0 C 50 0 50 0 100 0 V 100 H 0;                         M 0 25 C 50 15 50 60 100 50 V 100 H 0;                         M 0 50 C 50 50 50 85 100 80 V 100 H 0;                         M 0 100 C 50 100 50 100 100 100 V 100 H 0" dur="0.4s" begin="0.63s" fill="freeze" repeatCount="1"></animate>
  </path>
  <path class="overlay_path">
    <animate attributeName="d" values="M 0 0 V 0 C 50 0 50 0 100 0 V 0 H 0;                         M 0 25 V 25 C 50 15 50 60 100 50 V 0 H 0;                         M 0 50 V 50 C 50 50 50 85 100 80 V 0 H 0;                         M 0 100 V 100 C 50 100 50 100 100 100 V 0 H 0" dur="0.4s" begin="0.2s" fill="freeze" repeatCount="1"></animate>
    <animate attributeName="d" values="M 0 0 C 50 0 50 0 100 0 V 100 H 0;                         M 0 25 C 50 15 50 60 100 50 V 100 H 0;                         M 0 50 C 50 50 50 85 100 80 V 100 H 0;                         M 0 100 C 50 100 50 100 100 100 V 100 H 0" dur="0.4s" begin="0.53s" fill="freeze" repeatCount="1"></animate>
  </path>
</svg>

我试图将它用作页面转换,以便它在下一页加载之前覆盖整个屏幕,但我遇到的问题是即使使用 SVGSVGELEMENT.getCurrentTime(),我似乎也无法暂停它在正确的位置,因此 svg 将在不同的点暂停。

  $('.the_box').removeClass('loaded');
  $('.ccs').load('/wordpress/wp-content/themes/Tsunami-Waves-PHP/img/waves.svg', function() {
    var svgDoc = $('.ccs svg');
    var animWatch = setInterval(function() {
      if (svgDoc[0].getCurrentTime() > 0.56 && !($('.the_box').hasClass('loaded'))) {
        svgDoc[0].pauseAnimations();
        console.log(svgDoc[0].getCurrentTime());
      } else if (svgDoc[0].getCurrentTime() > 0.56 && $('.the_box').hasClass('loaded')) {
        svgDoc[0].unpauseAnimations();
        $('.the_box').siblings('.slider-transition').html($('.the_box').html());
        $('.slider-transition').children('.slider-transition').unwrap();
        $('video').trigger('play');
        clearInterval(animWatch);
      }
    }, 10);
  });
  // $('#holder').load(function(){ var imgcount = $('#holder img').length; $('#holder img').load(function(){ imgcount--; if (imgcount == 0) { /* now they're all loaded, let's display them! */ } }); });
  $('.the_box').load(href + ' .slider-transition', function() {
    var svgDoc = $('.ccs svg');
    $(this).addClass('loaded');
    $('.woocommerce-product-gallery').each(function() {
      $(this).wc_product_gallery();
    });
    slideShowInit();
    initParalax();
  });

即使间隔为 10(甚至 1),暂停也会在完全不同的时间发生并且似乎无法在正确的时刻捕捉到它所以我认为我最好的选择是将 svg 转换为 jQuery 这样我可以更好地控制它,有没有一种简单的方法可以做到这一点,还是我必须学习如何去做?

最佳答案

如果我理解正确你想要实现什么,每个路径的第一个动画应该立即运行,第二个只有在新页面加载时才运行。如果是这种情况,您可以明确地执行此操作。

为最早的第二个动画设置一个id="reveal"begin="indefinite",并以$('#reveal ')[0].beginElementAt()。然后可以使用相对开始时间开始其他两个动画:begin="reveal.begin+0.1s"

<svg class="svg_el" viewbox="0 0 100 100" preserveaspectratio="none">
  <path class="overlay_path">
    <animate attributeName="d" values="..." dur="0.4s" fill="freeze"></animate>
    <animate attributeName="d" values="...." dur="0.4s" begin="reveal.begin+0.2s" fill="freeze"></animate>
  </path>
  <path class="overlay_path">
    <animate attributeName="d" values="..." dur="0.4s" begin="0.1s" fill="freeze"></animate>
    <animate attributeName="d" values="" dur="0.4s" begin="reveal.begin+0.1s" fill="freeze"></animate>
  </path>
  <path class="overlay_path">
    <animate attributeName="d" values="" dur="0.4s" begin="0.2s" fill="freeze"></animate>
    <animate id="reveal" attributeName="d" values="..." dur="0.4s" begin="indefinite" fill="freeze"></animate>
  </path>
</svg>

对于第二组动画的开始时间,您现在需要等待加载事件。如果第一组动画还在运行,你可以延迟第二组的开始时间。 beginEvent 触发其他操作。

  $('.the_box').removeClass('loaded');
  var svgLoad = $.Deferred(), sliderLoad = $.Deferred();
  // first animations start immediatly after svg load
  $('.ccs').load('/wordpress/wp-content/themes/Tsunami-Waves-PHP/img/waves.svg', svgLoad.resolve);
  $('.the_box').load(href + ' .slider-transition', sliderLoad.resolve);
  // wait for both load events
  $.when(svgLoad, sliderLoad).then(function() {
    var svgDoc = $('.ccs svg');
    // delay start time of second animations if load is earlier than 0.53s
    var startTime = Math.max(0.53, svgDoc[0].getCurrentTime());
    var reveal = $('#reveal');
    // link DOM change and video play to animation beginEvent
    reveal.on('beginEvent', function () {
        $('.the_box').siblings('.slider-transition').html($('.the_box').html());
        $('.slider-transition').children('.slider-transition').unwrap();
        $('video').trigger('play');
    });
    reveal[0].beginElementAt(startTime);
    $(this).addClass('loaded');
    $('.woocommerce-product-gallery').each(function() {
      $(this).wc_product_gallery();
    });
    slideShowInit();
    initParalax();
  });

关于javascript - 将 SVG 转换为 jQuery 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51070204/

相关文章:

jquery - 使用 Spring MVC 进行 Ajax 获取请求

javascript - 转换数据表列日期格式

javascript - JSONP 没有收到任何回调?

javascript - 为什么 JavaScript 传播符号在这里不起作用

javascript - jQuery 获取跨度的文本内容

javascript - 从 Angular $mdDialog 中访问表单

svg - Inkscape:持久对齐

javascript - 检测鼠标悬停在哪个元素上并传递给函数

javascript - 暂停 JS 打字机

javascript - 如何自动更新html内容?