JS scroll/jump to the specified position

Source: Internet
Author: User
Two requirements should be solved: one is to jump from page a to page B and scroll to any place on the page; the other is to click an element inside page B and scroll to any place on the page; how can this problem be solved? It's very easy, of course, to use an anchor. First, create an anchor on page.

<Body>

<A href = "B .html # POS" target = "_ blank"> click to jump </a> <body>
Then define the anchor on page B.

<Body>

... There are a lot of text here. Open the page and pull out the scroll bar... <a name = "POS"> scroll here </a>... add more text... </body>
Okay, test, OK! Click "scroll here" to appear at the top of the browser. The actual situation is often when the production personnel cut the page, the product suddenly said to add a function, need to scroll to a certain place, but that place is a DIV, there is no a anchor, creating an anchor occupies the space of the webpage and affects the webpage layout. What should I do? Try to create a hidden anchor. The hidden anchor does not occupy space. Continue: If I have <a name = "POS"> scroll here </a> On page B not displayed, set display: none; so can we scroll here? Try

<Body>

... There are a lot of text here. Open the page and pull out the scroll bar... <a name = "POS" style = "display: none;"> scroll here </a> hide and scroll here... add more text... </body>
Poor, ie can work, Firefox does not give face. You can only use another method. Can you specify an ID for the anchor to scroll here? Try

<Body>

... There are a lot of text here. Open the page and pull out the scroll bar... <a id = "POS"> scroll here </a> hide and scroll here... add more text... </body>
OK, success! It seems that it is a good way to change to id. Now try changing the anchor to Div.

<Body>

... There are a lot of text here. Open the page and pull out the scroll bar... <Div id = "POS"> scroll here </div> to hide and scroll here... add more text... </body>
OK, success! This solves the problem. You do not need to insert a <A> </a> anchor. You only need to add an ID to the div. Next, we will solve the second requirement. Click an element in page B and scroll to the specified position. First, create an anchor at the top of B .html to point to the DIV to be rolled

<Body>

<A href = "# POS"> click here to jump to this page </a>... there are a lot of text here. Open the page and pull out the scroll bar... <Div id = "POS"> scroll here </div> to hide and scroll here... add more text... </body>
OK, success! What if you want to click a button to scroll to the specified position? You cannot add href to the button, but you can only perform twists and turns. Create a hidden anchor, add an onclick event when you click the button, and call the anchor's click event through js to save the country.

<Body>

<SCRIPT type = "text/JavaScript"> function click_scroll () {document. getelementbyid ("anchor_scroll "). click () ;}</SCRIPT> <input type = "button" value = "click button to jump to" onclick = "click_scroll (); "/> <a id =" anchor_scroll "href =" # POS "style =" display: none "> click here to go to this page </a>... there are a lot of text here. Open the page and pull out the scroll bar... <Div id = "POS"> scroll here </div> to hide and scroll here... add more text... </body>
OK, success! Now that the button is successful, you can do anything else. IMG and Div can do this. There is also a way to do this through jquery's aminate animation method. I post the code

<Body>

<SCRIPT type = "text/JavaScript"> function click_scroll () {var scroll_offset = $ ("# POS "). offset (); // get the offset of the POs Div layer, which contains two values: Top and left $ ("body, HTML "). animate ({scrolltop: scroll_offset.top // you can scroll to make the scrolltop of the body equal to the top of the POs.}, 0 );} </SCRIPT> <input type = "button" value = "click button to jump to" onclick = "click_scroll ();"/>... there are a lot of text here. Open the page and pull out the scroll bar... <Div id = "POS"> scroll here </div> to hide and scroll here... add more text... </body>
OK, success! The advantage of this method is to control the rolling speed. The blue 0 above controls the speed, and 0 immediately means to set it to 1000, we can see that it is slowly rolling to the POs. If it is set to 5000, it will be slower. Why? Because jquery's animation is used for animation, more functions can be studied in detail. It is a great pleasure to help you with your hard work code. You are welcome to leave a message to the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.