简体   繁体   English

当使用移动设备打开我的网页时,如何禁用 javascript 中的某些 function?

[英]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.

相关问题 javascript一个替换我网页上的一些单词的功能 - javascript a function to replace some words on my webpage 如何在移动设备上禁用脚本 - How to disable a script when on mobile device Android Webview在某些移动设备中不执行javascript功能 - Android webview doesn't execute javascript function in some mobile device 如何为移动设备禁用我的 javascript - How to disable my javascript for mobile devices 如何在移动设备上禁用我的这部分JavaScript? - How to disable this part of my javascript on mobile devices? 如何打开网页并运行一些javascript函数? - How do I open a webpage and run some javascript functions? 在移动设备上输入时,如何让我的 React TextField 打开数字键盘? - How do I get my React TextField to open the number pad when inputting on a mobile device? 如果我的网页未在任何选项卡中打开,则禁用通知 - Disable notification if my webpage is not open in any tab 如何在Chrome网页上使用javascript禁用键盘的ContextMenu功能? - How to disable keyboard's ContextMenu function with javascript in webpage on chrome? 如果在移动设备上打开网页,则禁用该网页。 (Android / iPad /黑莓手机) - Disable webpage if opened on a mobile device. (Android / iPad / Blackberry)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM