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

时间:2014-09-30 09:04:31

标签: javascript php jquery laravel

我的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

这是jquery脚本,当有人点击ENTER

时提交评论
<script>

$('#flash-overlay-modal').modal();

$('.comments__create-form').on('keydown', function(e) {

    if (e.keyCode == 13) {

        e.preventDefault();
        $(this).submit();
    }

});

</script>

感谢您的帮助!

1 个答案:

答案 0 :(得分:6)

好吧,我尝试了所有可用的解决方案,当你在我的页面上有很多表单时,它们并没有完美地工作。

感谢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!

相关问题