Scroll to top button with animation HTML CSS

Code Snippets 4 U

Class for the button:

.scroll-top-button {
    position: fixed;
    right: 50px;
    bottom: 47px;
    width: 45px;
    height: 45px;
    -webkit-animation: pulse 2s infinite;
    animation: pulse 2s infinite;
    -webkit-transition: all .5s ease-in-out;
    transition: all .5s ease-in-out;
    border: 0;
    border-radius: 50%;
    outline: none;
    background-color: #ed3e44;
    opacity: 0;
    color: #fff;
    font-size: 18px;
    cursor: pointer;
    -webkit-box-shadow: 0 0 0 rgba(204,169,44,0.4);
    box-shadow: 0 0 0 rgba(204,169,44,0.4);
    z-index: 99;
}

HTML Element:

<div class="scroll-top-button active" id="scroll-top-button"></div>

Media queries : (As per your requirements)

@media (min-width: 1200px)
.scroll-top-button {
    bottom: 62px;
}

Active CSS class

.active {
    opacity: 1;
}

After and before CSS:

.scroll-top-button::after {
    left: 19px;
    -webkit-transform: rotate(45deg);
    transform: rotate(45deg);
}

.scroll-top-button::after, .scroll-top-button::before {
    position: absolute;
    top: 45%;
    width: 41%;
    height: 2px;
    background: #fff;
    content: '';
}

.scroll-top-button::before {
    left: 7px;
    -webkit-transform: rotate(-45deg);
    transform: rotate(-45deg);
}
.scroll-top-button::after, .scroll-top-button::before {
    position: absolute;
    top: 45%;
    width: 41%;
    height: 2px;
    background: #fff;
    content: '';
}

Pulse animation keyframes:

@keyframes pulse {
 0% {
     -webkit-box-shadow: 0 0 0 0 rgba(204,17,20,0.4);
     box-shadow: 0 0 0 0 rgba(204,17,20,0.4);
 }
 70% {
     -webkit-box-shadow: 0 0 0 10px rgba(204,169,44,0);
     box-shadow: 0 0 0 10px rgba(204,169,44,0);
 }
 100% {
     -webkit-box-shadow: 0 0 0 0 rgba(204,169,44,0);
     box-shadow: 0 0 0 0 rgba(204,169,44,0);
 }
}

Now you need to listen to scroll event and after some specific scroll length you need to apply active class.

Leave a Reply

Your email address will not be published. Required fields are marked *

five + four =