gpt4 book ai didi

jquery - 如何让我的进度条平滑变色

转载 作者:行者123 更新时间:2023-11-28 02:35:44 24 4
gpt4 key购买 nike

我有以下功能,

<script>
function fadetoComplete() {
$("#progressbar-1").css({
'background-color': 'green'
}).text("complete");
}
</script>

下面的函数控制进度条如何增加。

<script>
var progress = 0;
var total_questions = 5;
//need this to check if the question has not been answered before
var questions = {
"q1": 0,
"q2":0,
"q3":0,
"q4":0,
"q5":0
}
$( function() {
$("#progressbar-1").text(progress)
$("input, select").change( function() {
el_name = $(this).attr("name");


switch (this.nodeName.toLowerCase()) {
case "select":
field =$("[name='"+el_name+"']");
val = (field.val() === "" || !field.val()) ? null: field.val();
break;
case "input":
field =$("[name='"+el_name+"']:checked");
val = field.length;
break;
}

if (val) {

if (!questions[el_name]) {
progress = progress +1;
questions[el_name]=1
}

} else {


questions[el_name]=0
progress = (progress > 0)?progress-1:0;


}
var result = progress / total_questions;
result = result * 100;

$("#progressbar-1").css({'width':result+'%','background-color':'red'});
$("#progressbar-1").text(progress);

if(result == 100){

fadetoComplete();
}
})
})
</script>

我问卷中的一些 html。

    <div class="container-main bg-5">
<form id="quiz">
<div class="questions">
<!--Question 5 -->
<h2>Hardware or Software?</h2>
<input type="radio" name="radio2" value="c1">
Hardware<br />
<input type="radio" name="radio2" value="c2">
Software<br />
</div>

<button type='button' id="submit" onclick="answers()">Submit Your Answers</button>

<button type="reset" id="reset" onclick="resetAnswer()">Reset</button>

</form>

<div id ="progressbar-1"></div>


</div>

当进度条达到 100% 时,这会将我的进度条更改为绿色。显然,虽然它只是跳到下一个颜色。我试过使用 CSS 过渡,但没有效果。理想情况下,我希望在进度条达到 100% 时调用该函数,并让该函数缓慢更改进度条颜色。

最佳答案

这是我的想法:

var w = 0;
$('button').on('click',function(){

w +=25;
$("#progressbar-1").css('width', w+'%');


if(w >= 100){
w = 100;
$("#progressbar-1").css({'width':'100%','background':'green'});

}

});
#progressbar-1{ 
display:block;
width:0%;
height:5px;
background-color:red;
transition: 2s;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<div id="progressbar-1"></div>
<br>
<button>Add +</button>

关于jquery - 如何让我的进度条平滑变色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47424856/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com