2014-12-29 151 views
0

我用我的网站上的各个部分下面的jQuery插件 - https://github.com/flesler/jquery.scrollTojquery.scrollTo - 滚动页面加载

我可以让它很好地工作环节,让被点击时,他们就滚动到网页的某一部分,像这样:

$('#button-top').bind('click', function(e) { 
    try { 
     e.preventDefault(); 
     target = this.hash; 
     $('html, body').scrollTo(target, 150); 
    } catch (error) { 
     alert('error - ' + error); 
    } 
}); 

但在一个页面上,我需要它,这样它滚动网页时加载,这样的:

if ($('.gform_validation_error').length > 0) { 
    // scroll here 
} 

我怎么会去一个让我的脚本轻轻滑动,以便在页面加载时滚动而不是点击链接时滚动?

回答

1

试试这个小提琴 - 消防使用.ready()功能页面加载

Here is Working Demo - Click here

$(document).ready(function(){ 
    if ($('.gform_validation_error').length > 0) { 
     var controlID ="#" + "divToScroll"; // this is ID of DIV where you want to scroll on page load. 
     $('html, body').animate({ 
    scrollTop: $(controlID).offset().top 
}, 2000); 
    } 
}); 
+0

作品像对待, 谢谢! :d – pealo86