1

I'm trying to create this carousel from scratch and have this for now.

<script type="text/javascript">
    $(document).ready(function() {
        setTimeout(function() {
            $('.carousel-inner li').animate({
                right: '580px'
                }, 500);                
        }, 3000);

        $('#right').click(function() {
            $('.carousel-inner li').animate({
                right: '580px'
                }, 500);
        });
        $('#left').click(function() {
            $('.carousel-inner li').animate({
                left: '0px'
                }, 500);
        });     
    });
</script>

It works when I click "#right" and "#left" but only once. I want to make it work that when I click "#next" again it moves again.

Here is the Fiddle

2 Answers 2

2

Insead of left: '0px' use left: '-=580px'. And where you have right: '580px' put left: '+=580px'. But the user will be able to shift all your li's outside visible areas, you're gonna need some if's.

Sign up to request clarification or add additional context in comments.

3 Comments

The left: '-=580px' does not make it go back it just keeps going forward.
This made it work thanks. Could you help me out get started with the if's? have no clue on where to start them. Or do I have to re-write the code? Thanks in advance.
Not to think too much, I would suggest something like: var n = 1; (it's in global) everywhere, where there is left: +=580px put if (n <= 0) return; n--; in the beginning of a function. Where there's left: -=580px put if (n >= 3) return; n++; Constant 3 you may change depending on amount of blocks (I took it from the fiddle). And also you can name a counter variable properly.
0

You should find the current position of the element using $('.carousel-inner li').offset() and add it to the value that you wish to move the element by.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.