An instance of jquery mock anchor jumping to the specified position on the page

  • 2021-07-21 06:49:36
  • OfStack

The js mock anchor jumps to the specified location on the page using the offset () method


$(document).ready(function(){

 var target_top = $("#qa5").offset().top;
 //$("html,body").animate({scrollTop: target_top}, 1000);  // Jump with sliding effect 
 $("html,body").scrollTop(target_top);

  });

jQuery:

offset ():

Gets the relative offset of the matching element at the current viewport.

The returned object contains two integer attributes: top and left. This method is only valid for visible elements.

position ():

Gets the offset of the matching element from the parent element.

The returned object contains two integer attributes: top and left. For accurate calculation results, use pixel units on padding, border and padding attributes. This method is only valid for visible elements.

scrollTop ():

Method sets or returns the vertical scroll bar position of the selected element.

Tip: When the scroll bar is at the top, the position is 0.

When used to return to a position:

This method returns the vertical position of the scroll bar of the first matching element.

When used to set a position:

This method sets the vertical position of scroll bars for all matching elements.


Related articles: