如何滚动发布表单(How to scroll on post a form)

我有一个表单,我想做一件事:当我点击de按钮并发布表单时,我想滚动到一个特定的点。 我尝试过使用cookies,我的代码是:`

<!-- I have a library called cookie and the cookie works well, but when I send, the page go up and I want to stay in the scroll position --> <form method="post" action=""> <input type="submit" value="send" name="btn" /></div> </form> <script> function send() { cookie.asignar("altura", $(window).scrollTop(), "../js/", 10); $(window).scrollTop(0, cookie.agafar("altura")); cookie.suprimir("altura"); } </script> <?php if (isset($_POST['btn'])) { echo '<script>send();</script>'; } ?>

我使用jquery和php。

I have a form that I want to do one thing: when I click de button and it post the form, I want to scroll to a specific point. I've tried with cookies and my code is this: `

<!-- I have a library called cookie and the cookie works well, but when I send, the page go up and I want to stay in the scroll position --> <form method="post" action=""> <input type="submit" value="send" name="btn" /></div> </form> <script> function send() { cookie.asignar("altura", $(window).scrollTop(), "../js/", 10); $(window).scrollTop(0, cookie.agafar("altura")); cookie.suprimir("altura"); } </script> <?php if (isset($_POST['btn'])) { echo '<script>send();</script>'; } ?>

I use jquery and php.

最满意答案

那么,尝试使用localStorage而不是cookies的另一个解决方案:

<form method="post" action=""> <input type="submit" value="send" name="btn" /></div> </form> <script> $(document).ready(function(){ // Scroll to screen height if previously saved var previousHeight = localStorage.getItem("pageHeight"); if(previousHeight){ $('html, body').animate({ scrollTop: localStorage.getItem("pageHeight") }, 1000); localStorage.setItem("pageHeight", null); } // Saves scroll height on submit $("form").on("submit", function(){ localStorage.setItem("pageHeight", $(window).scrollTop()); }); }); </script>

Well, trying another solution using localStorage instead of cookies:

<form method="post" action=""> <input type="submit" value="send" name="btn" /></div> </form> <script> $(document).ready(function(){ // Scroll to screen height if previously saved var previousHeight = localStorage.getItem("pageHeight"); if(previousHeight){ $('html, body').animate({ scrollTop: localStorage.getItem("pageHeight") }, 1000); localStorage.setItem("pageHeight", null); } // Saves scroll height on submit $("form").on("submit", function(){ localStorage.setItem("pageHeight", $(window).scrollTop()); }); }); </script>

更多推荐

cookie,send,电脑培训,计算机培训,IT培训"/> <meta name="description"