jQuery的scrollTop不工作在iOS的iframe中

iOS和iframe ..这样的痛苦。 我有一个简单的回到顶部button应该animation滚动(而不是跳转到页面的顶部)。

$(document).on('click touchstart', '.backtotop', function() { $('html, body').animate({ scrollTop: 0 }, 1500); }); 

除了iOS上的iframe之外,这个function无处不在。 我还没有完全理解iOS如何处理iframe。 jQuery的.scrollTop()函数也不会工作(无论如何不能animation)。

在iOS上的iframe的唯一工作是这样的:

 parent.self.scrollTo(0, 0); 

显然不是最好的解决scheme,因为这不适用于桌面浏览器。 任何有关如何解决这个问题或iOS上的iframe一般深入的知识是非常感谢。

看起来像特定的上下文修复了这个问题:

 $('body, html', parent.document).animate({ scrollTop: 0 },1500); 

由于这将只适用于iOS我已经包括从这个线程的iOS检测:

 var iOS = ( navigator.userAgent.match(/(iPad|iPhone|iPod)/g) ? true : false ); $(document).on('click touchstart', '.backtotop', function() { if (iOS) { $('html, body', parent.document).animate({ scrollTop: $("body").offset().top},1500,"easeOutQuart"); } else { $('html, body').animate({ scrollTop: $("body").offset().top},1500,"easeOutQuart"); } }); 

显然只有parent.document作为上下文。 parent.window或只有文档没有影响。

这适用于运行ios 9.2.1的iPhone 6 Plus