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);
}