Mobile Web開發(fā)基礎(chǔ)之四--處理手機(jī)設(shè)備的橫豎屏問(wèn)題
為了應(yīng)對(duì)移動(dòng)設(shè)備屏幕的碎片化,我們?cè)陂_發(fā)Mobile Web應(yīng)用時(shí),一個(gè)最佳實(shí)踐就是采用流式布局,保證最大可能地利用有限的屏幕空間。由于屏幕存在著方向性,用戶在切換了屏幕的方向后,有些設(shè)計(jì)上或?qū)崿F(xiàn)上的問(wèn)題就會(huì)凸顯——我們至少需要處理一下當(dāng)前顯示元素的寬度的適配(當(dāng)然,要做的可能不僅僅是這個(gè))。很多時(shí)候,我們需要為不同的屏幕方向來(lái)設(shè)計(jì)對(duì)應(yīng)的應(yīng)用顯示模式,這個(gè)時(shí)候,實(shí)時(shí)地獲知設(shè)備的模豎屏狀態(tài)就顯得極為重要。
- window.orientation屬性與onorientationchange事件
window.orientation :這個(gè)屬性給出了當(dāng)前設(shè)備的屏幕方向,0表示豎屏,正負(fù)90表示橫屏(向左與向右)模式
onorientationchange : 在每次屏幕方向在橫豎屏間切換后,就會(huì)觸發(fā)這個(gè)window事件,用法與傳統(tǒng)的事件類似
1:使用onorientationchange事件的回調(diào)函數(shù),來(lái)動(dòng)態(tài)地為body標(biāo)簽添加一個(gè)叫orient的屬性,同時(shí)以body[orient=landspace]或body[orient=portrait]的方式在css中定義對(duì)應(yīng)的樣式,這樣就可以實(shí)現(xiàn)在不同的屏幕模式下顯示不同的樣式。如下代碼示例:
<!Doctype html>
<html>
<head>
<meta charset="utf-8">
<meta id="viewport" name="viewport" content="width=device-width,initial-scale=1.0;">
<title>橫豎屏切換檢測(cè)</title>
<style type="text/css">
body[orient=landscape]{
background-color: #ff0000;
}
body[orient=portrait]{
background-color: #00ffff;
}
</style>
</head>
<body orient="landspace">
<div>
內(nèi)容
</div>
<script type="text/javascript">
(function(){
if(window.orient==0){
document.body.setAttribute("orient","portrait");
}else{
document.body.setAttribute("orient","landscape");
}
})();
window.onorientationchange=function(){
var body=document.body;
var viewport=document.getElementById("viewport");
if(body.getAttribute("orient")=="landscape"){
body.setAttribute("orient","portrait");
}else{
body.setAttribute("orient","landscape");
}
};
</script>
</body>
</html>
2: 類似的思路,不通過(guò)CSS的屬性選擇器來(lái)實(shí)現(xiàn),如下代碼的實(shí)現(xiàn)方案:
<!Doctype html>
<html>
<head>
<meta charset="utf-8">
<meta id="viewport" name="viewport" content="width=device-width,initial-scale=1.0;">
<title>橫豎屏切換檢測(cè)</title>
<style type="text/css">
.landscape body {
background-color: #ff0000;
}
.portrait body {
background-color: #00ffff;
}
</style>
</head>
<body orient="landspace">
<div>
內(nèi)容
</div>
<script type="text/javascript">
(function(){
var init=function(){
var updateOrientation=function(){
var orientation=window.orientation;
switch(orientation){
case 90:
case -90:
orientation="landscape";
break;
default:
orientation="portrait";
break;
}
document.body.parentNode.setAttribute("class",orientation);
};
window.addEventListener("orientationchange",updateOrientation,false);
updateOrientation();
};
window.addEventListener("DOMContentLoaded",init,false);
})();
</script>
</body>
</html>
- 使用media query方式
這是一種更為方便的方式,使用純CSS就實(shí)現(xiàn)了對(duì)應(yīng)的功能,如下代碼演示:
<!Doctype html>
<html>
<head>
<meta charset="utf-8">
<meta id="viewport" name="viewport" content="width=device-width,initial-scale=1.0;">
<title>橫豎屏切換檢測(cè)</title>
<style type="text/css">
@media all and (orientation : landscape) {
body {
background-color: #ff0000;
}
}
@media all and (orientation : portrait){
body {
background-color: #00ff00;
}
}
</style>
</head>
<body>
<div>
內(nèi)容
</div>
</body>
</html>
- 低版本瀏覽器的平穩(wěn)降級(jí)
如果目標(biāo)移動(dòng)瀏覽器不支持media query,同時(shí)window.orientation也不存在,則我們需要采用另外一種方式來(lái)實(shí)現(xiàn)————使用定時(shí)器“偽實(shí)時(shí)”地對(duì)比當(dāng)前窗口的高(window.innerHeight)與寬(window.innerWidth)之比,從而判定當(dāng)前的橫豎屏狀態(tài)。如下代碼所示:
<!Doctype html>
<html>
<head>
<meta charset="utf-8">
<meta id="viewport" name="viewport" content="width=device-width,initial-scale=1.0;">
<title>按鍵</title>
<style type="text/css">
.landscape body {
background-color: #ff0000;
}
.portrait body {
background-color: #00ffff;
}
</style>
<script type="text/javascript">
(function(){
var updateOrientation=function(){
var orientation=(window.innerWidth > window.innerHeight)? "landscape" : "portrait";
document.body.parentNode.setAttribute("class",orientation);
};
var init=function(){
updateOrientation();
window.setInterval(updateOrientation,5000);
};
window.addEventListener("DOMContentLoaded",init,false);
})();
</script>
</head>
<body>
<div>
內(nèi)容
</div>
</body>
</html>
- 統(tǒng)一解決方案
將以上的兩種解決方案整合在一起,就可以實(shí)現(xiàn)一個(gè)跨瀏覽器的解決方案,如下代碼:
<!Doctype html>
<html>
<head>
<meta charset="utf-8">
<meta id="viewport" name="viewport" content="width=device-width,initial-scale=1.0;">
<title>橫豎屏切換檢測(cè)</title>
<style type="text/css">
.landscape body {
background-color: #ff0000;
}
.portrait body {
background-color: #00ffff;
}
</style>
<script type="text/javascript">
(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);
})();
</script>
</head>
<body>
<div>
內(nèi)容
</div>
</body>
</html>
原英文網(wǎng)址:http://davidbcalhoun.com/2010/dealing-with-device-orientation
以上所述是小編給大家介紹的Mobile Web開發(fā)基礎(chǔ)之四--處理手機(jī)設(shè)備的橫豎屏問(wèn)題,希望對(duì)大家有所幫助!
相關(guān)文章
JavaScript子窗口ModalDialog中操作父窗口對(duì)像
在使用js中會(huì)碰到這樣的需求:利用子窗口操作父窗口對(duì)像,本人很是遺憾,于是搜索整理下,拿出來(lái)和大家分享,需要的朋友可以參考下2012-12-12
JavaScript 學(xué)習(xí)筆記之語(yǔ)句
這篇文章主要介紹了JavaScript中的語(yǔ)句,包括條件分支語(yǔ)句、循環(huán)語(yǔ)句、迭代語(yǔ)句、Lable語(yǔ)句、break和continue語(yǔ)句、with語(yǔ)句、swith語(yǔ)句,十分全面細(xì)致,推薦給小伙伴們。2015-01-01
從零學(xué)JSON之JSON數(shù)據(jù)結(jié)構(gòu)
這篇文章主要介紹了JSON數(shù)據(jù)結(jié)構(gòu)的相關(guān)知識(shí),需要的朋友可以參考下2014-05-05
JavaScript數(shù)組的快速克隆(slice()函數(shù))和數(shù)組的排序、亂序和搜索(sort()函數(shù))
JavaScript數(shù)組的快速克隆(slice()函數(shù))和數(shù)組的排序、亂序和搜索(sort()函數(shù))...2006-12-12
js setTimeout 常見問(wèn)題小結(jié)
主要包括this指向問(wèn)題、向setTimeout傳入?yún)?shù)等相關(guān)問(wèn)題,下面與大家分享下以上問(wèn)題的解決方法,感興趣的朋友可以參考下2013-08-08

