2016-04-03 9 views
1

iPad'in yönü değiştirildikten sonra mutlak konum belirleme gerçekleştiriyor. Sayfa ilk yüklendiğinde, konumlandırma yoktur.javascript javascript ayarı sayfası mutlak bir pozisyonda iken ilk kez yükleme

Sayfanın doğru konuma doğru yönde nasıl yükleneceğini nasıl edinebilirim? Buna

<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> 

cevap

0

İki adım:

  1. sadece kapanış </body> etiketinden önce, HTML altına senaryolarınızı (hepsi) taşıyın

    . Bu şekilde, komut dosyalarınızı indirmek sayfanızı oluşturmaya dayanmaz; ve (sorunuzla daha alakalı), harekete geçmeye çalıştıkları unsurlar, çalıştırmadan önce var olacaklar. Daha:

  2. YUI Best Practices for Speeding Up your Website sizin işleve çağrı ekleyin:

    detectIPadOrientation(); 
    

Yan notu: type özellik için gerek yok, JavaScript varsayılandır.

Yani (yorumlara bakınız):

<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>