Create progress bar with jQuery hover ()

1

I need to create a progress bar while my cursor is over an image. If the cursor exits, then I need the progress to be reset to repeat the process if the cursor returns to the top of the image. When I get to 100% I'm going to open a modal.

Any ideas?

    
asked by anonymous 01.12.2017 / 19:56

1 answer

1

You can use the hover() function to get the event of when the mouse pointer enters / exits the element:

/ p>

$(function(){
  
  var interval,
      progress = $('progress'),
      INTERVAL_DELAY = 50;
  
  
  // "mouse in"
  progress.hover(function(){
  
    interval = setInterval(function(){
      progress.val(progress.val()+1);
      
      if(progress.val() === 100){
        console.log('Modal!');
        clearInterval(interval);
      } 
      
    }, INTERVAL_DELAY);
    
  // "mouse out"  
  }, function(){
  
    clearInterval(interval);
    progress.val(0);
    
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<progress min='0' max='100' value='0'>
    
01.12.2017 / 20:08