2013-11-04 50 views
1

我正在使用一個平滑滾動腳本來處理我目前正在使用的網站,並且我遇到了一個非常令人討厭的問題,那就是我之前用相同的腳本經歷過的。它工作得很好,順利,但是當我點擊其中一個導航點時,應該引導我到達div(或a)的目標,它向我顯示0.1秒的目標區域,然後開始滾動。它不會每次都發生,但往往足以令人討厭。我怎麼能阻止呢?這是我說的腳本:平滑滾動腳本問題

$(window).load(function(){ 
       $(".contactLink").click(function(){ 
        if ($("#contactForm").is(":hidden")){ 
         $("#contactForm").slideDown("slow"); 
        } 
        else{ 
         $("#contactForm").slideUp("slow"); 
        } 
       }); 
      }); 
      function closeForm(){ 
       $("#messageSent").show("slow"); 
       setTimeout('$("#messageSent").hide();$("#contactForm").slideUp("slow")', 2000); 
      } 

$(window).load(function() { 
    function filterPath(string) { 
    return string 
     .replace(/^\//,'') 
     .replace(/(index|default).[a-zA-Z]{3,4}$/,'') 
     .replace(/\/$/,''); 
    } 
    $('a[href*=#]').each(function() { 
    if (filterPath(location.pathname) == filterPath(this.pathname) 
    && location.hostname == this.hostname 
    && this.hash.replace(/#/,'')) { 
     var $targetId = $(this.hash), $targetAnchor = $('[name=' + this.hash.slice(1) +']'); 
     var $target = $targetId.length ? $targetId : $targetAnchor.length ? $targetAnchor : true; 
     if ($target) { 
     var targetOffset = $target.offset().top - 110; 
     $(this).click(function() { 
      $('html, body').animate({scrollTop: targetOffset}, 1400); 
      var d = document.createElement("div"); 
     d.style.height = "101%"; 
     d.style.overflow = "hidden"; 
     document.body.appendChild(d); 
     window.scrollTo(0,scrollToM); 
     setTimeout(function() { 
     d.parentNode.removeChild(d); 
      }, 10); 
      return false; 
     }); 
     } 
    } 
    }); 
}); 
+1

請不要在您的網站上使用任何平滑滾動腳本,因爲它肯定會與任何實現平滑滾動的用戶瀏覽器插件衝突。那。是。非常。壞。實踐。 – dbanet

+0

我實際上同意 – mikakun

+0

看起來像刪除返回false沒有解決問題無論如何。好的,但是我應該怎麼使用? (這個問題在Firefox中不會發生) – h2c

回答

2

找到了解決辦法的:

$(this).click(function(e) { 
       e.preventDefault(); 

現在推出的罰款。

1
setTimeout(function() { 
    d.parentNode.removeChild(d); 
     }, 10); 
     return false; 
    }); 

return falsesetTimeOut