Dav*_*och 25 javascript jquery scroll scrollbar mousewheel
这是一个证明问题的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;
});
Run Code Online (Sandbox Code Playgroud)
它工作得很好......直到我手动尝试滚动窗口.当滚动滚动条或鼠标滚轮时,我需要停止当前的滚动动画......但我不知道该怎么做.
这可能是我的出发点......
$(window).scroll(e){
if(IsManuallyScrolled(e)){
$("html,body").stop();
}
}
Run Code Online (Sandbox Code Playgroud)
...但我不确定如何编写该IsManuallyScrolled功能.我已经在Google Chrome的控制台和AFAIK中检出了e(event对象),无法区分手动滚动和jQuery的animate()滚动.
如何区分手动滚动和通过jQuery $.fn.animate函数调用的滚动?
Mot*_*tie 37
试试这个功能:
$('body,html').bind('scroll mousedown wheel DOMMouseScroll mousewheel keyup', function(e){
if ( e.which > 0 || e.type == "mousedown" || e.type == "mousewheel"){
$("html,body").stop();
}
})
Run Code Online (Sandbox Code Playgroud)
另外,你看到这个教程了吗?
更新:现代浏览器现在使用"wheel"作为事件,所以我将它包含在上面的代码中.
| 归档时间: |
|
| 查看次数: |
14105 次 |
| 最近记录: |