2013-09-30 58 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 
    }); 

});