当移动应用程序页面方向从横向变为纵向或者相反方向时,改变CSS文件的最佳方法是什么?我需要支持Android和iPhone。似乎媒体查询不是最干净的方法,还有其他的想法吗?
当移动应用程序页面方向从横向变为纵向或者相反方向时,改变CSS文件的最佳方法是什么?我需要支持Android和iPhone。似乎媒体查询不是最干净的方法,还有其他的想法吗?
示例
/* For portrait */
@media screen and (orientation: portrait) {
#toolbar {
width: 100%;
}
}
/* For landscape */
@media screen and (orientation: landscape) {
#toolbar {
position: fixed;
width: 2.65em;
height: 100%;
}
p {
margin-left: 2em;
}
}
更多详细信息请参见此处
。$(document).ready(function(){
// The event for orientation change
var onChanged = function() {
// The orientation
var orientation = window.orientation;
if(orientation == 90) {
$('#cssElement').attr('href', '/path/to/landscape.css');
} else {
$('#cssElement').attr('href', '/path/to/portrait.css');
}
};
// Bind the orientation change event and bind onLoad
$(window).bind(orientationEvent, onChanged).bind('load', onChanged);
});
window.orientation
是一个数字,而不是一个字符串。 - icktoofay$(document).ready(function() {
$(window).resize(function() {
alert(window.orientation);
});
});
你可以使用 window.onorientationchange 事件。