[英]How to disable some function in javascript when mobile device is use to open my webpage?
How I can disable some of my js script function when mobile device is used to open my webpage?当使用移动设备打开我的网页时,如何禁用我的一些 js 脚本 function? I can make it if it's on css only, but some button need to have function on js.
如果它仅在 css 上,我可以做到,但某些按钮需要在 js 上具有 function。 this is my internal js code
这是我的内部 js 代码
var menuBtn = document.getElementById("menu")
var sideNav = document.getElementById("header")
var xBtn = document.getElementById("xBtn")
var slidePage = document.getElementById("main-page")
sideNav.style.right = "-250px";
menuBtn.onclick = function(){
if(sideNav.style.right == "-250px"){
sideNav.style.right = "0";
menuBtn.style.display = "none";
slidePage.style.left = "-9%";
}
else{
sideNav.style.right = "-250px";
menuBtn.style.display = "block";
}
}
xBtn.onclick = function(){
sideNav.style.right = "-250px";
slidePage.style.left = "0";
menuBtn.style.display = "block";
}
You can detect using this function.您可以使用此 function 进行检测。
function isMobile(){
return window.innerWidth <= 600;
}
Happy coding:)快乐编码:)
You can use matchMedia()
for javascript to keep track of the width of the screen.您可以对 javascript 使用
matchMedia()
来跟踪屏幕的宽度。 If the screen width meets the specified conditions, then the specified function is disabled by returning a false .如果屏幕宽度满足指定条件,则通过返回false禁用指定的 function。 Like that:
像那样:
if (window.matchMedia('(max-width: 767px)').matches) {
function nameFunction() {
return false;
}
} else {}
you can detect Mobile device with below function.您可以检测到具有以下 function 的移动设备。
function isMobile(){
if (/Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent) ||
(/Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.platform))) {
return true;
}
return false;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.