我有一个计数器javascript:
function animateSun() {
var $elie = $("#sun");
$({
degree: 0
}).animate({
degree: 360
}, {
duration: 190999,
easing: 'linear',
step: function(val) {
now = Math.round(val);
if ((now == 5) || (now == 10) || (now == 15)) //increament the value on every 5 value
{
var i = 0;
$('#tam').text(i += 1);
}
$elie.css({
transform: 'rotate(' + now + 'deg)'
});
$('#demo').text('sec:' + now);
},
});
}
animateSun();

#sun {
position: absolute;
width: 55px;
height: 55px;
border: 1px solid red;
background-color: orange;
opacity: 0.9;
border-radius: 100%;
text-align: center;
top: 50%;
left: 50%;
animation: round 3s infinite linear;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<p id="demo"></p>
<p id="tam">0</p>
<p id="sun">sun</p>
&#13;
计时器正在运行。在5
上添加1
的时间值达到#tam
,然后下一次到达10
#tam
添加1
,将值更改为2。但它不起作用。请帮我。如何#tam
上每5个值的变化#demo
的值增加。提前谢谢。
答案 0 :(得分:2)
您可以尝试以下操作。将Math.floor()
应用于step/5
。
function animateSun() {
var $elie = $("#sun");
$({
degree: 0
}).animate({
degree: 360
}, {
duration: 190999,
easing: 'linear',
step: function(val) {
now = Math.round(val);
$('#tam').text(Math.floor(val/5));
$elie.css({
transform: 'rotate(' + now + 'deg)'
});
$('#demo').text('sec:' + now);
},
});
}
animateSun();
&#13;
#sun {
position: absolute;
width: 55px;
height: 55px;
border: 1px solid red;
background-color: orange;
opacity: 0.9;
border-radius: 100%;
text-align: center;
top: 50%;
left: 50%;
animation: round 3s infinite linear;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<p id="demo"></p>
<p id="tam">0</p>
<p id="sun">sun</p>
&#13;
答案 1 :(得分:0)
正如Rejith建议的那样,将计数器值保存在全局变量中。目前,您每次将计数器设置为0然后递增,因此它始终为1。
var counter = 0;
function animateSun() {
var $elie = $("#sun");
$({
degree: 0
}).animate({
degree: 360
}, {
duration: 190999,
easing: 'linear',
step: function(val) {
now = Math.round(val);
// if ((now == 5) || (now == 10) || (now == 15)) //increment the value on every 5 value
if( now % 5 == 0 )
{
$('#tam').text(counter++);
}
$elie.css({
transform: 'rotate(' + now + 'deg)'
});
$('#demo').text('sec:' + now);
},
});
}
animateSun();