Sunday, September 24, 2023
Hire us on Freelancer.com
HomejQueryHow To Create Scroll Back To Top Button

How To Create Scroll Back To Top Button

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!

RELATED ARTICLES

18 COMMENTS

  1. 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.

  2. 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.

  3. 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

  4. 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.

  5. 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.

  6. 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

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Hire Us

Categories