我有一个自定义 slider 。我想做这样的事情。当用户向下滚动时,它应该以动画方式向下滚动到下一张幻灯片,并且在此动画期间用户不应该能够滚动。但我有一个问题。滚动事件被多次触发,一个动画完成后,第二个动画开始,依此类推。
这是我的代码示例
$(window).scroll(function(e){
if($scrolling){
e.preventDefault();
e.stopPropagation();
}
})
$(window).on('mousewheel', function(event){
$scrolling = true
$('html, body').animate({scrollTop: 'my position here' }, {done: function(){ $scrolling = false; } }, 1000)
});
我做错了什么?提前致谢!
最佳答案
您可以仅通过 mousewheel
事件控制一切。
var $scrolling = false;
$(window).on('mousewheel', function(event){
if (!$scrolling){
$scrolling = true;
$('html, body').animate({scrollTop: 'my position here' }, {done: function(){ $scrolling = false; } }, 1000);
}
});
关于javascript - 在滚动事件上设置滚动顶部动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20940528/