2014-09-30 60 views
0

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

謝謝您的幫助!

回答

4

好的,我嘗試了所有可用的解決方案,當你在頁面上有很多表單時,我可能會使用所有解決方案,並且它們都不能正常工作。

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

+1

即使您訪問網站上的不同頁面,上面的代碼仍會保持滾動位置。你可以修改'window.name + ='['+ location.pathname +'['+ $(window).scrollTop()。toString()+'['+ $(window).scrollLeft()。 toString();'AND' if(parts [parts.length - 3] == location.pathname){window.scrollTo(parseInt(parts [parts.length - 1]),parseInt(parts [parts.length - 2 ])); }' – 2016-04-21 17:12:21