Laravel-在帖子上保持滚动位置


Laravel - maintain scroll position on post

我的Laravel应用程序中有一个简单的评论系统。问题是当有人提交评论页面时,会在页面顶部重新加载。我希望该页面将用户返回到原始评论所在的位置(保持滚动位置)。

以下是负责评论的部分观点:

@if ($signedIn)
    {{ Form::open(['route' => ['comment_path', $status->id], 'class' => 'comments__create-form',]) }}
    {{ Form::hidden('status_id', $status->id) }}
<div class="form-group">
    {{ Form::textarea('body', null, ['class' => 'form-control', 'rows' => 1]) }}
</div>
    {{ Form::close() }}
@endif
@unless ($status->comments->isEmpty())
<div class="comments">
@foreach ($status->comments as $comment)
@include ('statuses.partials.comment')
@endforeach
</div>
@endunless

以下是当有人点击ENTER 时提交评论的jquery脚本

<script>
$('#flash-overlay-modal').modal();
$('.comments__create-form').on('keydown', function(e) {
    if (e.keyCode == 13) {
        e.preventDefault();
        $(this).submit();
    }
});
</script>

谢谢你的帮助!

好吧,我可能尝试了所有可用的解决方案,但当你像我一样在页面上有很多表单时,没有一个能完美工作。

感谢Evalds Urtan,我解决了这个问题。Evalds具有保持滚动位置的最佳和最短脚本。

你不必做任何事情,只需包括他的jquery

/*
* Maintain / Keep scroll position after post-back / postback / refresh. Just include plugin (no need for cookies)
*
* Author: Evalds Urtans
* Website: http://www.evalds.lv
*/
(function($){
window.onbeforeunload = function(e){    
window.name += ' [' + $(window).scrollTop().toString() + '[' + $(window).scrollLeft().toString();
};
$.maintainscroll = function() {
if(window.name.indexOf('[') > 0)
{
var parts = window.name.split('['); 
window.name = $.trim(parts[0]);
window.scrollTo(parseInt(parts[parts.length - 1]), parseInt(parts[parts.length - 2]));
}   
};  
$.maintainscroll();
})(jQuery);

谢谢Evalds!