2015-07-06 60 views

回答

1

非jQuery的方式。除非你真的需要,否則不要增加90kb的頁面重量!

window.addEventListener('resize', function(){ 
    if (window.innerHeight < window.innerWidth){ 
     document.body.style.transform = "rotate(90deg)"; 
     document.body.style.webkitTransform = "rotate(90deg)"; 
     document.body.style.mozTransform = "rotate(90deg)"; 
    } 
}); 

另外,要小心這個用戶體驗。除非他們期待,否則它可能會嚴重地惹惱用戶。這是絕對必要的嗎?

0
$(window).resize(function(){ 
    if($(this).height() < $(this).width()){ 
      $("body").css("transform","rotate(90deg)"); 
    } 
}); 

DOC:https://api.jquery.com/resize/

而且不要忘了添加jQuery庫。

<script src="//code.jquery.com/jquery-1.11.3.min.js"></script> 

編輯:

,如果你只是使用的javascript:

window.onresize = function(e) { 
    var w = this.innerWidth; 
    var h = this.innerHeight; 
    if(h < w){ 
      document.getElementsByTagName("body")[0].style.transform = "rotate(90deg)"; 
    } 
};