Friends

onorientationchange设备兼容性处理

(function(){  
    var supportOrientation=(typeof window.orientation == "number" && typeof window.onorientationchange == "object");  

    var updateOrientation=function(){  
        if(supportOrientation){  
            updateOrientation=function(){  
                var orientation=window.orientation;  
                switch(orientation){  
                    case 90:  
                    case -90:  
                        orientation="landscape";  
                        break;  
                    default:  
                        orientation="portrait";  
                }  
                document.body.parentNode.setAttribute("class",orientation);  
            };  
        }else{  
            updateOrientation=function(){  
                var orientation=(window.innerWidth > window.innerHeight)? "landscape":"portrait";  
                document.body.parentNode.setAttribute("class",orientation);  
            };  
        }  
        updateOrientation();  
    };  

    var init=function(){  
        updateOrientation();  
        if(supportOrientation){  
            window.addEventListener("orientationchange",updateOrientation,false);  
        }else{      
            window.setInterval(updateOrientation,5000); 
        }  
    };  

    window.addEventListener("DOMContentLoaded",init,false);  
})();  

Written on December 17, 2014