JS implementation Zo Shu in the current page different position jump __js

Source: Internet
Author: User

The use of JS to achieve the internal page jump to the specified position of the method, similar to the anchor effect of a tag, first introduced the basic methods used:

1.offset ()

The offset () method returns or sets the offset (position) of the matching element relative to the document.

Returns the offset coordinate of the first matching element.

The object returned by this method contains two integer attributes: Top and left, in pixels. This method is valid only for visible elements. Grammar

$ (selector). Offset ()
Set offset coordinates

Sets the offset coordinates for all matching elements. Grammar

$ (selector). Offset (value)
Parameters Description
Value

Necessary. Specify top and left coordinates in pixels.

Possible values: value pairs, such as {top:100,left:0} objects with top and left attributes

2.scrollTop ()

The ScrollTop () method returns or sets the vertical position of the scroll bar for the matching element.

If the method does not set a parameter, it returns the offset at the top of the relative scroll bar in pixels.

Grammar

$ (selector). scrolltop (offset)
Parameters Description
Offset Optional. Specify the offset, in pixels, relative to the top of the scroll bar.
Tips and Comments

Note: This method is valid for both visible and invisible elements.

Note: When used to get a value, the method returns only the scroll top offset of the first matching element.

Note: When used to set a value, this method sets the scroll top offset for all matching elements.

Implementation method:

1. Set the address of a link to the ID of the div corresponding to the jump position;

2. The href that gets the link is stored in the variable.

3. Finds the element by ID selector and obtains its offset from the document (up offset);

4. Assign the upper offset to the scrolltop () of the BODY element .

Code on:

<li class= "Menu" >
<a href= "#桌面" >

<span> desktop </span>
</a>
</li>

$ (". Menu Li"). On ("click", Function () {

var domid=$ ($ (this). Children (' a '). attr (' href ')), Scrollheight=domid.offset (). $ (' body '). scrolltop (ScrollHeight);

}


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.