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);
});
567 Who developed the "avformat-59.dll" that comes with FFmpeg?
884 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
567 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
606 Uncaught (inpromise) Error on Electron: An object could not be cloned
© 2024 OneMinuteCode. All rights reserved.