Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change background when active class changes on Bootstrap Carousel

I wanted to change the background of a container every time the slider slides. I figure I need to check if/ when the class of the item because active. I also wonder if there isn't an event for which I should be listening. Here's the jsfiddle I'm working on:

https://jsfiddle.net/podbarron/nj04xms5/

$(function(){
$('#carousel-indicators').on('click', 'li', function(){

});
});

Thanks!

like image 686
PudparK Avatar asked Sep 03 '26 09:09

PudparK


1 Answers

Bootstrap Carousel has an event which fires on/after each slide transition. You should be able to use this.

Example: https://jsfiddle.net/nj04xms5/7/

The js inside the event is very basic but just to give you an idea.

e.relatedTarget will get you the current slide you are on if you wanted to get information from it (i.e. the ID)

$('#carousel-example-generic').on('slide.bs.carousel', function(e) {
      if(e.relatedTarget.id == 'firstSlide'){
          $("#landingContainer").css('background-color', 'orange');
      } else if(e.relatedTarget.id == 'secondSlide'){
          $("#landingContainer").css('background-color', 'green');
      }
})
like image 66
haxtbh Avatar answered Sep 04 '26 23:09

haxtbh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!