Created
March 19, 2014 00:28
-
-
Save ivomota/9633128 to your computer and use it in GitHub Desktop.
Cover with CSS3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| img.bg { | |
| /* Set rules to fill background */ | |
| min-height: 100%; | |
| min-width: 1024px; | |
| /* Set up proportionate scaling */ | |
| width: 100%; | |
| height: auto; | |
| /* Set up positioning */ | |
| position: fixed; | |
| top: 0; | |
| left: 0; | |
| } | |
| @media screen and (max-width: 1024px) { /* Specific to this particular image */ | |
| img.bg { | |
| left: 50%; | |
| margin-left: -512px; /* 50% */ | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #bg { | |
| position: fixed; | |
| top: -50%; | |
| left: -50%; | |
| width: 200%; | |
| height: 200%; | |
| } | |
| #bg img { | |
| position: absolute; | |
| top: 0; | |
| left: 0; | |
| right: 0; | |
| bottom: 0; | |
| margin: auto; | |
| min-width: 50%; | |
| min-height: 50%; | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <div id="bg"> | |
| <img src="images/bg.jpg" alt=""> | |
| </div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #bg { position: fixed; top: 0; left: 0; } | |
| .bgwidth { width: 100%; } | |
| .bgheight { height: 100%; } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <img src="images/bg.jpg" id="bg" alt=""> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $(window).load(function() { | |
| var theWindow = $(window), | |
| $bg = $("#bg"), | |
| aspectRatio = $bg.width() / $bg.height(); | |
| function resizeBg() { | |
| if ( (theWindow.width() / theWindow.height()) < aspectRatio ) { | |
| $bg | |
| .removeClass() | |
| .addClass('bgheight'); | |
| } else { | |
| $bg | |
| .removeClass() | |
| .addClass('bgwidth'); | |
| } | |
| } | |
| theWindow.resize(resizeBg).trigger("resize"); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <img id="bg" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" alt="" style="position: fixed; left: 0; top: 0" /> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (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's 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', '/img/bg/' + chosen + '.jpg'); | |
| // for testing... | |
| // console.log('Chosen background: ' + chosen); | |
| } | |
| // 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(); | |
| })(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment