1.css如何判斷手機橫屏?javascript
媒體查詢:css
@media all and (orientation : landscape) { java
h2{color:red;}/*橫屏時字體紅色*/jquery
} android
@media all and (orientation : portrait){ ios
h2{color:green;}/*豎屏時字體綠色*/瀏覽器
} 安全
2.jquery判斷設備類型app
//判斷設備類型 $("#aass").click(function() { var browser = { versions: function() { var u = navigator.userAgent, app = navigator.appVersion; return { mobile: !!u.match(/AppleWebKit.*Mobile.*/), ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), android: u.indexOf("Android") > -1 || u.indexOf("Linux") > -1, iPhone: u.indexOf("iPhone") > -1, iPad: u.indexOf("iPad") > -1 }; } (), language: (navigator.browserLanguage || navigator.language).toLowerCase() }; $("#aa2").append("userAgent 內容: <BR/>" + navigator.userAgent + "<BR/><BR/>"); $("#aa2").append("是否爲移動終端: " + browser.versions.mobile + "<BR/>"); $("#aa2").append("是否 ios: " + browser.versions.ios + "<BR/>"); $("#aa2").append("是否 android: " + browser.versions.android + "<BR/>"); //判斷 瀏覽器 類型 var ex = navigator.userAgent, bUA; if (ex.indexOf("MSIE") >= 0) { bUA = "Internet Explorer 10 Or Earlier"; //IE } else if (ex.indexOf("Firefox") >= 0) { bUA = "Firefox"; } else if (ex.indexOf("Chrome") >= 0) { bUA = "Chrome"; if (ex.indexOf("360SE") >= 0) { //360安全瀏覽器 bUA = "360SE"; } else if (ex.indexOf("360EE") >= 0) { //360急速瀏覽器 bUA = "360EE"; } else if (ex.indexOf("SE") >= 0 && ex.indexOf("360SE") == -1) { //搜狗瀏覽器 bUA = "SouGou"; } else if (ex.indexOf("Maxthon") >= 0) { //遨遊瀏覽器 bUA = "Maxthon"; } } else if (ex.indexOf("UCBrowser") >= 0 || ex.indexOf("UCWEB") >= 0) { // UC瀏覽器 bUA = "UCBrowser"; } else if (ex.indexOf("Opera") >= 0) { bUA = "Opera"; } else if (ex.indexOf("Safari") >= 0) { // 蘋果瀏覽器 bUA = "Safari"; } else if (ex.indexOf("Netscape") >= 0) { bUA = "Netscape"; } else if (ex.indexOf("like Gecko") >= 0 && ex.indexOf("Trident") >= 0) { bUA = "Internet Explorer 11 Or Later"; //IE11 之後,再也不用 MSIE } else { bUA = "Other Broswer"; } $("#aa2").append("當前瀏覽器: " + bUA + "<BR/>"); });