Javascript 如何在滚动中只制作一次动画

Javascript 如何在滚动中只制作一次动画,javascript,jquery,html,css,Javascript,Jquery,Html,Css,这是我的代码: const ratingcount=document.queryselectoral('.ratingcount'); const totalratingcounter=ratingcount.length; var stopNow=总计计数器 countEach() $(窗口).on('scroll',函数(e){ countEach() }) 函数countEach(){ $('.ratingcount')。每个(函数(){ if(showOnScreen(this)&&&

这是我的代码:

const ratingcount=document.queryselectoral('.ratingcount');
const totalratingcounter=ratingcount.length;
var stopNow=总计计数器
countEach()
$(窗口).on('scroll',函数(e){
countEach()
})
函数countEach(){
$('.ratingcount')。每个(函数(){
if(showOnScreen(this)&&&$(this).attr('show')!='false'&&stopNow!=0){
console.log($(this.text())
console.log($(this.attr('show'))
$(this.attr('show','false'))
号码(本)
stopNow=stopNow-1;
}如果(!屏幕上显示(此)){
$(this.attr('show','true'))
}
})
}
屏幕上的功能显示(目标){
如果($(窗口).scrollTop()+$(窗口).height()>=$(目标).offset().top)
返回true;
其他的
返回false;
}
函数编号RANIMATE(目标){
变量$this=$(目标);
jQuery({
柜台:0
}).制作动画({
计数器:$this.text()
}, {
持续时间:1000,
放松:"摇摆",,
步骤:函数(){
$this.text(this.Counter.toFixed(1));
}
});
}

按照我的步骤,向下滚动到中间
5.6
5.6










5.6
5.6 现在再次向上滚动,然后向下滚动










现在看,这是行不通的 5.6

5.6
好吧,如果我没听错的话-您希望在屏幕上第一次显示时,每个评级计数器都被设置一次动画。要实现这一点,您只需从函数
countEach()
中删除
else if(){…}
,如下所示:

const ratingcount=document.queryselectoral('.ratingcount');
const totalratingcounter=ratingcount.length;
var stopNow=总计计数器
countEach()
$(窗口).on('scroll',函数(e){
countEach()
})
函数countEach(){
$('.ratingcount')。每个(函数(){
if(showOnScreen(this)&&&$(this).attr('show')!='false'&&stopNow!=0){
//console.log($(this.text())
//console.log($(this.attr('show'))
$(this.attr('show','false'))
号码(本)
stopNow=stopNow-1;
}/*如果(!屏幕上显示(此)){
$(this.attr('show','true'))
}*/
})
}
屏幕上的功能显示(目标){
如果($(窗口).scrollTop()+$(窗口).height()>=$(目标).offset().top)
返回true;
其他的
返回false;
}
函数编号RANIMATE(目标){
变量$this=$(目标);
jQuery({
柜台:0
}).制作动画({
计数器:$this.text()
}, {
持续时间:1000,
放松:"摇摆",,
步骤:函数(){
$this.text(this.Counter.toFixed(1));
}
});
}

按照我的步骤,向下滚动到中间
5.6
5.6










5.6
5.6 现在再次向上滚动,然后向下滚动










现在看,这是行不通的 5.6

5.6
是否希望它们中的每一个只制作一次动画?是@ItayGano,只制作一次+仅滚动(如果屏幕上未显示)