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