Javascript 滚动高度脚本仅运行一次
这绝不是枯燥的代码,但我正在尝试运行一个脚本,该脚本在特定元素出现在视口中时计算数字。对于Javascript 滚动高度脚本仅运行一次,javascript,jquery,scroll,Javascript,Jquery,Scroll,这绝不是枯燥的代码,但我正在尝试运行一个脚本,该脚本在特定元素出现在视口中时计算数字。对于.counter-number产品,该特定元素的数字会累加,但对于.counter-number-2_产品,我似乎无法使其再次运行。代码如下: ///////////////////////////////////////// // Counting Up Numbers on Products Page// ///////////////////////////////////////// functi
.counter-number产品
,该特定元素的数字会累加,但对于.counter-number-2_产品
,我似乎无法使其再次运行。代码如下:
/////////////////////////////////////////
// Counting Up Numbers on Products Page//
/////////////////////////////////////////
function countUp(element) {
$(element).each( function() {
console.log('this' , this)
var $this = $( this ),
countTo = $this.attr( 'data-count' );
$( {
countNum: $this.text()
} ).animate( {
countNum: countTo
}, {
duration: 2000,
easing: 'linear',
step: function() {
$this.text( Math.floor( this.countNum ) );
},
complete: function() {
$this.text( this.countNum );
//alert('finished');
}
} );
} );
}//end of countUp()
////////////////////////////////////////////////////////////
// Checking when element is visible for counting animation//
////////////////////////////////////////////////////////////
function checkVisible( elm, eval ) {
console.log(elm)
eval = eval || "object visible";
var viewportHeight = $(window).height(), // Viewport Height
scrolltop = $(window).scrollTop(), // Scroll Top
y = $(elm).offset().top,
elementHeight = $(elm).height();
if (eval == "object visible") return ((y < (viewportHeight + scrolltop)) && (y > (scrolltop - elementHeight)));
if (eval == "above") return ((y < (viewportHeight + scrolltop)));
}
///////////////////////////////////////////////////////////////////////
// Running countUp() when first numbers on page are visible on scroll//
///////////////////////////////////////////////////////////////////////
$(window).on('scroll',function() {
if (checkVisible($('.counter-number_products'))) {
var element = $('.counter-number_products');
countUp(element);
$(window).off('scroll');
// console.log('first scroll is running')
} else {
// console.log('first scroll is not running')
}
});
$(window).on('scroll',function() {
if (checkVisible($('.counter-number-2_products'))) {
var element = $('.counter-number-2_products');
countUp(element);
$(window).off('scroll');
// console.log('second scroll is running')
} else {
// console.log('second scroll not running')
}
});
/////////////////////////////////////////
//在产品页面上计算数字//
/////////////////////////////////////////
函数倒计时(元素){
$(元素)。每个(函数(){
console.log('this',this)
变量$this=$(this),
countTo=$this.attr('data count');
$( {
countNum:$this.text()
})。制作动画({
countNum:countTo
}, {
期限:2000年,
“线性”,
步骤:函数(){
$this.text(Math.floor(this.countNum));
},
完成:函数(){
$this.text(this.countNum);
//警报(“完成”);
}
} );
} );
}//倒计时结束()
////////////////////////////////////////////////////////////
//检查元素何时可见以计数动画//
////////////////////////////////////////////////////////////
功能检查可见(elm、eval){
console.log(elm)
eval=eval | |“对象可见”;
var viewportHeight=$(窗口).height(),//视口高度
scrolltop=$(窗口).scrolltop(),//Scroll Top
y=$(elm).offset().top,
elementHeight=$(elm).height();
if(eval==“对象可见”)返回((y<(viewportHeight+scrolltop))&&(y>(scrolltop-elementHeight));
if(eval==“上方”)返回((y<(视口高度+滚动顶部));
}
///////////////////////////////////////////////////////////////////////
//当页面上的第一个数字在滚动条上可见时运行countUp()//
///////////////////////////////////////////////////////////////////////
$(窗口).on('scroll',function(){
如果(检查可见($('.计数器编号\产品')){
var元素=$('.counter-number_-products');
倒计时(元素);
$(窗口).off('scroll');
//console.log('第一个滚动正在运行')
}否则{
//console.log('第一个滚动未运行')
}
});
$(窗口).on('scroll',function(){
如果(检查可见($('.计数器编号-2_产品')){
var元素=$('.counter-number-2_-products');
倒计时(元素);
$(窗口).off('scroll');
//console.log('第二个滚动正在运行')
}否则{
//console.log('第二次滚动未运行')
}
});
你知道为什么countUp()不会运行两次吗
$(window).off('scroll')
禁用所有滚动处理程序,因此当在第一个函数中调用它时,它也会删除第二个滚动处理程序。就是这样-谢谢@CBroe!