你需要的動畫標記,這樣
$("html, body").stop(true, false).prop('animatedMark',0.0).animate({scrollTop : top, animatedMark: '+=1.0'})
下面是代碼,該代碼是GWT和JavaScript的組合,以便把它搬到JS,不是完全測試的東西,請嘗試
var lastAnimatedMark=0.0;
function scrollToThis(top){
// Select/ stop any previous animation/reset the mark to 0
// and finally animate the scroll and the mark
$("html, body").stop(true, false).prop('animatedMark',0.0).
animate({scrollTop : top, animatedMark: '+=1.0'}
,10000,function(){
//We finished , nothing just clear the data
lastAnimatedMark=0.0;
$("html, body").prop('animatedMark',0.0);
});
}
//Gets the animatedMark value
function animatedMark() {
var x=$("html, body").prop('animatedMark');
if (x==undefined){
$("html, body").prop('animatedMark', 0.0);
}
x=$("html, body").prop('animatedMark');
return x;
};
//Kills the animation
function stopBodyAnimation() {
lastAnimatedMark=0;
$("html, body").stop(true, false);
}
//This should be hooked to window scroll event
function scrolled(){
//get current mark
var currentAnimatedMark=animatedMark();
//mark must be more than zero (jQuery animation is on) & but
//because last=current , this is user interaction.
if (currentAnimatedMark>0 && (lastAnimatedMark==currentAnimatedMark)) {
//During Animation but the marks are the same !
stopBodyAnimation();
return;
}
lastAnimatedMark=currentAnimatedMark;
}
下面是關於它的
http://alaamurad.com/blog/#!canceling-jquery-animation-after-user-interaction的博客
享受!
您的代碼可能無法正常工作,因爲你的代碼'的keyCode == 38 || 40'。你應該更喜歡'keyCode == 38 || keyCode == 40'。由於40被解釋爲「真」,因此你的當前代碼總是通過'if'-塊。 – pimvdb 2011-05-28 09:13:25
啊,很高興知道!然而,這個錯誤並不能阻止它的工作,無論你按什麼鍵,它都會使滾動停止。順便說一下,是否有一種緊湊的方式來給出if語句的鍵碼列表? (我在上面/下面留下了很多頁面,在家裏/結尾 - 我在這裏複製的內容是保持簡短。)似乎必須有更好的方法,而不是像編寫keyCode == number這樣的六次。 – 2011-05-28 13:34:28
你可以例如提供有問題的密鑰數組,然後使用'$ .inArray'進行檢查。 – polarblau 2011-05-28 15:16:06