三種檢測iPhone/iPad設備方向的方法
更新時間:2014年04月23日 15:37:31 作者:
這篇文章主要介紹了檢測iPhone/iPad設備方向的三種方法,需要的朋友可以參考下
使用meta tag "viewport"
viewport標簽包含如下屬性:
為了能自動探測并適配到屏幕寬度,應該使用device-with而不是設定一個固定值,另外為了避免用戶縮放導致界面超出屏幕,需要設置maximum-scale,
<meta name="viewport" content="width=device-width, maximum-scale=1.0" />
使用javascript腳本
下面的腳本通過檢測屏幕寬度來檢測方向并調整方向:
<script type="text/javascript">
var updateLayout = function() {
if (window.innerWidth != currentWidth) {
currentWidth = window.innerWidth;
var orient = (currentWidth == 320) ? "profile" : "landscape";
document.body.setAttribute("orient", orient);
window.scrollTo(0, 1);
}
};
iPhone.DomLoad(updateLayout);
setInterval(updateLayout, 400);
</script>
上述腳本可放在head部分
使用CSS
使用CSS的media query:
<link rel="stylesheet" media="all and (orientation:portrait)" href="portrait.css">
<link rel="stylesheet" media="all and (orientation:landscape)" href="landscape.css">
viewport標簽包含如下屬性:
為了能自動探測并適配到屏幕寬度,應該使用device-with而不是設定一個固定值,另外為了避免用戶縮放導致界面超出屏幕,需要設置maximum-scale,
復制代碼 代碼如下:
<meta name="viewport" content="width=device-width, maximum-scale=1.0" />
使用javascript腳本
下面的腳本通過檢測屏幕寬度來檢測方向并調整方向:
復制代碼 代碼如下:
<script type="text/javascript">
var updateLayout = function() {
if (window.innerWidth != currentWidth) {
currentWidth = window.innerWidth;
var orient = (currentWidth == 320) ? "profile" : "landscape";
document.body.setAttribute("orient", orient);
window.scrollTo(0, 1);
}
};
iPhone.DomLoad(updateLayout);
setInterval(updateLayout, 400);
</script>
上述腳本可放在head部分
使用CSS
使用CSS的media query:
復制代碼 代碼如下:
<link rel="stylesheet" media="all and (orientation:portrait)" href="portrait.css">
<link rel="stylesheet" media="all and (orientation:landscape)" href="landscape.css">
相關文章
JavaScript中null和undefined的區(qū)別詳解
null 是一個原始值,表示“無”或“空值”,它通常用于指示變量應有的對象或值不存在,undefined 是一個原始值,表示“未定義”,本文給大家詳細介紹了JavaScript中null和undefined的區(qū)別,需要的朋友可以參考下2024-10-10
js中數(shù)組結合字符串實現(xiàn)查找(屏蔽廣告判斷url等)
這篇文章主要介紹了js中數(shù)組結合字符串實現(xiàn)查找(屏蔽廣告判斷url等),需要的朋友可以參考下2016-03-03

