简体   繁体   English

如何在移动设备上禁用脚本

[英]How to disable a script when on mobile device

I have a site which has a parallax scroll effect and it's running the skrollr.js script. 我有一个具有视差滚动效果的网站,它正在运行skrollr.js脚本。 It's a mobile first website, but I would like for the script to not run on mobile devices because it won't allow scrolling. 这是一个移动的第一个网站,但我希望脚本不能在移动设备上运行,因为它不允许滚动。 Does anyone know how I can prevent the script from running when on a mobile device? 有谁知道如何阻止脚本在移动设备上运行? Thanks. 谢谢。

The code where the script is uploaded is at then end of the body section. 上载脚本的代码位于正文部分的末尾。 If any other code is needed let me know. 如果需要任何其他代码,请告诉我。

<!-- SCRIPTS -->

<script type="text/javascript" src="js/skrollr.js"></script>
<script type="text/javascript">
    skrollr.init();
</script>

<!--/ SCRIPTS -->

You can use modernizr 's touch detection in order to check if it's a touch device, and, if yes, don't init the script. 您可以使用modernizr的触摸检测来检查它是否是触摸设备,如果是,则不要初始化脚本。

if (Modernizr.touch) {

}
else 
{
    skrollr.init();
}

or you can check for the user agent (this might not be your best option as user agent isn't always reliable), and write a simple if else, with the skrollr init in the else 或者您可以检查用户代理(这可能不是您的最佳选择,因为用户代理并不总是可靠的),并在else中使用skrollr init编写一个简单的if else

  var isMobile = {
            Android: function () {
                return navigator.userAgent.match(/Android/i);
            },
            BlackBerry: function () {
                return navigator.userAgent.match(/BlackBerry/i);
            },
            iOS: function () {
                return navigator.userAgent.match(/iPhone|iPad|iPod/i);
            },
            Opera: function () {
                return navigator.userAgent.match(/Opera Mini/i);
            },
            Windows: function () {
                return navigator.userAgent.match(/IEMobile/i);
            },
            any: function () {
                return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows());
            }
        };

        if (isMobile.any()) {

        }
        else {

             skrollr.init(); 
        }

Another way of testing would be by checking window.innerWidth and only init your script if the screen size is larger than 760px: 另一种测试方法是检查window.innerWidth ,如果屏幕大小大于760px,则只启动脚本:

if (window.innerWidth > 760) {
skrollr.init();
}

Assume if you are writing a function name exampleNotForMobileDevicese() if the window width is less than 768 then the next code wont excute beacuse we have use return ie if the condition satisfies then the execution will stop 假设你正在编写一个函数名称exampleNotForMobileDevicese()如果窗口宽度小于768那么下一个代码将不会执行因为我们使用了返回,即如果条件满足则执行将停止

 function exampleNotForMobileDevicese(){ if(window.innerwidth < 768){ return } your business logics....... } 

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

相关问题 如果在移动设备上查看脚本,如何禁用或隐藏脚本在我的网站上运行? - How can I disable or hide a script from running on my site if being viewed on a mobile device? 如何在移动设备上的点击上禁用引导弹出窗口 - How to disable bootstrap popover on tap in mobile device 当使用移动设备打开我的网页时,如何禁用 javascript 中的某些 function? - How to disable some function in javascript when mobile device is use to open my webpage? 如何在移动设备上禁用fancybox并直接显示大图像 - How to disable fancybox on mobile device and display the large image directly 如何在jQuery Autocomplete Widget的移动设备上禁用悬停效果 - How to disable hover effect on mobile device for jQuery Autocomplete Widget 如何在不检查设备的情况下禁用移动浏览器上的滚动? - How to disable scroll on mobile browser without checking device? 如何在移动设备上禁用引导数据间谍? (较小的分辨率) - How to disable bootstrap data-spy on mobile device? (smaller resolution) 如何仅在移动设备上禁用滚动回到顶部 - How to disable scroll back to top only on mobile device 在手机上滚动时如何禁用:hover? - How to disable :hover when scrolling on mobile? 设备移动时如何更改元素的顺序 - How to change order of elements when device is mobile
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM