desirelabs
3/9/2015 - 2:06 PM

Scroll to top

Scroll to top

.totop {
    width: 60px;
    height: 60px;
    background: url("totop.png") no-repeat 0 0;
    position: fixed;
    bottom: 30px;
    right: 30px;
    display: none;
    cursor: pointer;
}
$(document).ready(function () {
    // Append a totop div element to body
    $('body').append('<div class="totop"></div>');
    
    // Trigger scrolling event
    $(window).scroll(function () {
        if ($(this).scrollTop() > 100) {
            $('.totop').fadeIn();
        } else {
            $('.totop').fadeOut();
        }
    });

    // Trigger click on the button
    $('body').on('click', '.totop', function () {
        $("html, body").animate({
            scrollTop: 0
        }, 600);
        return false;
    });

});