Javascript 如何使用滚动触发器设置“marginLeft”的动画?
我有js代码:Javascript 如何使用滚动触发器设置“marginLeft”的动画?,javascript,jquery,animation,Javascript,Jquery,Animation,我有js代码: $(window).load(function() { var h = $('#home').height(); $(window).scroll(function() { var scroll = $(window).scrollTop(), ww = $(window).width(), s = $("#skills"); scroll >= 0 &&
$(window).load(function() {
var h = $('#home').height();
$(window).scroll(function() {
var scroll = $(window).scrollTop(),
ww = $(window).width(),
s = $("#skills");
scroll >= 0 && scroll < h + 299 ? s.addClass("first") : s.removeClass("first");
scroll >= h + 299 && scroll < h + 599 ? s.addClass("second") : s.removeClass("second");
scroll >= h + 600 && scroll < h + 899 ? s.addClass("third") : s.removeClass("third");
scroll >= h + 900 && scroll < h + 1199 ? s.addClass("fourth") : s.removeClass("fourth");
scroll >= h + 1200 && scroll < h + 1499 ? s.addClass("fifth") : s.removeClass("fifth");
scroll >= h + 1500 && scroll < h + 1799 ? s.addClass("sixth") : s.removeClass("sixth");
scroll >= h + 1800 && scroll < h + 2099 ? s.addClass("seventh") : s.removeClass("seventh");
scroll >= h + 2100 && scroll < h + 2399 ? s.addClass("eighth") : s.removeClass("eighth");
scroll >= h + 2400 ? s.addClass("ninth") : s.removeClass("ninth");
scroll >= h && scroll <= h + 2699 ? $("a.about").addClass("active") : $("a.about").removeClass("active");
scroll >= h + 2700 ? s.animate({ marginLeft: -ww }, 'slow') : s.animate({ marginLeft: 0 }, 'slow');
});
$("a.about").click(function() { $('html,body').animate({ scrollTop: h}, 'slow'); });
});
$(窗口).load(函数(){
var h=$('#home').height();
$(窗口)。滚动(函数(){
var scroll=$(窗口).scrollTop(),
ww=$(窗口).width(),
s=$(“技能”);
滚动>=0&滚动=h+299&&scroll=h+600&&scroll=h+900&&scroll=h+1200&&scroll=h+1500&&scroll=h+1800&&scroll=h+2100&&scroll=h+2400?s.addClass(“第九”):s.removeClass(“第九”);
滚动>=h&&scroll=h+2700?s.animate({marginLeft:-ww},'slow'):s.animate({marginLeft:0},'slow');
});
$(“a.about”)。单击(函数(){$('html,body')。设置动画({scrollTop:h},'slow');});
});
这是正确的,我想做什么就做什么。但是,我无法成功地设置marginLeft
的动画。如果从页面顶部开始滚动,当达到animatescroll>=h+2700
的条件时,需要等待很长时间,因为animate已启动。如果在scroll>=h+2700
之前重新加载页面并向下滚动一次,则动画将毫不延迟地完成
当用户到达滚动>=h+2700
时,我需要做什么来立即为我的块设置动画
至于设置条件的权利,就像我为他设置滚动支票计数一样?我是说有那么多几乎相同的行
UPD
我添加了.stop()
现在动画效果更好了。但如果你不停下来滚动,动画就会慢得多
-我在堆栈溢出中找到它。如果滚动JSFIDLE示例,您可以理解我的意思