Skip to content

Instantly share code, notes, and snippets.

@kazu69
Created March 16, 2013 13:55
Show Gist options
  • Save kazu69/5176499 to your computer and use it in GitHub Desktop.
Save kazu69/5176499 to your computer and use it in GitHub Desktop.
mediaqueryで最適な画像に切り替えるサンプル refs: https://speakerdeck.com/smashingmag/responsive-web-design-clever-tips-and-techniques
@media all and (min-width:45em) {
body:after {
content: 'desktop';
display: none;
}
<a href='#'>
<img src='desktop-friendly-version.jpg' data-medium='tablet-friendly-version.jpg' data-large='desktop-friendly-version.jpg'>
</a>
$(function(){
var size = window.getComputedStyle(document.body, ':after').getPropertyValue('content');
if(size == 'desktop') {
$('img').each(function(index){
$(this).attr(
src: $(this).attr('data-large');
);
});
}
if(size == 'tablet') {
$('img').each(function(index){
$(this).attr(
src: $(this).attr('data-medium');
);
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment