I need to create a wallpaper. So this would work:
html { background: url(images/bg.jpg) no-repeat center center fixed; -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; background-size: cover; }
So, I would upload a large background image (width of about 2000 pixels), but I think this solution is pretty bad, as mobile detectors also load this large image.
I tried loading the image added to the screen size:
jQuery(document).ready(function($) { var win = $(window); win.resize(function() { var win_w = win.width(), win_h = win.height(), $bg = $("#bg"); // Load narrowest background image based on // viewport width, but never load anything narrower // that what already loaded if anything. var available = [ 1024, 1280, 1366, 1400, 1680, 1920, 2560, 3840, 4860 ]; var current = $bg.attr('src').match(/([0-9]+)/) ? RegExp.$1 : null; if (!current || ((current < win_w) && (current < available[available.length - 1]))) { var chosen = available[available.length - 1]; for (var i=0; i<available.length; i++) { if (available[i] >= win_w) { chosen = available[i]; break; } } // Set the new image $bg.attr('src', '/images/bg/' + chosen + '.jpg'); } // Determine whether width or height should be 100% if ((win_w / win_h) < ($bg.width() / $bg.height())) { $bg.css({height: '100%', width: 'auto'}); } else { $bg.css({width: '100%', height: 'auto'}); } }).resize(); });
So I need an image element like this:
<img id="bg" src="" style="position: fixed; left: 0; top: 0" />
But actually this is not the background (for example, the first solution). Is it possible to adapt jquery-script to html-background-url? Or does anyone know a simpler solution?
I also want to download the image first (!), And after that the website will fade ... I think I need the .load () jQuery function for this; but I don't know how to do this in a script ...