scrolltop with animate not working

后端 未结 6 832
梦毁少年i
梦毁少年i 2020-12-01 11:53

I\'m trying to animate while scrolling but no luck with my code...

I have this jquery

$(window).scrollTop(200);

Now wanted to give

6条回答
  •  南笙
    南笙 (楼主)
    2020-12-01 12:28

    You have to use $('html,body') instead of $(window) because window does not have a scrollTop property.

    $('#scroll-bottom').on('click', function() {
      $('html, body').animate({
        scrollTop: 2000
      }, 2000); // for all browsers
      
      // $('html').animate({scrollTop: 2000}, 2000); // works in Firefox and Chrome
      // $('body').animate({scrollTop: 2000}, 2000); // works in Safari
    })
    #top {
      margin-bottom: 2000px;
    }
    
    
    
    bottom

提交回复
热议问题