[英]How to stop bootstrap 4 carousel automatic sliding in mobile using JavaScript or any
[英]How to stop bootstrap carousel automatic slide in mobile
嗨,我正在尝试找到一种方法来停止引导带轮播自动滑动功能,使其仅在移动设备中停止。 我尝试使用javascript自己执行此操作,但是我使用的代码似乎不起作用。
var ismobile = window.matchMedia("only screen and (max-width: 760px)");
if (ismobile.matches) {
$('.carousel').carousel ({
interval:false
});
}
if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
$('.carousel').carousel ({
interval:false
});
}
从这里得到
我正在使用这个,为我工作:
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());
}
};
$('.carousel').carousel ({
interval: isMobile.any() ? false : 5000
});
资料来源: http : //www.abeautifulsite.net/detecting-mobile-devices-with-javascript/
轻微的更新,因为上面的代码片段对我来说也有点麻烦,因为它不太起作用。
(function(){
var isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
var windowIsThin = window.matchMedia("(max-width:992px)").matches;
if (isMobile || windowIsThin) {
//carousel disabled
$('.carousel').carousel({
interval: false
});
};
});
经过chrome,IE,Firefox和Opera的测试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.