2013-09-30 57 views
0

我用光滑的腳本滾動我的網頁,但腳本不工作我很困惑我哪裏錯了,請幫我如何我的頁面滾動下來平滑滾動

我的腳本是:

$(".scroll").click(function(event){ 
    event.preventDefault(); 
    //calculate destination place 
    var dest=0; 
    if($(this.hash).offset().top > $(document).height()-$(window).height()){ 
      dest=$(document).height()-$(window).height(); 
    }else{ 
      dest=$(this.hash).offset().top; 
    } 
    //go to destination 
    $('html,body').animate({scrollTop:dest}, 1000,'swing'); 
}); 

和我在錨標記使用.scroll此類型:

<a class="scroll" href="#bot">Click here</a> 

請幫幫我!

+0

*,但劇本不起作用*非常模糊 –

+0

對不起,你說的這個腳本是非常模糊的,但我看到在各種網站上的這個腳本。 –

+0

你能做一個小提琴嗎? –

回答

0

用於平滑滾動我會建議使用此:

(從劇本我在GitHub上發現稍微修改 - 雖然我不記得了原作者 - 抓住它從我的項目之一)

jQuery.fn.anchorAnimate = function(settings) { 

settings = jQuery.extend({ 
     speed : 1100 
}, settings); 

return this.each(function(){ 
    var caller = this 
    $(caller).click(function (event) { 
     event.preventDefault() 
     var locationHref = window.location.href 
     var elementClick = $(caller).attr("href") 

     var destination = $(elementClick).offset().top; 
     $("html:not(:animated),body:not(:animated)").animate({ 
      scrollTop: destination 
     }, settings.speed, function() { 
       window.location.hash = elementClick 
     }); 
     return false; 
    }) 
}) 
} 

然後調用它像這樣:

jQuery(document).ready(function($) { 

    $(".menu li a").anchorAnimate({ 
     speed: 600 
    }); 

});