简体   繁体   English

首次加载页面时,javascript为ipad设置绝对位置

[英]javascript setting absolute position for ipad when the page loads the first time

Absolute positioning is working after the iPad's orientation is changed. iPad的方向改变后,绝对定位正在工作。 When the page first loads there is no positioning. 当页面第一次加载时,没有定位。

How do I get the page to load with the correct position to the correct orientation? 如何使页面以正确的位置加载到正确的方向?

<script type="text/javascript">
  window.onorientationchange = detectIPadOrientation;

  function detectIPadOrientation() {
    if (orientation == 0) {
      //alert ('Portrait Mode, Home Button bottom');

      var elementStyle = document.getElementById("emkeypos").style;

      elementStyle.position = "absolute";

      elementStyle.top = "390px"
      elementStyle.left = "20px";

    } else if (orientation == 90) {
      //alert ('Landscape Mode, Home Button right');

      var elementStyle = document.getElementById("emkeypos").style;

      elementStyle.position = "absolute";

      elementStyle.top = "470px"
      elementStyle.left = "30px";

    } else if (orientation == -90) {
      //alert ('Landscape Mode, Home Button left');
      var elementStyle = document.getElementById("emkeypos").style;

      elementStyle.position = "absolute";

      elementStyle.top = "470px"
      elementStyle.left = "30px";
    } else if (orientation == 180) {
      //alert ('Portrait Mode, Home Button top');

      var elementStyle = document.getElementById("emkeypos").style;

      elementStyle.position = "absolute";

      elementStyle.top = "390px"
      elementStyle.left = "20px";
    }
  }
</script>
<div id="emkeypos">
  <a href="contest.html"><img src="#httploc#/expmeridian/assets/customer/EMkey.png" width="50px"></a>
</div>

Two steps to this: 这两个步骤:

  1. Move your scripts (all of them) to the bottom of the HTML, just before the closing </body> tag. 将您的脚本(所有脚本)移动到HTML的底部,紧靠</body>标记之前。 That way, downloading your scripts doesn't hold up rendering your page; 这样,下载脚本不会阻止渲染页面。 and (more relevant to your question) the elements they try to act on will exist before they run. (与你的问题更相关)他们试图采取行动的因素将在他们运行之前存在。 More: YUI Best Practices for Speeding Up your Website 更多: YUI加速网站的最佳实践

  2. Add a call to your function: 添加对您的函数的调用:

     detectIPadOrientation(); 

Side note: No need for the type attribute, JavaScript is the default. 附注:不需要type属性,JavaScript是默认值。

So (see comments): 因此(请参阅评论):

<div id="emkeypos"><a href="contest.html"><img src="#httploc#/expmeridian/assets/customer/EMkey.png" width="50px" ></a></div>
<!-- MOVED SCRIPTS -->
<script>
window.onorientationchange = detectIPadOrientation;
detectIPadOrientation(); // <=== ADDED CALL
function detectIPadOrientation () {
   if ( orientation == 0 ) {
        //alert ('Portrait Mode, Home Button bottom');

           var elementStyle = document.getElementById("emkeypos").style;

           elementStyle.position = "absolute";

           elementStyle.top = "390px"
           elementStyle.left = "20px";

   }
   else if ( orientation == 90 ) {
       //alert ('Landscape Mode, Home Button right');

           var elementStyle = document.getElementById("emkeypos").style;

           elementStyle.position = "absolute";

           elementStyle.top = "470px"
           elementStyle.left = "30px";

   }
   else if ( orientation == -90 ) {
    //alert ('Landscape Mode, Home Button left');
           var elementStyle = document.getElementById("emkeypos").style;

           elementStyle.position = "absolute";

           elementStyle.top = "470px"
           elementStyle.left = "30px";
   }
   else if ( orientation == 180 ) {
    //alert ('Portrait Mode, Home Button top');

           var elementStyle = document.getElementById("emkeypos").style;

           elementStyle.position = "absolute";

           elementStyle.top = "390px"
           elementStyle.left = "20px";
   }
}
</script>
</body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM