In my image carousel, the main thing that changes is the value of the margin-left
. From knowing this value, I wanted to try to interpret a slide number (like 'Image 3 of 4').
I got the number of all slides with:
var all_slides_width = $('#gallery_wrapper').outerWidth();
var slide_width = $('.image_content').outerWidth();
var slide_quantity = all_slides_width / slide_width;
Then I wanted to get the number of the currently visible image with:
var margin_left = parseInt($('#gallery_wrapper').css('margin-left'));
var slide_count = parseInt(-margin_left + slide_width) / slide_width;
and displaying it with:
<span class="slide_number"></span>
<script>
$( '.slide_number' ).text( 'Image ' + slide_count + " of " + slide_quantity);
</script>
The problem is, that the margin-left
changes with every new slide. So I need a loop that changes the slide_count
every time the margin-left
changes. How can I achieve this?
A setTimeout
of every second was obviously very inefficient. Here's a fiddle of my current status: http://jsfiddle.net/cqgnvsce/