问题
Hi I'm trying to find a way to stop bootstraps carousel automatic slide function to stop only in mobile. I tried to carry this out myself using javascript, but the code I've used doesn't seem to work.
var ismobile = window.matchMedia("only screen and (max-width: 760px)");
if (ismobile.matches) {
$('.carousel').carousel ({
interval:false
});
}
回答1:
I am using this one, working grate for me:
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
});
Source: http://www.abeautifulsite.net/detecting-mobile-devices-with-javascript/
回答2:
if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
$('.carousel').carousel ({
interval:false
});
}
got from here
回答3:
Slight update as I too was having a little trouble with this the code snippet above taken as it is didn't quite work.
(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
});
};
});
Tested in chrome, IE, Firefox and Opera.
来源:https://stackoverflow.com/questions/26380240/how-to-stop-bootstrap-carousel-automatic-slide-in-mobile