javascript 自动滚动页面到 HTML 的特定元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18979344/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 13:56:29  来源:igfitidea点击:

Auto scroll of page to specific element of HTML

javascriptjqueryhtmlcssjsp

提问by chaman shareef

Hi I have length jsp page and at the bottom of the page I have save button once I click the save button it will save the data to the server and once data is saved I load the same page again and I am displaying some information saying "Data is saved" at the bottom of the page.

嗨,我有长度的 jsp 页面,在页面底部我有保存按钮,一旦我点击保存按钮,它就会将数据保存到服务器,一旦保存数据,我会再次加载同一页面,我正在显示一些信息说“数据已保存”在页面底部。

My question is how can I load the page to show this message when page loads instead of loading page at its top.

我的问题是如何加载页面以在页面加载时显示此消息,而不是在其顶部加载页面。

<html>
<head></head>
<body>
<span> Hi </span>
.....................
.....................
.....................
.
.
.
.
.
.
//at the bottom of page
<span> Data Saved succefully</span>
</body>
</html>

So when I load the page I should see "Data Saved succefully" message.

因此,当我加载页面时,我应该看到“数据已成功保存”消息。

回答by Ashis Kumar

You can simply assign an id to the span and can redirect to the current page with #your-idattached at the end of your url.

您可以简单地为 span 分配一个 id,并可以重定向到当前页面,并#your-id附加在您的 url 末尾。

<a href="http://jsfiddle.net/tFcsP/show/#demo">Click to go</a>

Check this link http://jsfiddle.net/tFcsP/show/#demo

检查此链接http://jsfiddle.net/tFcsP/show/#demo

回答by SarathSprakash

Working DEMO

工作演示

Try this

试试这个

This will animate towards the required div or span

这将针对所需的 div 或 span 设置动画

$(document).ready(function () {
    target_offset = $('#third1').offset(),
    target_top = target_offset.top;
    $('html, body').animate({
        scrollTop: target_top
    }, 800);
});

回答by SarathSprakash

Using jQuery :

使用 jQuery :

 $(function() {       
     $(window).scrollTop($('#div_name').offset().top);
 });

回答by Pratim

use simple java script

使用简单的java脚本

<htm

l>
<head>
<script>
function scrollWindow()
  {
  window.scrollTo(100,500)
  }
</script>
</head>
<body>

<input type="button" onclick="scrollWindow()" value="Scroll">
<p>SCROLL SCROLL SCROLL SCROLL SCROLL SCROLL SCROLL SCROLL</p>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<p>SCROLL SCROLL SCROLL SCROLL SCROLL SCROLL SCROLL SCROLL</p>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<p>SCROLL SCROLL SCROLL SCROLL SCROLL SCROLL SCROLL SCROLL</p>

</body>
</html> 

when you save the data and after say page load aur any response recieved then.

当您保存数据并在说页面加载之后收到任何响应时。

Heres w3 school link scroll to

继承人 w3 学校链接 滚动到