2016-02-27 41 views
1

我有一个rails应用程序并使用引导程序。更新并重新插入对象后,我想通过快速动画滚动到div的顶部,因为我基于updated_at进行了排序。我尝试了不同的版本,但不能使它正常发生。我所做的只是能够滚动到没有动画的窗口顶部。插入对象后jquery滚动

我该如何让它工作?

<body> 
    <header>.....</header> 
    <div id ="sidebar">...</div>  //this is not scrollable 
    <div id = "page-content-wrapper"> //I can't decide which one is the scrollable one(wrapper/container/post-index) 
    <div class="container-fluid post-container"> 
     <div class="post-index new-post-insert"> 
     <%= render @posts %> 
     </div> 
    </div> 
    </div> 
</body> 

create.js.erb(当对象被更新了这个被调用)

$('#post_<%= @post.id %>').remove(); //old object gets removed 
$(".new-post-insert").prepend($post); //new object get inserted to the top 

//THIS ONE IS WORKING, BUT NOT IDEAL 
window.scrollTo(0,0); 

//I ALSO TRIED THESE WITH DIFFERENT CLASSES (wrapper/container/post-index) BUT NON OF THESE WERE WORKING 
//window.animate({ scrollTo(0,window.offset().top) }, 1000); 
//$('.post-container').scrollTop(100); 
//$('#page-content-wrapper').scrollTo(0, $('#post_<%= @post.id %>').offset().top); 
//$('.new-post-insert').stop().animate({scrollTop: $post.offset().top}, '500'); 
//$('#page-content-wrapper').scrollTop($('.post-index')[0].scrollHeight); 

回答

0

尝试使用这个,如果你想有一个平滑的动画

$("html, body").animate({ scrollTop: 0 }, "slow");