I have a modal containing a carousel with 2 slides with login and signup forms.
I want to achieve the following:
- When user clicks a “Login” link in site header, the modal should open
and carousel should slide to the “login” slide (it’s at the index of
0). - When user clicks a “Signup” link in site header, the modal should
open and carousel should slide to the “signup” slide (it’s at the
index of 1) - Whenever a slide transition occures, the modal title text
is changed (binding to the “slid” event of the carousel)
However, I cannot seemt to get this to work. I think the problem is with the order of how all these events happen.
Here’s the basic code:
// Initialize modal
$('.modal').modal()
// Initialize carousel
$('.carousel').carousel()
$('.carousel').carousel('pause') // Pause the carousel so it doesn't auto-rotate
$('.signuplink').on('click', function() {
$('.carousel').carousel(1).on('slid', function() {
$('.modal .modal-title').html('Sign Up');
$('.modal').modal('show');
});
});
But it does not work. The “slid” event never occures when the modal is not shown beforehand. So I tried to show the modal and then slide the carousel, binding to the “shown” event of the modal, but while it does slide to the next slide, the “slid” event does not occur.
What is even more weird, is that the close button on the modal does not work anymore, like all event handling is messed up…
EDIT: It seems that the “shown” event is not fired when the modal is being shown the first time. It fires only when I hide the modal. When I open it again, “shown” seems to be fired on the correct moment.
First of all, there is too much nesting.
You should almost never nest event binding inside a
clickevent – or any event – because you will add another listener each time you click.This is a solution.
Live Demo (jsfiddle)
But there is still a bug if you activate the sliding effect : the
Carouselobject keepsliding: trueafter a few slidings – thus blocking future slidings ; improvement welcome.