0
$(".myClass").click(function() {        
    window.location.replace("#goToDiv");        
});

My code is in here. When i click .myClass button, the user goes to #goToDiv. But I want the animation for this. For example, it can go like transitive passing. How can I use animation for this?

Minal Chauhan
  • 6,025
  • 8
  • 21
  • 41
Engin A
  • 9
  • 1
  • Does this answer your question? [Animate scroll to ID on page load](https://stackoverflow.com/questions/6682451/animate-scroll-to-id-on-page-load) – Mohammad Jan 28 '21 at 11:42

2 Answers2

0

$('a[href^="#"]').on('click', function(e) {
  e.preventDefault();
  var target = this.hash;
  var $target = $(target);
  $('html, body').stop().animate({
    'scrollTop': $target.offset().top
  }, 900, 'swing', function() {
    window.location.hash = target;
  });
});
Gutelaunetyp
  • 2,144
  • 4
  • 15
  • 40
0

You can use jQuery .animate() function with scrollTop.

Use this:

$(".myClass").click(function() {        
    $('html, body').animate({
        scrollTop: $("#goToDiv").offset().top
    }, 1200);      
});
Yasin BARAN
  • 239
  • 3
  • 11