2014-03-02 70 views
0

我的回到頂部按鈕似乎處於決定當我向下滾動一下時是向上還是向下滑動,以及當我停止滾動時它只是向上滑動然後下來幾次然後停留,直到我再次滾動並重復相同的過程。有時當我在頁面上一路走下來,從滾動按鈕的時候一路向上滑動即使認爲scrolltop屬性爲0.我不確定我的代碼是問題還是使用 使用最新的jQuery版本返回頁首按鈕在滾動條件下即使滿足條件時也會上下滑動

$(document).ready(function(){ 
$(".totop").hide(); 

$(function(){ 
$(window).scroll(function(){ 
    if ($(this).scrollTop()>600) 
    { 
    $('.totop').slideDown(); 
    } 
    else 
    { 
    $('.totop').slideUp(); 
    } 
}); 

$('.totop a').click(function (e) { 
    e.preventDefault(); 
    $('body,html').animate({scrollTop: 0}, 500); 
}); 

}); 
}); 

回答

0

我在我的頁面上有相同的代碼,它在jQuery版本1.6.4下運行良好。我測試了最新的jQuery版本,實際上它改變了它的行爲。看起來像現在jQuery顯示對象,然後再滑動它。我對代碼進行了一些修復,並添加了油門/去抖插件以避免多次調用滾動事件。我認爲現在看起來不錯。

測試http://jsfiddle.net/QXJ53/

/* 
* jQuery throttle/debounce - v1.1 - 3/7/2010 
* http://benalman.com/projects/jquery-throttle-debounce-plugin/ 
* 
* Copyright (c) 2010 "Cowboy" Ben Alman 
* Dual licensed under the MIT and GPL licenses. 
* http://benalman.com/about/license/ 
*/ 
(function(b,c){var $=b.jQuery||b.Cowboy||(b.Cowboy={}),a;$.throttle=a=function(e,f,j,i){var h,d=0;if(typeof f!=="boolean"){i=j;j=f;f=c}function g(){var o=this,m=+new Date()-d,n=arguments;function l(){d=+new Date();j.apply(o,n)}function k(){h=c}if(i&&!h){l()}h&&clearTimeout(h);if(i===c&&m>e){l()}else{if(f!==true){h=setTimeout(i?k:l,i===c?e-m:e)}}}if($.guid){g.guid=j.guid=j.guid||$.guid++}return g};$.debounce=function(d,e,f){return f===c?a(d,e,false):a(d,f,e!==false)}})(this); 

$(document).ready(function(){ 

    $(".totop").hide(); 

    $(window).scroll($.throttle(1000, function(){ 

     if ($(window).scrollTop()>100) 
     { 
     $('.totop').slideDown(); 
     } 
     else 
     { 
      if($('.totop').is(':visible')){ 

       $('.totop').slideUp(); 
      } 

     } 
    })); 

    $('.totop a').click(function (e) { 
     e.preventDefault(); 
     $('body,html').animate({scrollTop: 0}, 500); 
    }); 

}); 
+0

感謝那定了! – imGreg

相關問題