How to scroll to element cross browser using jquery animate

佐手、 提交于 2019-12-07 15:26:21

问题


This code:

jQuery('body').animate({scrollTop: target.offset().top}, 300);

Works in firefox, but not chrome.

This code:

jQuery('html').animate({scrollTop: target.offset().top}, 300);

Works in chrome, but not firefox.

I haven't tested yet in IE.

What is the correct way to do this, cross-browser? If it's not clear from the above snippets, I target is a div on the page, and I want to scroll down slowly to it slowly, so they do exactly what I want... just not cross-browser.


回答1:


Specify both html and body:

$("html,body").animate({scrollTop: target.offset().top}, 300);


来源:https://stackoverflow.com/questions/15664799/how-to-scroll-to-element-cross-browser-using-jquery-animate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!