這篇沒有什麼新奇的高科技,單純記錄我在做移動端時踩過的坑。無論是微信、支付寶還是自家 App,都會遇到這些跨平台的相容性問題。
<meta content="no-cache" http-equiv="Pragma" /> <br /> <meta content="no-cache" http-equiv="Cache-Control" /> <br /> <meta content="0" http-equiv="Expires" /> <br /> <meta content="text/html; charset=UTF-8" http-equiv="content-type" /> <br /> <meta content="IE=edge" http-equiv="X-UA-Compatible" /> <br /> <meta name="description" /> <br /> <meta name="keywords" /> <br /> <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport" /> <br /> <title></title> <br /> <!-- Set render engine for 360 browser --><br /> <meta content="webkit" name="renderer" /> <br /> <!-- No Baidu Siteapp--> <pre class="brush:java;"> </pre> <link href="。。。.css" rel="stylesheet" />
iOS 可以直接選擇相機或相簿,安卓則不行。解法是為 input 補上 capture 屬性,讓安卓也能選擇拍照或相簿;但需注意,1G 記憶體的機型可能因記憶體不足,在儲存照片時出現卡頓甚至崩潰。
當頁面有 input 輸入框,使用者輸入文字或數字時鍵盤彈出、頁面滾動,原本用 position: fixed 置底的固定欄就會出現浮動問題。差異在於:安卓的可用高度會扣掉鍵盤高度,固定欄仍穩穩貼在鍵盤上方;而 iOS 的固定欄則會固定在頁面的某個位置不動。
解法是判斷機型,在 iOS 鍵盤彈出時把固定欄改為 position: relative 或 absolute 並移到頁面後方。以下範例依賴 jQuery,適用於頁面有 input、但底部固定欄內不含 input 的情況:
<script src="js/jquery.js"></script>
<script type="text/javascript">
var u = navigator.userAgent,
app = navigator.appVersion;
var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1; //android終端或者uc浏覽器
var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios終端
if (isiOS) {
$("input").focus(function () { //是否用 $(":text"),看著用
var hbody = $(document.body).height() + 10; //浏覽器當前窗口文檔body的高度
var hwindow = $(window).height();
if (hbody > hwindow) {
$(".bottom-button").css("position", "relative");
//$(".whitespace[am-mode='50px']").css("height", "0");
} else {
$(".bottom-button").css("position", "absolute");
}
}).blur(function() { //輸入框失焦後還原初始狀態
$(".bottom-button").css("position", "fixed");
});
}
</script>
當 input 為 type="date" 且搭配 focus() 時,iOS 可以在任意 DIV 上透過 focus 直接彈出日期/時間視窗,但文字無法靠右對齊;安卓則僅在時間 input 獲得焦點、出現游標時才會觸發,未必彈窗,文字對齊行為也不一致。
解法是針對 input 撰寫樣式:
<div id="DateDiv"> <span style="text-align: right; float: right; "> <input id="txt_Date" style="min-width: 94px; text-align: right; " type="date" value="2016-02-15" />
iOS 只要限制 input 寬度就能實現靠右對齊,再用 focus 即可在任意位置彈出日期/時間視窗;安卓則要把 input 寬度設為所需大小,透過 var DatedivWidth = $("#DateDiv").width();$("#txt_Date").css("min-width", DatedivWidth); 讓它在指定寬度內才能打開彈窗。