Jump to the top of a web page
While I am surfing the web for research, fun or even work, I keep an eye out for cool looking UI elements. I recently found one that is so simple, yet so useful.
I was on a site, like most sites these days, which had an exceedingly long home page. I am so accustomed to these long pages that I just scroll to the top without thinking about it. This web site had a non-assuming icon at the lower right in the footer. There was no label or tooltip to explain what it was for. They might want to change that part, but it is fun to find these hidden treats.
When you clicked the icon, it jumped the page back up to the top of the page, nice and smooth. I thought it was cool. I kept going to the bottom and clicking on it.
How did they do it?
When I looked at the code, I was surprised at how simple it was:
onclick=`window.scrollTo({top:0, behavior:`smooth`})`
That was it! My next thought was, can I do this in Blazor?
Blazor Implementation
Good news, sort of, it works the same way in Blazor. But you
must use JavaScript. I am not a major fan of mixing of JavaScript in my Blazor
applications, but this was different. It was in line, not script file, no
script tags in my layout.chtml file. The best part is that it just works.
Summary
I like this technique of jumping back to the top of the
page. I like it so much; I added it to my blog.
Note: Why the airplane icon? I was a member of the U.S. Air
Force, and it just says “Fly to the Top”
Comments
Post a Comment