Skip to content

Instantly share code, notes, and snippets.

@madpilot
Created May 23, 2012 04:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save madpilot/2773319 to your computer and use it in GitHub Desktop.
Save madpilot/2773319 to your computer and use it in GitHub Desktop.
Responsive images idea
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
var resize = function() {
$('altimg').each(function(i, e) {
if($(e).css('display') != "none") {
$('img[data-name=' + $(e).attr('data-name') + ']').attr('src', $(e).attr('src')).attr('alt', $(e).attr('alt'));
}
});
}
$(window).load(resize);
$(window).resize(resize);
</script>
<style type="text/css">
altimg {
display: none;
}
@media screen and (min-width: 640px) and (max-width: 800px) {
altimg.img-640 {
display: block
}
}
@media screen and (min-width: 800px) and (max-width: 1024px) {
altimg.img-800 {
display: block
}
}
@media screen and (min-width: 1024px) {
altimg.img-1024 {
display: block
}
}
</style>
</head>
<body>
<img src="melbourne-500x500.jpeg" alt="500x500" data-name="melbourne">
<altimg src="melbourne-640x480.jpeg" alt="640x480"class="img-640" data-name="melbourne"></altimg>
<altimg src="melbourne-800x600.jpeg" alt="800x600" class="img-800" data-name="melbourne"></altimg>
<altimg src="melbourne-1024x768.jpeg" alt="1024x768" class="img-1024" data-name="melbourne"></altimg>
</img>
</body>
</html>
@madpilot
Copy link
Author

An idea for Responsive Images.

Advantages:

  1. Allows Images AND alt tags to be specified for all images
  2. Targetable via CSS
  3. Can be used now (with a small JS shim) (Only tested on FF/Chrome - Can someone test this on IE?)
  4. Images don't download until they are required

Disadvantages

  1. Requires HTML5 spec to allow children (Then we can get rid of the data-name attributes)
  2. First (default) image is still downloaded first time

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment