我一直在寻找一些方法来检测iPad3和iPad4设备。我正在动态添加元视口。我想查看桌面版本并删除桌面、iPad3和iPad4中的视口,但在移动设备中当然添加视口以查看移动版本。
请检查我在 var ApplyViewPort 中的初始化函数:
var deviceDetection = function () {
var osVersion,
device,
deviceType,
userAgent,
isSmartphoneOrTablet;
device = (navigator.userAgent).match(/Android|iPhone|iPad|iPod/i);
if ( /Android/i.test(device) ) {
if ( !/mobile/i.test(navigator.userAgent) ) {
deviceType = 'tablet';
} else {
deviceType = 'phone';
}
osVersion = (navigator.userAgent).match(/Android\s+([\d\.]+)/i);
osVersion = osVersion[0];
osVersion = osVersion.replace('Android ', '');
} else if ( /iPhone/i.test(device) ) {
deviceType = 'phone';
osVersion = (navigator.userAgent).match(/OS\s+([\d\_]+)/i);
osVersion = osVersion[0];
osVersion = osVersion.replace(/_/g, '.');
osVersion = osVersion.replace('OS ', '');
} else if ( /iPad/i.test(device) ) {
deviceType = 'tablet';
osVersion = (navigator.userAgent).match(/OS\s+([\d\_]+)/i);
osVersion = osVersion[0];
osVersion = osVersion.replace(/_/g, '.');
osVersion = osVersion.replace('OS ', '');
}
isSmartphoneOrTablet = /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent);
userAgent = navigator.userAgent;
return { 'isSmartphoneOrTablet': isSmartphoneOrTablet,
'device': device,
'osVersion': osVersion,
'userAgent': userAgent,
'deviceType': deviceType
};
}();
var ApplyViewPort = {
init: function() {
this.metaView = '<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />';
/// Preppend a meta viewport if it's not yet preppended
/// Else remove the meta viewport
if (deviceDetection.deviceType == 'tablet') {
$('meta[name="viewport"]').remove();
if (window.devicePixelRatio == 2) {
$('meta[name="viewport"]').remove();
}
} else {
$('head').append(this.metaView);
}
}
}
$(document).ready(function() {
ApplyViewPort.init();
});