iPhone/Androidçã¢ãã¤ã«çwebkitã§ç¸¦æã¡(portrait)横æã¡(landscape)ã®cssãåãæ¿ããã
iPhone/Androidã§ã¯JavaScriptã®window.orientation;ãåå¾ã§ããã
jQueryã¹ã¿ã¤ã«ã§ã®è¨è¿°æ¹æ³ãç¨ããã¨ãä¸è¨ã®ãããªå¤§å¤ã·ã³ãã«ãªã³ã¼ãã§ããã¤ã¹ã®ç¸¦æã¡ï¼portraitï¼ã¨æ¨ªæã¡(landscape)ã§cssã®åãæ¿ããå®è£
å¯è½ã§ããã
function switchOrientation(){ var orientation = window.orientation; if(orientation == 0){ $("body").addClass("portrait"); $("body").removeClass("landscape"); }else{ $("body").addClass("landscape"); $("body").removeClass("portrait"); } } $(document).ready(function(){ switchOrientation(); var agent = navigator.userAgent; if(agent.search(/iPhone/) != -1){ window.onorientationchange = switchOrientation; }else{ window.onresize = switchOrientation; } });
ä¸è¨ã®è¨è¿°ã§bodyã¿ã°ã«ç¸¦æã¡ã®æã¯class=âportraitâãã横æã¡ã®æclass=âlandscapeâãä»å ãããã
ãã¨ã¯cssã§portraitæ/landscapeæããããã®å¤ãæå®ããã°ããã
.portrait div#contents { //ããã«ç¸¦æã¡(portrait)ã®ã¨ãã®ã¹ã¿ã¤ã«ãè¨å ¥ãä¾)width:452px; } .landscape div#contents { //ããã«æ¨ªæã¡(landscape)ã®ã¨ãã®ã¹ã¿ã¤ã«ãè¨å ¥ãä¾)width:687px; }
jQueryã®ãã£ããã³ãã¼âThe Write Less, Do Moreï¼ããå°ãªãè¨è¿°ã§ãããå¤ãã®ãã¨ãï¼âãä½ç¾ãããããªãã·ã³ãã«ãªã³ã¼ãã¨ãªã£ãã
(追è¨2011.5.9)
â Android2.3ã§ã®ä¸å
·åã«ã¤ãã¦
Android 2.3ã¨iPhoneã®ãã©ã¦ã¶ã§ã¯ãwindow.onresizeãã³ã¼ã«ããã
window.onorientationchangeã¯ã³ã¼ã«ãããªãã
2.2以ä¸ã§ã¯ãwindow.onorientationchangeãã³ã¼ã«ãããã
ãã®ãããAndroid2.3ã«å¯¾å¿ããããã«ã¯ä¸è¨ã®æ¡ä»¶åå²ãæ¸ãæããå¿
è¦ãããã
ã¨ãããã¨ã¯ãã¤ã¾ãããããªã³ã¼ãã§ããã®ã§ã¯ãªããï¼ï¼
$(function(){ $(window).resize(function(){ var orientation = window.orientation; if(orientation == 0){ $("body").addClass("portrait"); $("body").removeClass("landscape"); }else{ $("body").addClass("landscape"); $("body").removeClass("portrait"); } }); });
å ¨æ©ç¨®æ¤è¨¼ãªã©ã¯ãã¦ããªããã©ããããã§ãããã