Is there a better fixed scheme for scrolling to the top?

Mobile, PC, Mini Program can all
I have implemented one. The principle is that by listening to the scroll bar, when the element reaches the top, it shows the element of fixed and hides the original element.
but the monitoring of the scroll bar is not sensitive, there will be ghosting, is there a better way to implement it?

Sep.28,2021

I read in a blog post that I also used this to make my own website:

$('-sharpbackTop').click(function (e) {
    e.preventDefault();
    $('html, body').animate({ scrollTop: 0 }, 300);
});
<a id="backTop" href="-sharp">backTop</a>
Menu