The remainder operator is really useful for wrapping things like your index
around:
index = (index + 1) % length
…where length
is the length of what you’re indexing into (this assumes 0-based indexing).
So your Slide
function can do something along these lines:
function Slide(car) {
var urls = bs.url[car];
var index = 0;
function run() {
Slider.css({
"background": urls[index],
"background-position": "center",
"background-attachment" : "fixed",
"background-size" : "cover"
});
index = (index + 1) % urls.length;
}
run();
setInterval(run, bs.time);
}
6
solved Switching Images [closed]