user6155249 user6155249 - 1 month ago 14
HTML Question

Animate scroll when button taped

Good Evening,

I have been trying to animate a scroll-feature when tapping on a button. Below you will find the code that isn't working, my question is why.

$(document).ready(function () {
$('a[href="#panel-body"]').on('click', function (event) {
if(this.hash !== "") {
event.preventDefault();
var hash = this.hash;

$("html, body").animate({
scrollTop: $(hash).offset().top
}, 800 function () {
window.location.hash = hash;
});
}
});
});


I have assigned a link with the href="#panel-body". Tried with different variants and searched for a solution, still haven't managed to solve it.

Thanks for the help in advance!

Answer

I just found out that you want to create Up Going Button, For do this use the code below :

$(document).ready(function () {
    $('button').click(function () {
        $('html, body').animate({
            scrollTop: '0px'
        }, 750);
    });     
});