Javascript 如何在页面加载时将jQuery更改为Fire
好吧,这看起来应该很简单,但我搞不懂。我从另一个开发人员那里得到了这段代码,我的任务是在页面加载时更改为fire,而不是滚动。我已经尝试过改变我所能想到的一切,例如:changing.bind to.load和jQuerythis.bind'inview',functionevent,对$document.readyfunction可见,但没有任何效果。如何使这些数字仅在初始页面加载时才具有动画效果Javascript 如何在页面加载时将jQuery更改为Fire,javascript,jquery,function,jquery-animate,bind,Javascript,Jquery,Function,Jquery Animate,Bind,好吧,这看起来应该很简单,但我搞不懂。我从另一个开发人员那里得到了这段代码,我的任务是在页面加载时更改为fire,而不是滚动。我已经尝试过改变我所能想到的一切,例如:changing.bind to.load和jQuerythis.bind'inview',functionevent,对$document.readyfunction可见,但没有任何效果。如何使这些数字仅在初始页面加载时才具有动画效果 // Animate any number jQuery('.animateNu
// Animate any number
jQuery('.animateNumber').each(function(){
var value = new Number;
// Grab contents of element and turn it into a number
value = jQuery(this).text();
value = parseInt(value);
// Set the starting text to 0
jQuery(this).text('0');
// Animate to correct value
jQuery(this).bind('inview', function(event,visible) {
if(visible == true) {
jQuery(this).animateNumber(
{
number: value,
},
1000
)
}
});
});
更新
我已经将代码更改为以下内容,它在Firefox中运行良好,但在Chrome中没有动画。有人知道为什么会这样吗
// Animate any number
jQuery('.animateNumber').each(function(){
var value = new Number;
// Grab contents of element and turn it into a number
value = jQuery(this).text();
value = parseInt(value);
// Set the starting text to 0
jQuery(this).text('0');
// Animate to correct value
jQuery(this).animateNumber(
{
number: value
},
1000
);
});
只需将其全部包装在一个文档中即可
$(document).ready(function() {
//Your code
});
编辑:查看您的代码,如下所示:
jQuery(this).bind('inview', function(event,visible) {
将在每次您使元素可见时激发,因此当您向下滚动并备份时,该函数将再次运行。。。我建议删除它和下面的if语句,使您的代码看起来像这样:
// Animate any number
jQuery('.animateNumber').each(function(){
var value = new Number;
// Grab contents of element and turn it into a number
value = jQuery(this).text();
value = parseInt(value);
// Set the starting text to 0
jQuery(this).text('0');
// Animate to correct value
jQuery(this).animateNumber(
{
number: value,
},
1000
);
如果它破坏了其他内容,您还可以创建一个全局变量,以防止它执行多次:
var numbersAnimate = true;
// Animate any number
jQuery('.animateNumber').each(function(){
var value = new Number;
// Grab contents of element and turn it into a number
value = jQuery(this).text();
value = parseInt(value);
// Set the starting text to 0
jQuery(this).text('0');
// Animate to correct value
jQuery(this).bind('inview', function(event,visible) {
if(visible == true && numbersAnimate == true) {
numbersAnimate = false;
jQuery(this).animateNumber(
{
number: value,
},
1000
)
}
});
虽然,我不推荐它。。。最多可以作为临时补丁或者作为简写,您可以使用$function{/*您的代码*/};我尝试了你建议的两种解决方案,它破坏了页面上的其他项目。如果你能看到我所说的实际页面,也许会有所帮助。我试图更改的项目是主标题下的统计信息。