简体   繁体   English

滚动时触发圆环图动画

[英]Trigger Donut Chart animation on scroll

I've created some Donut charts with a simple fill animation but I'm having trouble triggering the animation on scroll.我创建了一些带有简单填充动画的甜甜圈图表,但在滚动时无法触发动画。 Would it be a good idea to add in animate.css and wow.js or is there an easier way to trigger the animation inside of the function?添加 animate.css 和 wow.js 是个好主意还是有更简单的方法来触发函数内部的动画? Thanks in advance for the help if you are able to help with this issue.如果您能够帮助解决此问题,请提前感谢您的帮助。

<div class="donut-chart orange" data-percent="72">
  <p><span class="counter" data-count="72">0</span>%</p>
  <div class="doughnut"></div>
</div>
$(function() {
  $('.donut-chart').each(function(index) {
    $(this).append('<svg preserveAspectRatio="xMidYMid" xmlns:xlink="http://www.w3.org/1999/xlink" id="donutChartSVG' + index + '"><path d="M100,100"/></svg>');
    var p = new Donut_chart({
      element: $('#donutChartSVG' + index),
      percent: $(this).attr('data-percent')
    });
    p.animate();
  });
});

$('.counter').each(function() {
  var $this = $(this),
    countTo = $this.attr('data-count');

  $({
    countNum: $this.text()
  }).animate({
    countNum: countTo
  }, {
    duration: 2000,
    easing: 'linear',
    step: function() {
      $this.text(Math.floor(this.countNum));
    },
    complete: function() {
      $this.text(this.countNum);
    }
  });
});
.donut-chart svg {
  pointer-events: none;
  height: 100%;
  stroke: #19a8ff;
}

.donut-chart svg path {
  fill: none;
  stroke-width: 35px;
  stroke: #19a8ff;
}

.donut-chart {
  width: 200px;
  height: 200px;
  display: inline-block;
  left: 20%;
  margin-bottom: 30px;
}

.donut-chart p {
  margin: 0;
  position: absolute;
  left: 1%;
  top: 25%;
  font-family: 'Open Sans', sans-serif;
  font-weight: bolder;
  color: #222;
  width: 100%;
  text-align: center;
  font-size: 2em;
}

.doughnut {
  border: 35px solid #EAECEF;
  border-radius: 100px;
  height: 200px;
  width: 200px;
  position: absolute;
  top: 0px;
  z-index: -1;
}

try this, just check if the user scrolls then include inside if() your event function.试试这个,只需检查用户是否滚动,然后将if()包含在您的事件函数中。

$(window).scroll(function() {
    if ($('.donut-chart').is(':visible')) {
        // do your stuff here, include function with p.animate();
    }
});

but remember that this may slow down the performance of your site.但请记住,这可能会降低您网站的性能。 You may refer to this site for improvement, https://johnresig.com/blog/learning-from-twitter/ .您可以参考此站点进行改进, https://johnresig.com/blog/learning-from-twitter/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM