Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>No there isn't. Even with multiple background images you still only have one attribute background-image. You need to sort out the string value yourself and hand it back. Something like the following will do the trick.</p> <p><a href="http://jsfiddle.net/cHave/6/" rel="nofollow">jsFiddle</a></p> <pre><code>&lt;div id="showcase"&gt;&lt;/div&gt; &lt;button class="changeBG firstBG"&gt;change first bg image&lt;/button&gt; &lt;button class="changeBG secondBG"&gt;change second bg image&lt;/button&gt; &lt;button class="changeBG thirdBG"&gt;change third bg image&lt;/button&gt; var backgrounds = [ ["url11", "url12", "url13"], ["url21", "url22", "url23"], ["url31", "url32", "url33"], ]; $('#showcase').data('currentBGs', [0,0,0]); $('.changeBG').on('click',function(){ var $button = $(this), whichButton, whichBGnum, bgLength, currentBGs, bgString, bg0, bg1, bg2; if( $button.hasClass('firstBG') ){ whichButton = 0; }else if( $button.hasClass('secondBG') ){ whichButton = 1; }else if( $button.hasClass('thirdBG') ){ whichButton = 2; } currentBGs = $('#showcase').data('currentBGs'); currentBGnum = currentBGs[whichButton]; bgLength = backgrounds[whichButton].length; currentBGnum = ( currentBGnum + 1 ) % bgLength; currentBGs[whichButton] = currentBGnum; bg0 = backgrounds[0][ currentBGs[0] ]; bg1 = backgrounds[1][ currentBGs[1] ]; bg2 = backgrounds[2][ currentBGs[2] ]; bgString = 'url(' + bg0 + '), url(' + bg1 + '), url(' + bg2 + ')'; $('#showcase').data('currentBGs', currentBGs) .css('background-image', bgString); }); </code></pre> <p>This code is not meant to be optimized, but readable. The example pics are silly, they are just whatever browser windows I had open.</p> <ul> <li>Keep a JS array of all the pics. This is a multidimensional array, first level represents each part of the background, second level is each option for that background.</li> <li>Write down the current state of the backgrounds, here we just <code>.data()</code>.</li> <li>Figure out which button was clicked</li> <li>Get the current state on the display</li> <li>Cycle the appropriate counter</li> <li>Get the new backgrounds</li> <li>Update the state of the display with both the data and the new backgrounds.</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload