See jsfiddle.—https://jsfiddle.net/ac189xhn/
Create a two-page page that contains the main content and the sidebar.
I want to be able to smoothly scroll to the section tag with the same main content as the id in the href when I click on the sidebar link, but now I scroll to an unexpected place when I click the same link twice or after the second time.
No matter how many times you click on the same link, it stays in the same place, and when this occurs, we could infer that the value obtained by destination.offset().top
below is not the distance from the top of the parent element, but from the top of the browser screen.
$(".content").animate({
scrollTop —destination.offset().top,
},
"slow"
);
I understand that, but how can I behave correctly?
javascript html jquery
I solved myself.
varsh=$(".content").scrollTop();
Obtain the current location of the element to scroll to, and then
By adding destination.offset().top to the destination element, the behavior was correct.
Resolved jsfiddle↓
https://jsfiddle.net/zok428uq/
function scrollToAnchor(aid){
const destination=$("section[id='"+aid+"]");
varsh=$(".content").scrollTop();
$(".content").animate({
scrollTop —destination.offset().top+sh,
},
"slow"
);
}
$(document).on("click", ".sidebara", function(){
varanchor=this.hash.replace("#", "");
scrollToAnchor(anchor);
});
341 Understanding the Meaning of mpm prefork Settings
342 Memory layouts learned in theory don't work out as expected when actually printed as addresses.
372 Update Flask User Information
367 To Limit Column Values to Strings in a String List Using sqlalchemy
356 Unity Virtual Stick Does Not Return to Center When You Release Your Finger
© 2023 OneMinuteCode. All rights reserved.