简体   繁体   English

在谷歌表单中按下提交按钮后如何使页面滚动到顶部?

[英]How to make page scroll to top after submit button is pressed in google form?

I have used google form on my website.我在我的网站上使用了谷歌表单。 I am having an issue when submit button is pressed, the page doesn't go to top and shows alot of blank white space as we have to go down alot in submitting the form.我在按下提交按钮时遇到问题,页面没有转到顶部并显示大量空白区域,因为我们在提交表单时必须向下很多。

Here's the form i used : <iframe src="https://docs.google.com/forms/d/e/1FAIpQLSc2zeeSoq25ZdSkWK5bY5uwp6NJ4r-PdzILlXAeuszyVwQlMA/viewform?embedded=true" width="100%" height="1800px" frameborder="0" marginheight="0" marginwidth="0">Loading…</iframe>这是我使用的表格: <iframe src="https://docs.google.com/forms/d/e/1FAIpQLSc2zeeSoq25ZdSkWK5bY5uwp6NJ4r-PdzILlXAeuszyVwQlMA/viewform?embedded=true" width="100%" height="1800px" frameborder="0" marginheight="0" marginwidth="0">Loading…</iframe>

Used this code but it doesn't work.使用此代码但它不起作用。 <form onsubmit="parent.scrollTo(0, 0); return true">

NOTE: I don't have knowledge of JS or Jquery so i maybe needing some detailed explaination.注意:我不了解 JS 或 Jquery,所以我可能需要一些详细的解释。 Thankyou谢谢

There are two options that you can use.您可以使用两个选项。

  1. Add a jquery script to the iframe load event在 iframe 加载事件中添加 jquery 脚本

 <script> jQuery("iframe").load(function() { jQuery("html,body").animate({ scrollTop: 0 }, "slow"); }); </script>

  1. Add an onload event to the iframe;给 iframe 添加一个 onload 事件; onload="window.parent.scrollTo(0,0)"

 < iframe src = "https://docs.google.com/forms/d/e/1FAIpQLSc2zeeSoq25ZdSkWK5bY5uwp6NJ4r-PdzILlXAeuszyVwQlMA/viewform?embedded=true" width = "100%" height = "1800px" frameborder = "0" marginheight = "0" marginwidth = "0" onload = "window.parent.scrollTo(0,0)" > Loading… </iframe>

The second option is recommended as it will work on forms with multiple pages.推荐使用第二个选项,因为它适用于具有多个页面的表单。

I had a similar problem.我有一个类似的问题。 While trying some advice, I came across a solution.在尝试一些建议时,我遇到了一个解决方案。 This JavaScript solution works like a charm in Flask.这个 JavaScript 解决方案在 Flask 中就像一个魅力。 In your HTML file, consider pasting this script, the part after your iframe.在您的 HTML 文件中,考虑粘贴此脚本,即 iframe 之后的部分。

<script>
document.querySelector("iframe").addEventListener("load", 
    function() {
        window.scrollTo({
    top: 0,
    left: 0,
    behavior: 'smooth'
  });
});

</script>  

This should solve it:这应该解决它:

<iframe src="iframe.url.com" onload="loaded()"></iframe>
<script type="application/javascript">
  var loadCounter = 0;
  var loaded = function () {
  loadCounter += 1;
  if (loadCounter === 2) {
    window.scrollTo({top: 550, left: 0, behavior: 'smooth'});
  }
}
</script>

More info: https://medium.com/@richard.jones/a-trick-to-handle-varying-page-sizes-in-embedded-google-forms-4f21b71055ed更多信息: https : //medium.com/@richard.jones/a-trick-to-handle-variing-page-sizes-in-embedded-google-forms-4f21b71055ed

You could just use a href="#header" on submit button and put the id of the header section, and it´ll scrool to the top.您可以在提交按钮上使用href="#header"并放置标题部分的 id,它会滚动到顶部。 ^-^ ^-^

 <div id="header"></div> <button href="#header">Submit</button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM