iOS方向更改缩放错误修复

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS方向更改缩放错误修复相关的知识,希望对你有一定的参考价值。

This is a simple fix for the ios zoom bug that happens when changing screen orientations.
  1. /*! A fix for the iOS orientationchange zoom bug.
  2.  Script by @scottjehl, rebound by @wilto.
  3.  MIT License.
  4. */
  5. (function(w){
  6. // This fix addresses an iOS bug, so return early if the UA claims it's something else.
  7. if( !( /iPhone|iPad|iPod/.test( navigator.platform ) && navigator.userAgent.indexOf( "AppleWebKit" ) > -1 ) ){ return; }
  8. var doc = w.document;
  9. if( !doc.querySelector ){ return; }
  10. var meta = doc.querySelector( "meta[name=viewport]" ),
  11. initialContent = meta && meta.getAttribute( "content" ),
  12. disabledZoom = initialContent + ",maximum-scale=1",
  13. enabledZoom = initialContent + ",maximum-scale=10",
  14. enabled = true,
  15. x, y, z, aig;
  16. if( !meta ){ return; }
  17. function restoreZoom(){
  18. meta.setAttribute( "content", enabledZoom );
  19. enabled = true; }
  20. function disableZoom(){
  21. meta.setAttribute( "content", disabledZoom );
  22. enabled = false; }
  23. function checkTilt( e ){
  24. aig = e.accelerationIncludingGravity;
  25. x = Math.abs( aig.x );
  26. y = Math.abs( aig.y );
  27. z = Math.abs( aig.z );
  28. // If portrait orientation and in one of the danger zones
  29. if( !w.orientation && ( x > 7 || ( ( z > 6 && y < 8 || z < 8 && y > 6 ) && x > 5 ) ) ){
  30. if( enabled ){ disableZoom(); } }
  31. else if( !enabled ){ restoreZoom(); } }
  32. w.addEventListener( "orientationchange", restoreZoom, false );
  33. w.addEventListener( "devicemotion", checkTilt, false );
  34. })( this );

以上是关于iOS方向更改缩放错误修复的主要内容,如果未能解决你的问题,请参考以下文章

如何在屏幕方向更改时附加片段?

方向更改时不可见的片段

方向更改后片段中的 getActivity() 为空

如何在 balkangraph orgchart 中“修复缩放功能”错误?

如何防止在方向更改时重新创建片段寻呼机中的片段?

在方向更改时自动缩放 UIView 以按比例缩放以适合父视图