Execution of jQuery Script

0

I have a progress bar script, I would like the progress bar fill animation to start as soon as the user scrolls the page to the animated elements. I tried to use $('.progress-bar').scroll(function() { etc... , but every time I use the mouse scroll, it restarts script execution. I would like the script to run only once.

$('.skill-progress').each(function() {
  $('.html-css.circle').circleProgress({
    value: 0.87,
    size: 160,
    startAngle: -1.5,
    fill: {
      gradient: [['#EC6630', .5], ['#0096E6', .5]], gradientAngle: Math.PI / 5
    }
  }).on('circle-animation-progress', function(event, progress) {
    $(this).find('strong').html('<i><span style="color: #EC6630;">HTML</span> / <span style="color: #0096E6;">CSS</span></i>');
  });
  $('.jq-js.circle').circleProgress({
    value: 0.63,
    size: 160,
    startAngle: -1.5,
    fill: {
      gradient: [['#1169AE', .5], ['#EEAF4B', .5]], gradientAngle: Math.PI / 1.8
    }
  }).on('circle-animation-progress', function(event, progress) {
    $(this).find('strong').html('<i><span style="color: #1169AE;">JQ</span> / <span style="color: #EEAF4B;">JS</span></i>');
  });
  $('.php-mysql.circle').circleProgress({
    value: 0.47,
    size: 160,
    startAngle: -1.5,
    fill: {
      gradient: [['#8993BE', .5], ['#F88500', .5]], gradientAngle: Math.PI / 1.5
    }
  }).on('circle-animation-progress', function(event, progress) {
    $(this).find('strong').html('<i><span style="color: #8993BE;">PHP</span> / <span style="color: #F88500;">MYSQL</span></i>');
  });
  $('.ps-cd.circle').circleProgress({
    value: 0.53,
    size: 160,
    startAngle: -1.5,
    fill: {
      gradient: [['#5889C4', .5], ['#55A746', .5]], gradientAngle: Math.PI / 2
    }
  }).on('circle-animation-progress', function(event, progress) {
    $(this).find('strong').html('<i><span style="color: #5889C4;">PS</span> / <span style="color: #55A746;">CDR</span></i>');
  });
});
.circle {
  margin: 6px 6px 20px;
  display: inline-block;
  position: relative;
  text-align: center;
  line-height: 1.2;
}
.circle strong {
  position: absolute;
  top: 51px;
  left: 0;
  width: 100%;
  text-align: center;
  line-height: 60px;
}
.circle strong i {
  font-style: normal;
  font-size: 1.4em;
  font-weight: 300;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><scriptsrc="https://rawgit.com/kottenator/jquery-circle-progress/1.2.1/dist/circle-progress.js"></script>
<div id="skills" class="container content center-align skill-progress">
  <div class="html-css circle">
    <strong></strong>
  </div>
  <div class="jq-js circle">
    <strong></strong>
  </div>
  <div class="php-mysql circle">
    <strong></strong>
  </div>
  <div class="ps-cd circle">
    <strong></strong>
  </div>
</div>
    
asked by anonymous 14.01.2017 / 00:18

1 answer

4

I did not really understand the final goal of the question, there are two interpretations of what you need

1)

Would you like it to be a progress bar for page content?

If so: You can use window.addEventListener('scroll', function() { by taking the user's screen size ( var height = document.body.getBoundingClientRect().height - window.innerHeight; ) to the current screen position ( var top = window.scrollY; ) to recalculate the current percentage and reloading the $('.circle').circleProgress({ script with the percentage value.

Function adapted from this one: progress-bar-when-scroll

function updateProgress(num1, num2) {
  var percent = Math.ceil(num1 / num2 * 100);
  $('.circle').circleProgress({
    value: percent / 100,
    animation: false,
    fill: {color: '#0000ff'}
  })
}

window.addEventListener('scroll', function() {
  var top = window.scrollY;
  var height = document.body.getBoundingClientRect().height - window.innerHeight;
  updateProgress(top, height);
});
.circle {
  margin: 6px 6px 20px;
  display: inline-block;
  position: relative;
  text-align: center;
  line-height: 1.2;
}
.circle strong {
  position: absolute;
  top: 51px;
  left: 0;
  width: 100%;
  text-align: center;
  line-height: 60px;
}
.circle strong i {
  font-style: normal;
  font-size: 1.4em;
  font-weight: 300;
}
.fixo {
  position: fixed!important;
  top: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><scriptsrc="https://rawgit.com/kottenator/jquery-circle-progress/1.2.1/dist/circle-progress.js"></script>
<div id="skills" class="container content center-align skill-progress">
  <div class="fixo">
    <div class="circle">
      <strong></strong>
    </div>
  </div>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
</div>

2)

Would you like the animation to start as soon as you reach the element?

You can do something similar: At the start of the page call the $('.circle').circleProgress({ function with value: 0 , create a function that calculates the distance of the element taking into account the current screen view:

$(window).scroll(function() {
  var $circle = $(".circle");
  var window_offset = $circle.offset().top - $(window).scrollTop();
});

And within this function call another function that "high destructs" when it exceeds a certain stipulated limit:

var myFunction = function(window_offset) {
  if (window_offset < 100) {
    myFunction = function() {};

Within this stipulated limit, still call the $('.circle').circleProgress({ function again with the percentage value you would like.

$('.circle').circleProgress({
  value: 0,
  fill: {
    color: '#0000ff'
  }
})

var myFunction = function () {
    $(".circle").circleProgress({
      value: 0.85,
      fill: {
        color: '#0000ff'
      }
    });

    myFunction = function() {};
    return 1;
};

$(window).bind("scroll resize", function() {
  var $circle = $(".circle");
  var win = $(window);

  var isVibile = win.scrollTop() + win.height() > $circle.offset().top + $circle.outerHeight();

  $("#window_offset").html(isVibile ? "Visível" : "Invisível");

  if (isVibile) {
     myFunction($circle);
  }
});
.circle {
  margin: 6px 6px 20px;
  display: inline-block;
  position: relative;
  text-align: center;
  line-height: 1.2;
}
.circle strong {
  position: absolute;
  top: 51px;
  left: 0;
  width: 100%;
  text-align: center;
  line-height: 60px;
}
.circle strong i {
  font-style: normal;
  font-size: 1.4em;
  font-weight: 300;
}
.fixo {
  position: fixed!important;
  top: 0px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><scriptsrc="https://rawgit.com/kottenator/jquery-circle-progress/1.2.1/dist/circle-progress.js"></script>
<div id="skills" class="container content center-align skill-progress">
  <br/>
  <br/>
  <label class="fixo" id="window_offset"></label>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <div class="circle">
    <strong></strong>
  </div>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
  <br/>
</div>
    
14.01.2017 / 01:00