最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - How to focus on a particular portion of a web page using JavaScript? - Stack Overflow

programmeradmin0浏览0评论

Can somebody tell me how to focus on particular part of a web page by using JavaScript events? Let's say I clicked a button and it will scroll down or scroll up to a particular portion a page. Here is my inplete code:

<html>
<head>
<script type="text/javascript">
 function focusOnForm() {
  document.getElementById('form').style.display = '';
 }
</script>
</head>
<body onload="document.getElementById('form').style.display = 'none';">
<div style="height: 900px; width: width: 500px; background-color: #000;">
</div>
<br />
<input type="button" value="Add" style="float: right;" onclick="focusOnForm();" />
<br />
<div id="form">
 First Name: <input type="text" /> <br />
 Last Name: <input type="text" /> <br />
 Address: <input type="text" /> <br />
 Contact Number: <input type="text" /> <br />
 Email: <input type="text" /> <br />
</div>
</body>
</html>

I really need help.

Can somebody tell me how to focus on particular part of a web page by using JavaScript events? Let's say I clicked a button and it will scroll down or scroll up to a particular portion a page. Here is my inplete code:

<html>
<head>
<script type="text/javascript">
 function focusOnForm() {
  document.getElementById('form').style.display = '';
 }
</script>
</head>
<body onload="document.getElementById('form').style.display = 'none';">
<div style="height: 900px; width: width: 500px; background-color: #000;">
</div>
<br />
<input type="button" value="Add" style="float: right;" onclick="focusOnForm();" />
<br />
<div id="form">
 First Name: <input type="text" /> <br />
 Last Name: <input type="text" /> <br />
 Address: <input type="text" /> <br />
 Contact Number: <input type="text" /> <br />
 Email: <input type="text" /> <br />
</div>
</body>
</html>

I really need help.

Share Improve this question edited Sep 2, 2022 at 15:46 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Nov 5, 2011 at 18:36 NinjaBoyNinjaBoy 3,75519 gold badges57 silver badges69 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

Use the .scrollIntoView() method:

 function focusOnForm() {
    var form = document.getElementById('form');
    form.style.display = '';
    form.scrollIntoView();
 }

Fiddle: http://jsfiddle/g8Mqr/

You can scroll to a pixel location with:

document.body.scrollTop = 2000;

http://jsfiddle/ThinkingStiff/A7JMZ/

发布评论

评论列表(0)

  1. 暂无评论