admin管理员组文章数量:1241097
I have a Bootstrap Carousel on my website.
When the user hovers over an element #formcontainer
, I'd like to pause the carousel. And when I hover off, I'd like to continue the cycle of the carousel. The first part works fine with the following code, but not the second. Can someone assist?
$(document).ready(function() {
$('.carousel').carousel({
interval: 1200,
pause: "hover"
});
$('#formcontainer').hover(function(){
$("#myCarousel4").carousel('pause');
});
});
I have a Bootstrap Carousel on my website.
When the user hovers over an element #formcontainer
, I'd like to pause the carousel. And when I hover off, I'd like to continue the cycle of the carousel. The first part works fine with the following code, but not the second. Can someone assist?
$(document).ready(function() {
$('.carousel').carousel({
interval: 1200,
pause: "hover"
});
$('#formcontainer').hover(function(){
$("#myCarousel4").carousel('pause');
});
});
Share
Improve this question
edited Dec 9, 2016 at 13:43
doppelgreener
5,10410 gold badges48 silver badges67 bronze badges
asked Dec 9, 2016 at 13:24
michaelmcgurkmichaelmcgurk
6,50925 gold badges100 silver badges197 bronze badges
3 Answers
Reset to default 9Use carousel cycle
method on mouseleave
event
$('#formcontainer').hover(function(){
$("#myCarousel4").carousel('pause');
},function(){
$("#myCarousel4").carousel('cycle');
});
jQuery's hover function has an implementation that takes two arguments: a hover in handler and a hover out handler:
$('#foo').hover(function() {
// handler in
}, function() {
// handler out
});
When you pass it just one argument, the function you give it handles both the in and out events, so you're pausing it on both mouse enter and mouse out.
You need to pass it separate handlers:
$('#myCarousel4').hover(function() {
$(this).carousel('pause');
}, function() {
$(this).carousel('cycle');
});
Note that we can use this
to refer to the carousel rather than rewriting its ID. Inside jQuery event handlers, this
always refers to the object you bound the event to when possible.
This will work!
(function($) {
$(document).ready(function(){
$('.carousel').carousel({
pause: 'hover'
});
}); }(jQuery));
本文标签: javascriptHow can I pause a Bootstrap Carousel on hover and resume it on mouseoutStack Overflow
版权声明:本文标题:javascript - How can I pause a Bootstrap Carousel on hover and resume it on mouse-out? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740035648a2221447.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论