我有一個代碼塊,通過一個animate
jQuery的東西滾動頁面,但增加可用性,如果用戶以任何方式干擾滾動運動(例如滾動輪,抓取滾動條等),那麼它會立即停止運動。滾動抓取工作,只移動一個像素
一個星期前我在這問了一個問題here但是有些測試人員試用了代碼後發現它們在基於Gecko的瀏覽器中不起作用。
這是我到目前爲止的代碼:
$(document).ready(function() {
// scroll to top: check if user/animate is scrolling
var scrollAnimating = false;
jQuery.fx.step.scrollTop = function(E) {
scrollAnimating = true;
E.elem.scrollTop = E.now;
scrollAnimating = false;
};
// go to top (on click)
$('#gototop').click(function() {
$(this).fadeOut(100,function() {
$(this).css({backgroundColor: '#CCC'}).html('Going... (scroll to stop)').fadeIn(100, function() {
$('html,body').animate({scrollTop:0},3000);
})
});
$(window).scroll(function() {
if (!scrollAnimating) {
$('html,body').stop();
$('#gototop').html('Go to top');
};
});
resetNames();
return false;
});
function resetNames() {
setTimeout(function() {
$('#gototop').html('Go to top').css({backgroundColor: '#DDD'});
},3000);
}
});
基本上就點擊#gototop
將開始動畫滾動到頂部。如果滾動受到干擾,則滾動動作停止,重置#gototop
的文本。
這段代碼有幾個問題:有些部分的效率太低,而且在基於Gecko的瀏覽器(biggie)中不起作用。
我該如何讓它起作用?任何關於如何使其高效的指針?
我需要讓它在所有瀏覽器中都能正常工作(IE除外:如果它沒有在那裏工作,對我很好)。也許我應該嘗試某種瀏覽器檢測腳本? – 2009-11-17 03:01:31
你在瀏覽器中試過我的腳本嗎?結果是什麼? – SimonDever 2009-11-17 03:21:06
沒有必要,因爲你完全錯過了所有複雜信息的要點(沒有任何意圖):當檢測到用戶正試圖對抗滾動時,所有的代碼應該自動停止滾動(比如停止它來閱讀)。很明顯,你的代碼不會這樣做。抱歉。 – 2009-11-18 22:21:44