1window.scroll({top: 0, left: 0});
2//Or with jQuery
3$('html,body').scrollTop(0);
4//with animation
5$('html, body').animate({ scrollTop: 0 }, 'fast');
1//EITHER:
2
3window.scroll({
4 top: 0,
5 left: 0,
6 behavior: 'smooth'
7});
8
9//OR:
10
11window.scroll({top: 0, left: 0});
12//Or with jQuery
13$('html,body').scrollTop(0);
14//with animation
15$('html, body').animate({ scrollTop: 0 }, 'fast');
1 $("#scroll_icon").click(function()
2 {
3 jQuery('html,body').animate({scrollTop:0},2000);
4 })
1// Select all »a« elements with a parent tag »nav« and add a function that is executed on click
2$('#id a').on('click', function (e) {
3
4 // Define variable of the clicked »a« element (»this«) and get its href value.
5 var href = $(this).attr('href')
6
7 // Run a scroll animation to the position of the element which has the same id like the href value.
8 $('html, body').animate({
9 scrollTop: $(href).offset().top
10 }, '300')
11
12 // Prevent the browser from showing the attribute name of the clicked link in the address bar
13 e.preventDefault()
14
15})