2016-06-01 47 views
0

嗨所有我試圖設置每個進度條在引導4的限制。 我希望它觸發點擊。如何設置限制價值 - jquery

的問題是當過我點擊

值總是轉到100.我如何可以爲每一個進度條的最大值?

這是代碼。

<button>run</button> 
<progress class="progress progress-striped progress-animated limit70" value="" max="100"></progress> 
<progress class="progress progress-striped progress-animated limit80" value="" max="100"></progress> 

$('button').on('click', function() { 
    $('.progress').each(function() { 
     var progBar = $(this); 
     var perc = progBar.attr("max"); 
     var userInput = $('input#speed').val(); // in seconds 
     var speed = userInput * 10; 
     var currentPerc = 0; 
     var progress = setInterval(function() { 

      if (currentPerc >= perc) { 
       clearInterval(progress); 

      } else { 
       currentPerc += 1; 
       progBar.attr('value', (currentPerc) + ''); 
      } 
      progBar.attr((currentPerc) + ''); 
     }, speed); 

    }); 
}); 

這裏有一個fiddle

+0

該進度條插件,你用嗎? –

+0

沒有使用插件。我用Bootstrap 4進度條剛剛添加了jquery來觸發動畫 –

回答

2

你可以用自定義的數據屬性的工作:

$('button').on('click', function() { 
 
    $('.progress').each(function() { 
 
    var progBar = $(this); 
 
    var perc = progBar.attr("max"); 
 
    var userInput = $('input#speed').val(); // in seconds 
 
    var speed = userInput * 10; 
 
    var currentPerc = 0; 
 
    var limit = progBar.data("limit"); 
 
    var progress = setInterval(function() { 
 

 
     if (currentPerc >= limit) { 
 
     clearInterval(progress); 
 

 
     } else { 
 
     currentPerc += 1; 
 
     progBar.attr('value', (currentPerc) + ''); 
 
     } 
 
     progBar.attr((currentPerc) + ''); 
 
    }, speed); 
 

 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> 
 
<div class="user-controls"> 
 
    <button>Click to run</button> 
 
</div> 
 

 

 
<progress class="progress progress-striped progress-animated limit70" data-limit="70" value="" max="100"></progress> 
 
<br/> 
 
<progress class="progress progress-striped progress-animated limit80" data-limit="80" value="" max="100"></progress>

更新小提琴:https://jsfiddle.net/csmrtrvg/2/

+0

這就做到了! :D我非常關注價值屬性。是的數據限制ftw!謝謝哥們。 新手敬禮! :D –

+0

是的,我正在等待冷靜完成:D –