我已經基於我已閱讀過的其他腳本編寫了此腳本,並考慮到我是js/jquery的新手。js設備和方向檢測
我想在頁面加載和方向更改時檢測設備的大小和方向。
因此,我可以對每種情況應用不同的規則。
它在Android設備上效果很好,但是我發現它在ipad的縱向模式下沒有工作現在我無法弄清楚我做錯了什麼。即使在js lint我也沒有設置所有的腳本,等等。一點幫助會很好。這是我寫的代碼。
如果使用PHP
$(document).ready(function(){
var height = $(window).height();
var width = $(window).width();
if ($(window).width() < 768) {
if(width>height) {
// Smartphone Landscape Rules
var orientation = ' Landscape';
}else{
// Smartphone Portrait Rules
var orientation = ' Portrait';
}
alert ('Smartphone '+width+' - '+height+orientation);
}
if ($(window).width() > 768) {
if(width>height) {
// Tablet Landscape Rules
var orientation = ' Landscape';
}else{
// Tablet Portrait Rules
var orientation = ' Portrait';
}
alert ('Tablet '+width+'-'+height+orientation);
}
$(window).resize(function(){
var height = $(window).height();
var width = $(window).width();
alert (width+' - '+height);
if ($(window).width() < 768) {
if(width>height) {
// Smartphone Landscape Rules
var orientation = ' Landscape';
}else{
// Smartphone Portrait Rules
var orientation = ' Portrait';
}
alert ('Smartphone '+width+'-'+height+orientation);
}
if ($(window).width() > 768) {
if(width>height) {
// Tablet Landscape Rules
var orientation = ' Landscape';
}else{
// Tablet Portrait Rules
var orientation = ' Portrait';
}
alert ('Tablet '+width+'-'+height+orientation);
}
});
});
爲什麼不使用媒體查詢? –
@Camhänget指出,媒體查詢將是一個好主意。如果你使用的是jQuery Mobile,你也可以試試'$(document).pageCreate(function(){})'而不是'.ready'。 –
,因爲我沒有應用已經單獨完成的不同的CSS樣式表。該腳本將被觸發以應用不同的腳本,因爲它根據設備的大小和方向而有不同的反應。 – SmileyWar