Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is your solution: (This also supports additional images).</p> <pre><code>var m = 0, imgs = ["images/bg.jpg", "images/bg_2.jpg"]; function changeDivImage() { document.body.style.backgroundImage = "url(" + imgs[m] + ")"; m = (m + 1) % imgs.length; } </code></pre> <p>Here is the <strong><a href="http://jsfiddle.net/CvEtq/" rel="nofollow">working code on jsFiddle</a></strong>. </p> <p>Here is the <strong><a href="http://jsfiddle.net/7VdAV/" rel="nofollow">jQuery version on jsFiddle</a></strong>.</p> <p><strong>UPDATE: <em>CROSS-FADING Version</em></strong></p> <p>Here is the <strong><a href="http://jsfiddle.net/ftdmw/" rel="nofollow">cross-fading jQuery version on jsFiddle</a></strong>.</p> <p>You wouldn't want the whole page (with all elements) to fade in/out. Only the bg should fade. So, this version has a div to be used as the <strong>background container</strong>. Its <strong>z-depth</strong> is arranged so that it will keep itself the bottom-most element on the page; and switch between its two children to create the cross-fade effect.</p> <p><strong>HTML:</strong></p> <pre><code>&lt;div id="bg"&gt; &lt;div id="bg-top"&gt;&lt;/div&gt; &lt;div id="bg-bottom"&gt;&lt;/div&gt; &lt;/div&gt; &lt;a id="bg-changer" href="#"&gt;change&lt;/a&gt; </code></pre> <p><strong>CSS:</strong></p> <pre><code>div#bg, div#bg-top, div#bg-bottom { display: block; position: fixed; width: 100%; /*height: 500px;*/ /* height is set by javascript on every window resize */ overflow: hidden; } div#bg { z-index: -99; } </code></pre> <p><strong>Javascript (jQuery):</strong></p> <pre><code>var m = 0, /* Array of background images. You can add more to it. */ imgs = ["images/bg.jpg", "images/bg_2.jpg"]; /* Toggles the background images with cross-fade effect. */ function changeDivImage() { setBgHeight(); var imgTop = imgs[m]; m = (m + 1) % imgs.length; var imgBottom = imgs[m]; $('div#bg') .children('#bg-top').show() .css('background-image', 'url(' + imgTop + ')') .fadeOut('slow') .end() .children('#bg-bottom').hide() .css('background-image', 'url(' + imgBottom + ')') .fadeIn('slow'); } /* Sets the background div height to (fit the) window height. */ function setBgHeight() { var h = $(window).height(); $('div#bg').height(h).children().height(h); } /* DOM ready event handler. */ $(document).ready(function(event) { $('a#bg-changer').click(function(event) { changeDivImage(); }); changeDivImage(); //fade in the first image when the DOM is ready. }); /* Window resize event handler. */ $(window).resize(function(event) { setBgHeight(); //set the background height everytime. }); </code></pre> <p>This could be improved more but it should give you an idea. </p>
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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