Creating a “Scroll Back To Top” button is a great way to improve user experience on your website. With jQuery, you can easily add this functionality with a few simple steps.
Steps to Create Scroll Back To Top Button:
First, you’ll want to create the HTML element that will contain the button. This can be a link, image, or any other HTML element. For this example, we’ll use a link element.
<style type="text/css"> .top-btn { position: fixed; top: 520px; right: 10px; border: 1px solid #FFB955; background: #ff0000; width: 60px; height: 60px; border-radius: 50%; cursor: pointer; display: none; transition: 0.5s ease-in; z-index: 999; } .top-btn:hover { background: #ed3237; border: 1px solid #ffffff; } .top-btn a:hover { color: #000000 !important; } .top-btn i { color: #ffffff !important; text-align: center; position: absolute; left: 25px; top: 20px; } </style>
Next, you’ll need to add some CSS to style the element. For this example, we’ll make the link look like a button with a round corner, a gray background, and white text.
<style type="text/css"> .top-btn { position: fixed; top: 520px; right: 10px; border: 1px solid #FFB955; background: #ff0000; width: 60px; height: 60px; border-radius: 50%; cursor: pointer; display: none; transition: 0.5s ease-in; z-index: 999; } .top-btn:hover { background: #ed3237; border: 1px solid #ffffff; } .top-btn a:hover { color: #000000 !important; } .top-btn i { color: #ffffff !important; text-align: center; position: absolute; left: 25px; top: 20px; } </style>
Now, it’s time to add the jQuery code that will make the “Scroll Back To Top” button work. We’ll bind the click event to the link element, and when the user clicks on it, the page will scroll to the top.
$(window).ready(function(){ $(window).scroll(function(){ if ($(this).scrollTop()>200) { $('.top-btn').fadeIn(500); }else{ $('.top-btn').fadeOut(500); } });
Finally, we’ll add the code to show and hide the button, depending on the user’s scroll position. When the user scrolls down the page, the button will appear and when the user scrolls to the top of the page, it will disappear.
$('.top-btn').click(function(){ $('html, body').animate({scrollTop:0},50) });
That’s it! With these few steps, you can easily create a “Scroll Back To Top” button on your website with jQuery. This will make it easier for users to quickly jump to the top of the page with just one click.
For more web development tips and tricks, be sure to check out our article on how to show and hide passwords using jQuery. Happy coding!
I’m really impressed with your writing skills as well as with the
layout on your weblog. Is this a paid theme or did you customize
it yourself? Either way keep up the nice quality writing, it is
rare to see a nice blog like this one these
days.
Hello to every body, it’s my first pay a
quick visit of this webpage; this webpage consists of awesome and genuinely fine
material designed for readers.
Hello, this weekend is nice designed for me, for the reason that this time i am
reading this enormous informative piece of
writing here at my home.
Hi, every time i used to check blog posts here early in the break of day,
since i like to find out more and more.
Nice blog here! Also your web site loads up fast! What host are you using?
Can I get your affiliate link to your host? I wish my site loaded up as
quickly as yours lol
my site :: 검증사이트
Here is Affiliate link you can buy from this Link? https://www.hostg.xyz/aff_c?offer_id=6&aff_id=130985
Excellent blog here! Also your website loads up fast! What web host are you using?
Can I get your affiliate link to your host? I wish my web site loaded
up as fast as yours lol
Valuable information. Fortunate me I discovered your site unintentionally, and I am surprised why this twist
of fate did not took place in advance! I bookmarked it.
Hey there! I know this is kinda off topic but I was
wondering which blog platform are you using for this website?
I’m getting fed up of WordPress because I’ve had problems
with hackers and I’m looking at alternatives for another platform.
I would be fantastic if you could point me in the direction of a good platform.
Hello welcome to my website ! you can use php Laravel for this kind of website otherwise you can use some plugins to protect your website from hackers.
Currently it sounds like Movable Type is the preferred blogging platform out there right now.
(from what I’ve read) Is that what you’re using on your blog?
all the time i used to read smaller content which also
clear their motive, and that is also happening with this piece of writing which I am reading here.
Neat blog! Is your theme custom made or did you download it from somewhere?
A design like yours with a few simple adjustements
would really make my blog jump out. Please let me know where
you got your theme. Thank you
Hello, thanks for your comment, theme which one am using is a paid theme and theme name is news12paper.
Hello! I just would like to give you a huge thumbs up for your excellent info you’ve got
here on this post. I’ll be coming back to your website for more soon.
Excellent post. I was checking constantly this blog and I’m
impressed! Extremely useful info specifically the last part :
) I care for such information much. I was seeking this certain info for
a long time. Thank you and good luck.
I’m now not certain where you’re getting your information, however great topic.
I needs to spend a while finding out much more or figuring out
more. Thanks for great information I was searching for this info
for my mission.
Hey there just wanted to give you a quick heads up.
The text in your article seem to be running off the
screen in Ie. I’m not sure if this is a format issue or something to do with internet browser compatibility but I figured I’d
post to let you know. The design and style look great though!
Hope you get the problem fixed soon. Thanks