我想检测用户在我的应用程序上使用移动设备的情况。
通常,我们会通过使用用户代理或可触摸屏幕来进行检测,但是在开发工具中的Chrome移动模式会更改这两个选项。
您有什么办法可以检测移动设备或桌面设备,并且不会被Chrome开发工具欺骗吗?
我想检测用户在我的应用程序上使用移动设备的情况。
通常,我们会通过使用用户代理或可触摸屏幕来进行检测,但是在开发工具中的Chrome移动模式会更改这两个选项。
您有什么办法可以检测移动设备或桌面设备,并且不会被Chrome开发工具欺骗吗?
与其根据设备类型进行选择,您可以检查操作系统。 这里 是可以使用的可能值列表。
windowsPlatforms = ['Win32', 'Win64', 'Windows', 'WinCE'];
android = "Android";
typeOfOS = window.navigator.platform;
if (windowsPlatforms.includes(typeOfOS)) {
//do for windows
} else if(typeOfOS === android){
//do for android
}
const isMobile = 'ontouchstart' in window || navigator.maxTouchPoints > 0;
body {
--background-image: url('https://thumbs.dreamstime.com/b/vertical-banner-template-hands-typing-computer-office-supplies-distance-education-e-learning-obtaining-university-156390588.jpg');
margin: 0;
padding: 0;
box-sizing: border-box;
background-image: var(--background-image);
background-size: 100vw 100vh;
z-index: -1000;
}
#submit-form {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
height: 180px;
width: 300px;
background-color: white;
border-radius: 10px;
border: 2px dashed;
display: flex;
flex-direction: column;
align-items: center;
justify-content: space-around;
z-index: 1000;
}
#email-input {
height: 30px;
width: 220px;
border-radius: 10px;
}
#submit-button {
height: 30px;
width: 80px;
border-radius: 10px;
background-color: lightgreen;
border: 1px dashed;
}
@media all and (min-width: 1000px) {
body {
background-image: url('https://img.freepik.com/free-vector/online-education-students-view-lessons-through-mobile-devices-during-distance-learning_178888-361.jpg?w=2000');
}
#submit-form {
height: 250px;
width: 500px;
}
#email-input {
height: 50px;
width: 300px;
border-radius: 10px;
}
}
function findUserDevice() {
const isOnMobile = false;
if(window.innerWidth < window.innerHeight) {
isOnMobile = true;
}
return isOnMobile;
}
这是我通常使用的,希望对你有所帮助!