简体   繁体   English

如何为移动设备禁用 javascript

[英]how to disable javascript for mobile devices

Hi so this is the page I will be talking about: https://wordpress-776730-2792401.cloudwaysapps.com/demo-horizontal-scrol/嗨,这是我要讨论的页面: https://wordpress-776730-2792401.cloudwaysapps.com/demo-horizontal-scroll/

I have a horizontal scroll on it which I want to disable for mobile devices (tablet and phone).我有一个水平滚动条,我想为移动设备(平板电脑和手机)禁用它。 This is the code i for the horizontal scroll:这是水平滚动的代码 i:

<script type="text/javascript">
    if (window.innerWidth > 1025) {
       const scrollContainer = document.querySelector("main");

        scrollContainer.addEventListener("wheel", (evt) => {
            evt.preventDefault();
            scrollContainer.scrollLeft += evt.deltaY;
        });
    } else {

}
</script> 

This is the code i used to disable it but it also disables desktop horizontal scroll:这是我用来禁用它的代码,但它也禁用了桌面水平滚动:

<script type="text/javascript">     function isMobile() { return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent); }  if (!isMobile()) { <script type="text/javascript">
    if (window.innerWidth > 1025) {
       const scrollContainer = document.querySelector("main");

        scrollContainer.addEventListener("wheel", (evt) => {
            evt.preventDefault();
            scrollContainer.scrollLeft += evt.deltaY;
        });
    } else {

}
</script>

PLEASE HELP:)请帮忙:)

You can try this, if you want to disable your custom js:如果你想禁用你的自定义 js,你可以试试这个:

function custom_load_scripts() {
    // Load if not mobile 
    if ( ! wp_is_mobile() ) {
        wp_enqueue_script( 'script-handle', get_template_directory_uri() . '/js/example.js', array(), '1.0.0', true );
    }
}
add_action( 'wp_enqueue_scripts', 'custom_load_scripts' );

Or you can use wp_print_scripts action to disable any scripts:或者您可以使用wp_print_scripts操作来禁用任何脚本:

add_action( 'wp_print_scripts', 'dequeue_unnecessary_scripts' );
function dequeue_unnecessary_scripts() {
    // Deregister if mobile
    if ( wp_is_mobile() ) {
        wp_dequeue_script( 'js-handle' );
        wp_deregister_script( 'js-handle' );
    }
}

Or you can check this post .或者你可以查看这篇文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM