更新:
这是一个jsbin示例证明了这个问题。
基本上,我有以下javascript,它将窗口滚动到页面上的锚点:
// get anchors with href's that start with "#"
$("a[href^=#]").live("click", function(){
var target = $($(this).attr("href"));
// if the target exists: scroll to it...
if(target[0]){
// If the page isn't long enough to scroll to the target's position
// we want to scroll as much as we can. This part prevents a sudden
// stop when window.scrollTop reaches its maximum.
var y = Math.min(target.offset().top, $(document).height() - $(window).height());
// also, don't try to scroll to a negative value...
y=Math.max(y,0);
// OK, you can scroll now...
$("html,body").stop().animate({ "scrollTop": y }, 1000);
}
return false;
});
它工作正常……直到我手动尝试滚动窗口.滚动滚动条或鼠标滚轮时,我需要停止当前的滚动动画...但是我不确定如何执行此操作。
这可能是我的出发点...
$(window).scroll(e){
if(IsManuallyScrolled(e)){
$("html,body").stop();
}
}
...但是我不确定如何编码
IsManuallyScrolled
功能.我已经检查了
e
(
event
对象)在Google Chrome浏览器的控制台和AFAIK中无法区分手动滚动和jQuery的
animate()
滚动。
How can I differentiate between amanualscroll and one called via jQuery's
$.fn.animate
功能?
最新回答
- 2021-1-121 #
- 2021-1-122 #
几天前我也遇到了同样的问题.如果要使用jquery的动画功能,则必须使用轮询功能来模拟动画。
我做了这个类,应该在 ScrollDown.slow()时提供平滑的向下滚动 被称为。ScrollDown.current=$(window).scrollTop(); ScrollDown.lastValue; ScrollDown.lastType; ScrollDown.enabled=true; ScrollDown.custom=function(value,rate){ //let's say value==='bottom' and rate=10 if(value==='bottom'){ value=$(document).height()-$(window).height(); } ScrollDown.current=$(window).scrollTop(); ScrollDown.lastValue=value; (function poll(){ setTimeout(function(){ var prev=$(window).scrollTop(); //This is the critical part /*I'm saving again the scroll position of the window, remember 10 ms have passed since the polling has started At this rate, if the user will scroll up for down pre!==ScrollDown.current And that means I have to stop scrolling.*/ ScrollDown.current++; //increasing the scroll variable so that it keeps scrolling $(window).scrollTop(ScrollDown.current); if(ScrollDown.current<ScrollDown.lastValue && ScrollDown.enabled){ //ScrollDown.current<ScrollDown.lastValue basically checks if it's reached the bottom if(prev!==ScrollDown.current-1){ /*I'm checking if the user scrolled up or down while the polling has been going on, if the user scrolls up then prev<ScrollDown.current-1, if the user scrolls down then prev>ScrollDown.current-1 and at the next poll() the scrolling will stop because ScrollDown.enabled will bet set to false by ScrollDown.stop()*/ ScrollDown.stop(); } poll(); } },rate); })(); }; ScrollDown.stop=function(){ ScrollDown.enabled=false; }; ScrollDown.continue=function(){ ScrollDown.enabled=true; switch (ScrollDown.lastType){ case "fast": ScrollDown.fast(ScrollDown.lastValue); break; case "normal": ScrollDown.normal(ScrollDown.lastValue); break; case "slow": ScrollDown.slow(ScrollDown.lastValue); break; } }; ScrollDown.fast=function(value){ if(!ScrollDown.enabled){ ScrollDown.continue(); }else{ ScrollDown.lastType='fast'; ScrollDown.custom(value,1); } }; ScrollDown.normal=function(value){ if(!ScrollDown.enabled){ ScrollDown.continue(); }else{ ScrollDown.lastType='normal'; ScrollDown.custom(value,10); } }; ScrollDown.slow=function(value){ if(!ScrollDown.enabled){ ScrollDown.continue(); }else{ ScrollDown.lastType='slow'; ScrollDown.custom(value,50); } }; function ScrollDown(){}
因此,如果您要致电 ScrollDown.slow('bottom') 除非您手动向上或向下滚动,否则它将开始缓慢滚动直到到达页面底部。
- 2021-1-123 #
您可以设置一个变量以指示您的动画调用处于活动状态,然后检查 滚动处理程序中的那个变量。
window.IsAutoScrolling = true; $("html,body").stop().animate({ "scrollTop": y }, 1000); // Do something to set IsAutoScrolling = false, when the animation is done. $(window).scroll(e){ if(!window.IsAutoScrolling){ $("html,body").stop(); }
相关问题
- javascript:jQuery Deferred和对话框javascriptjqueryjqueryui2021-01-12 01:25
- jquery:JavaScript是多线程的吗?javascriptjquery2021-01-11 23:57
- jquery:在JavaScript中将字符串转换为XML文档javascriptjqueryxmldom2021-01-11 23:27
- javascript:AJAX jQuery每5秒刷新一次divjavascriptphpjqueryajax2021-01-11 22:56
- javascript:jQuery验证动态表单输入上的插件不起作用javascriptjqueryformsjqueryvalidate2021-01-11 22:56
尝试此功能:
还看到了本教程吗?
更新:现代浏览器现在使用" wheel"作为事件,因此我已将其包含在上面的代码中。