Skip to content

Commit 313e966

Browse files
committed
Responsive enhance script created.
0 parents  commit 313e966

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

index.html

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<style>
6+
body {
7+
font-family: sans-serif;
8+
}
9+
.wrap {
10+
width: 75%;
11+
max-width: 800px;
12+
margin: 2em auto;
13+
}
14+
img {
15+
width: 100%;
16+
}
17+
pre {
18+
overflow: auto;
19+
padding: 1em;
20+
border: 1px solid #999;
21+
}
22+
</style>
23+
<title>Responsive Enhance</title>
24+
<meta name="viewport" content="width=device-width, initial-scale=1">
25+
</head>
26+
<body>
27+
<div class="wrap">
28+
<h1>Responsive Enhance</h1>
29+
<p>Depending on the size of your viewport, you may see a 400px by 300px wide image, or a 800px by 600px image. Try refreshing the page with a small or wide browser window. The standard image is always downloaded, full images are then downloaded as needed.</p>
30+
<p><img id="demo1" src="http://dummyimage.com/400x300" alt="Responsive Image" data-fullsrc="http://dummyimage.com/800x600"></p>
31+
<p><pre><code>&lt;img id="demo1" src="http://dummyimage.com/400x300" alt="Responsive Image" data-fullsrc="http://dummyimage.com/800x600"&gt;
32+
&lt;script&gt;responsiveEnhance(document.getElementById('demo1'), 400);&lt;/script&gt;</code></pre></p>
33+
<p>Here's a photo of some lovely star biscuits that <a href="http://twitter.com/qwertykate">@qwertykate</a> bought us. Image courtesy of <a href="http://www.flickr.com/photos/adactio/">adactio</a>.</p>
34+
<p><a href="http://www.flickr.com/photos/adactio/6806617685"><img id="demo2" src="http://farm8.staticflickr.com/7022/6806617685_3be0d007c3_m.jpg" alt="Responsive Image" data-fullsrc="http://farm8.staticflickr.com/7022/6806617685_b793771008_o.jpg"></a></p>
35+
<p><pre><code>&lt;img id="demo2" src="http://farm8.staticflickr.com/7022/6806617685_3be0d007c3_m.jpg" alt="Responsive Image" data-fullsrc="http://farm8.staticflickr.com/7022/6806617685_b793771008_o.jpg"&gt;
36+
&lt;script&gt;responsiveEnhance(document.getElementById('demo2'), 240);&lt;/script&gt;</code></pre></p>
37+
</div>
38+
</body>
39+
<script src="responsive-enhance.js"></script>
40+
<script>
41+
responsiveEnhance(document.getElementById('demo1'), 400);
42+
responsiveEnhance(document.getElementById('demo2'), 240);
43+
</script>
44+
</html>

readme.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Responsive Enhance
2+
==================
3+
4+
This script will check whether an image is larger than a specified width and load a full resolution image if neccessary.
5+
6+
<img id="demo" src="http://dummyimage.com/400x300" alt="Responsive Image" data-fullsrc="http://dummyimage.com/800x600">
7+
<script>responsiveEnhance(document.getElementById('demo'), 400);</script>
8+
9+
This will replace the image with the image defined in the `data-fullsrc` when the image is wider than 400 pixels.

responsive-enhance.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
var addEvent=function(){return document.addEventListener?function(a,c,d){if(a&&a.nodeName||a===window)a.addEventListener(c,d,!1);else if(a&&a.length)for(var b=0;b<a.length;b++)addEvent(a[b],c,d)}:function(a,c,d){if(a&&a.nodeName||a===window)a.attachEvent("on"+c,function(){return d.call(a,window.event)});else if(a&&a.length)for(var b=0;b<a.length;b++)addEvent(a[b],c,d)}}();
2+
3+
var responsiveEnhance = function(img, width, monitor) {
4+
console.log(img);
5+
if (img.length) {
6+
for (var i=0, len=img.length; i<len; i++) {
7+
responsiveEnhance(img[i], width, monitor);
8+
}
9+
} else {
10+
if (((' '+img.className+' ').replace(/[\n\t]/g, ' ').indexOf(' large ') == -1) && img.clientWidth > width) {
11+
var fullimg = new Image();
12+
addEvent(fullimg, 'load', function(e) {
13+
img.src = this.src;
14+
img.className += ' large';
15+
});
16+
fullimg.src = img.getAttribute('data-fullsrc');
17+
}
18+
}
19+
if (monitor != false) {
20+
addEvent(window, 'resize', function(e) {
21+
responsiveEnhance(img, width, false);
22+
});
23+
addEvent(img, 'load', function(e) {
24+
responsiveEnhance(img, width, false);
25+
});
26+
}
27+
};

0 commit comments

Comments
 (0)