Change an element's background if another is visble
I have multiples of ".container" and They're all shown one at a time. I'm trying to get the backgroundof the parent DIV to change depending on the visible ".container" . but it doesn't work. Any ideas on how this can be achieved?
HTML
- <div id="main">
- <div class="prev">Previous</div>
- <div class="next">Next</div>
- <div class="container active">One</div>
- <div class="container">Two</div>
- <div class="container">Three</div>
- <div class="container">Four</div>
-
- </div>
CSS
- .prev, .next {
- float: left;
- color:blue;
- font-size: 24px;
- margin: 10px 0 50px 50px;
- cursor:pointer;
- width: 40%;
- color: black;
- }
- .container {
- width: 100%;
- font-size: 70px;
- margin-bottom: 20px;
-
- }
- .container {
- display: none;
- }
- .container.active {
- display: inherit;
- }
-
jQuery
- $(".prev").on("click", function(){
- $(".container.active").removeClass("active").prev().addClass("active");
- var $prev = $('.container.active').removeClass('active');
- if ($prev.length) {
- $prev.addClass('active');
- }
- else {
- $(".container:last").addClass('active');
- }
-
- });
- $(".next").on("click", function(){
- $(".container.active").removeClass("active").next().addClass("active");
- var $next = $('.container.active').removeClass('active');
- if ($next.length) {
- $next.addClass('active');
- }
- else {
- $(".container:first").addClass('active');
- }
-
-
- });
- jQuery(document).ready(function(){
- if ($('.container:nth-of-type(2)').is(':visible')) {
- $('#main').css({'background' : 'red'});
- }
- else {}
- });
Here's a jsFiddle link