Josh Williams
Josh Williams

Reputation: 1

Smooth scrollTop button onclick

Not sure why my scrollTop function isn't working on the button click. I'm modifying a smart wizard so that if you're at the bottom of the page, it will move you up to the top when you move to the next stage.

$("#next-btn").on("click", function () {
    // Navigate next
    $('#smartwizard').smartWizard("next");
    var body = $('html, body');
    var scrollPos = body.scrollTop()

    if(scrollPos != 0){
        body.animate({scrollTop:0}, '600');
    }
    return true;
});

Any help is appreciated. Sorry if I'm being stupid kinda new to this.

Upvotes: 0

Views: 158

Answers (1)

Fadi Quader
Fadi Quader

Reputation: 153

try this

/*------------------------------------------------------------------------------*/
/* Scroll to Top
 /*------------------------------------------------------------------------------*/
$(window).scroll(function() { tm_scroll_to_top(); });
$(window).resize(function() { tm_scroll_to_top(); });
$(window).load(function()   { tm_scroll_to_top(); });
var duration = 500;
var offset   = 85;
$("#totop").click(function(event) {
    event.preventDefault();
    $("html, body").animate({scrollTop: 0}, duration);
    return false;
});

function tm_scroll_to_top(){
    if ($(window).scrollTop() > offset) {
        $("#totop").fadeIn(duration);
    } else {
        $("#totop").fadeOut(duration);
    }
}

Upvotes: 0

Related Questions