JQuery Else if statement?
JQuery Else if statement?
嗨,我是 JQuery 和语句的新手,我似乎无法让我的代码工作我很确定这是小错误,但我看不到找到问题到底是什么,这是我的代码
$(window).scroll(function () {
if ($(this).scrollTop() >= 400) {
$('.BOX').animate({
marginLeft: 500,
marginRight: 0,
display: 'toggle'
}, 5000);
} else if ($(this).scrollTop() < 400) {
$('.BOX').animate({
marginLeft: 0,
marginRight: 0,
display: 'toggle'
}, 5000);
}
});
如果您能指出正确的方向,请提前感谢您。
http://jsfiddle.net/b6KuE/86/- Scrolltop>400 播放动画,我试图做的是当动画小于 400 时将动画反转回 0 左。
如注释中所述,滚动事件处理程序在窗口滚动时被一遍又一遍地调用,因此动画会一遍又一遍地开始。这些动画会排队,所以当你滚动到足够远的地方让它向另一个方向动画时,你需要很长时间才能看到它以这种方式进行动画处理,而且可能永远不会因为溢出而出现。
您需要在重新启动动画之前停止动画:
$(window).scroll(function () {
if ($(this).scrollTop() >= 400) {
$('.BOX').stop().animate({
marginLeft: 500,
marginRight: 0,
display: 'toggle'
}, 5000);
} else if ($(this).scrollTop() < 400) {
$('.BOX').stop().animate({
marginLeft: 0,
marginRight: 0,
display: 'toggle'
}, 5000);
}
});
斯菲德尔
$(this)
到底指的是什么?
试试这个:
$(window).scroll(function () {
if ($(window).scrollTop() >= 400) {
$('.BOX').animate({
marginLeft: 500,
marginRight: 0,
display: 'toggle'
}, 5000);
} else if ($(window).scrollTop() < 400) {
$('.BOX').animate({
marginLeft: 0,
marginRight: 0,
display: 'toggle'
}, 5000);
}
});
另外,您对这个问题已经有相当负面的回答。尝试帮助我们帮助您。如果您说您收到错误,那么准确说明该错误是什么可能会有所帮助。其他明智的,我们只是二次猜测。
相关文章:
- Jquery else if statement
- Rails 3 if statement with jQuery?
- JQuery Else if statement?
- 在jQuery .load Ajax之后执行if-statement
- Javascript if statement if (ball.style.left === '0px'
- If-Statement中的Javascript元素验证
- Javascript document.cookie if statement
- Javascript Logic In a loop/if statement
- Javascript if statement Google Tools
- Javascript IF statement
- ActionScript 3 If/ Else If Statement
- Javascript if-statement vs. comparsion
- Javascript condense if statement
- 嵌套的 if's:使用 if (x) {statement;} 还是 if (!x) 返回更好;陈述;
- Javascript optimizing if statement
- Javascript and CSS if statement
- JQUERY, AJAX if else statement
- Vuejs IF statement
- Datatables Find Replace If Statement
- javascript msgbox if statement