detectDeviceType() {
// 检测是否为移动设备
const isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
const screenWidth = window.innerWidth;
const screenHeight = window.innerHeight;
// 判断设备类型
const deviceType = isMobile ? '移动端' : '桌面端';
console.log(`设备类型:${deviceType}`);
this.isMobile = isMobile;
// 判断屏幕宽度
const isScreenWidthInRange = screenWidth >= 375 && screenWidth <= 450;
console.log(isScreenWidthInRange ? "屏幕宽度在 375px 到 450px 之间" : "屏幕宽度不在 375px 到 450px 之间");
console.log(isMobile,
deviceType,
screenWidth,
screenHeight,
isScreenWidthInRange);
// 可选:返回一个对象,包含所有检测结果
return {
isMobile,
deviceType,
screenWidth,
screenHeight,
isScreenWidthInRange
};
}