I've been on a quest to uncover the cause and solution for this issue. Lately, I've been utilizing
$("#content").animate({scrollTop:$(#elementId).offset().top-183}, 600);
to achieve smooth scrolling to an ID within a <div>
. The number 183 serves as an offset to position my element at the top. While it does work, clicking it again when already at the target scrolls me back up to the beginning. If I click it while positioned below the element, it doesn't function correctly. It only behaves properly when I am situated above the element.
Could it be that "scrolltop" is not always the appropriate jQuery function for this task? Or should I assess whether the element is above or below me and employ a different command accordingly? It appears there isn't much documentation on this behavior, with only a few individuals mentioning it. Perhaps there's a simple solution that has eluded me thus far? I aim to create a JSfiddle to demonstrate this behavior, but I'm hopeful that someone here may have encountered this before and could offer some insight on how to resolve it?