最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Scroll to top not working for safari - Stack Overflow

programmeradmin9浏览0评论

I am calling this javascript on page load

document.body.scrollTop = document.documentElement.scrollTop = 0;

I want html page starts from top when loaded, above javascript working fine for chrome, safari(macbook) but not working for safari on iphone, can anybody suggest workaround in javascript only.

I am calling this javascript on page load

document.body.scrollTop = document.documentElement.scrollTop = 0;

I want html page starts from top when loaded, above javascript working fine for chrome, safari(macbook) but not working for safari on iphone, can anybody suggest workaround in javascript only.

Share Improve this question edited Nov 17, 2016 at 5:17 Lalji Gajera asked Nov 16, 2016 at 15:03 Lalji GajeraLalji Gajera 4711 gold badge5 silver badges10 bronze badges 3
  • Have you tried stackoverflow./a/4210804/1693915 ? – Maurice Klimek Commented Jan 9, 2017 at 13:00
  • I will give it a try, but why it is not working with above pure javascript? – Lalji Gajera Commented Feb 9, 2017 at 10:43
  • Hi @LaljiGajera I just tested this on Xcode's simulator in oldest model (iPhone 5) and it did work. But things like window.safari flag checking didn't. – Niloct Commented Nov 5, 2018 at 23:12
Add a ment  | 

2 Answers 2

Reset to default 4

Posting this here for future visitors from Google. This same error occured to me... The problem was just one CSS rule on body element.

overflow-x: hidden

If this CSS rule exists, remove it and scrollTop should work on mobile Safari.

As I tested this before, there are problems rendering on ios, but it also depends on the version of the OS for example, for an iPad pro, there is no problem! But on iPad air you there isn't much to do! so what you can do is to make your code patible like:

document.scrollingElement.scrollTop

it should give you the right solution.

发布评论

评论列表(0)

  1. 暂无评论