2015-07-06 65 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)"; 
    } 
};