Skip to content
</> Mücahit TopalA developer's notebook

Blog/Html Css Js

Scroll Top - A Back-to-Top Button for Your Page (jQuery)

Mücahit Topal 1 min read 5,163 views TREN

On a page made up of a long piece of writing, getting back to the top after scrolling down can be a hassle at times. You can easily get around this with a button that stays hidden while you are at the top of the page and appears on the right side once you start scrolling down.

First, let's specify the icon file to be used 


Then let's add our style to our CSS file 

It should sit in the bottom right corner and be invisible

#ScrollTop{
    position: fixed;
    right: 20px;
    bottom: 15px;
    cursor: pointer;
    display: none;
}

When the icon is clicked, let's scroll the page to the top 

$("#ScrollTop").click(function() {
    $("html,body").stop().animate({ scrollTop: "0" }, 1000);
});

 

The icon should become invisible when the page is at the top and become visible when it is scrolled down 

$(window).scroll(function() {
    var length = $(document).scrollTop();
    if (length > 300){
        $("#ScrollTop").fadeIn(500);
    }
    else{
        $("#ScrollTop").fadeOut(500);
    }
});

 

,,,,

Share X Facebook LinkedIn WhatsApp

More from this category

Comments 2

Osman Ökkeş

<a name= 

özelliği ile js kullanmadan daha basit bir şekilde hazırlayamaz mıydık?

admin

Tabiki hazırlayabilirdik ama js kullandığımızda geçiş hızını ayarlayabiliriz , kimin nasıl kolayına geliyorsa :)

Leave a comment

Comments appear after approval.