html - How to use jquery to cycle elements bg color on click, and reset upon another element's click -
so i'm setting background changer accessibility purposes client. have set of div's required bg color, , jquery change body background on click.
what want clicked element change white (ie, reset option) on click.
but code have, if click on another, turns white, without resetting other, end whole bunch of white divs.
here code:
<div class="bg_changer"> <ul><li class="bg_1 bg_setter"><a >bg_one</a></li> <li class="bg_2 bg_setter" ><a >bg_two</a></li> <li class="bg_3 bg_setter"><a >bg_three</a></li> <li class="bg_4 bg_setter"><a >bg_four</a></li> <li class="bg_5 bg_setter"><a >bg_five</a></li> <li class="bg_6 bg_setter"><a >bg_six</a></li> </ul> </div><!-- end of bg_changer --> css
.bg_1{ background-color: rgba(204,204,204,1); } .bg_2{ background-color: rgba(254,254,196,1); } .bg_3{ background-color: rgba(253,190,130,1); } .bg_4{ background-color: rgba(253,253,128,1); } .bg_5{ background-color: rgba(158,208,253,1); } .bg_6{ background-color: rgba(218,218,254,1); } and jq
$(document).ready(function(){ $(".bg_setter").click(function() { var bg_new = $(this).css('background-color'); $("body").css('background-color', bg_new); $(this).css('background-color', 'white'); }); }); does have ideas how implement change, if switch bg_one, switch bg_two, can reset bg_one it's original background color?
i can't think of way without storing tonne of variables, surely there must easier...
one way set global variable holds selected element, , selected element's color. when click another, sets selected element , color back, using variables.
$(document).ready(function() { //global vars - nothing yet, no color has been selected var prev_element, prev_color; $(".bg_setter").click(function() { //if there has been selected element - set it's color if (prev_element) { prev_element.css('background-color', prev_color); } //store element , current color prev_element = $(this); prev_color = $(this).css('background-color'); //update body background color color selected $("body").css('background-color', prev_color); //set selected element background color white prev_element.css('background-color', 'white'); }); });
Comments
Post a Comment