responsive_image_tag 0.2.2 → 0.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +62 -4
- data/VERSION +1 -1
- data/lib/generators/responsive_image_tag/jquery_generator.rb +1 -1
- data/responsive_image_tag.gemspec +1 -1
- metadata +3 -3
data/README.rdoc
CHANGED
@@ -1,5 +1,51 @@
|
|
1
1
|
= responsive_image_tag
|
2
2
|
|
3
|
+
== Creating responsive images
|
4
|
+
|
5
|
+
The @responsive_image_tag@ helper selects an image depending on the width of
|
6
|
+
the current device. The @screen.width@ is detected by javascript and then an
|
7
|
+
image element is inserted in the page with a dynamically created @src@
|
8
|
+
attribute.
|
9
|
+
|
10
|
+
It works with Rails 2 and Rails 3, and there are javascript adapters for both Jquery and Prototype. Pick your poison.
|
11
|
+
|
12
|
+
== HTML structure
|
13
|
+
|
14
|
+
Here's the HTML which the helper inserts into the page:
|
15
|
+
|
16
|
+
<span class="img-placeholder"></span>
|
17
|
+
<noscript data-mobilesrc="small.jpg" data-fullsrc="big.jpg" data-alttext="your alt text" class="responsivize">
|
18
|
+
<img src="big.jpg">
|
19
|
+
</noscript>
|
20
|
+
|
21
|
+
The responsive images have to appear for non-javascript users as well as
|
22
|
+
those with javascript turned on.
|
23
|
+
|
24
|
+
To achieve this the helper places the default image inside a <noscript>
|
25
|
+
tag which is then deleted by the javascript library. The image attributes
|
26
|
+
such as full size, mobile size and alt text are also stored as HTML5 data
|
27
|
+
attributes on the <noscript> tag so they are available in the DOM for the
|
28
|
+
javascript to access.
|
29
|
+
|
30
|
+
The library relies on a trick which has all the elegance of a greasy
|
31
|
+
late-night kebab: child elements of the <noscript> tag are not added to the
|
32
|
+
DOM, so deleting the <noscript> prevents an HTTP request being made to the
|
33
|
+
server. This way only the image being requested by the dynamically inserted image tag is making a request.
|
34
|
+
|
35
|
+
To insert the dynamically created image element into the page you need a
|
36
|
+
parent element in the DOM to append to. The rails helper also creates a
|
37
|
+
<span> tag with a class of "img-placeholder" to house the new image.
|
38
|
+
|
39
|
+
When the DOM is ready the javascript object responsiveImageTag detects
|
40
|
+
all <noscript> elements on the page with a class of "responsivize".
|
41
|
+
|
42
|
+
It retrieves the HTML5 data attributes (@data-mobilesrc@, @data-fullsrc@, and
|
43
|
+
@data-alttext@) and then creates an image tag with the correct source
|
44
|
+
depending on the available screen width of the users device. Any device
|
45
|
+
smaller than 768px is considered to be a mobile device. The library
|
46
|
+
inserts the new element into the page and the <noscript> tags are then hidden
|
47
|
+
from view.
|
48
|
+
|
3
49
|
== Installation
|
4
50
|
|
5
51
|
Put it in your Gemfile:
|
@@ -17,19 +63,31 @@ You can run it like this:
|
|
17
63
|
|
18
64
|
=== Rails 3
|
19
65
|
|
20
|
-
rails g responsive_image_tag:
|
66
|
+
rails g responsive_image_tag:jquery
|
67
|
+
|
68
|
+
or
|
69
|
+
|
70
|
+
rails g responsive_image_tag:prototype
|
21
71
|
|
22
72
|
=== Rails 2
|
23
73
|
|
24
|
-
script/generate
|
74
|
+
script/generate jquery_responsive_image_tag
|
75
|
+
|
76
|
+
or
|
77
|
+
|
78
|
+
script/generate prototype_responsive_image_tag
|
25
79
|
|
26
80
|
|
27
81
|
== Usage
|
28
82
|
|
29
83
|
You need to do two things. First, make sure that you include a reference to the JavaScript file in your layout:
|
30
84
|
|
31
|
-
<%= javascript_include_tag "responsive-image-tag" %>
|
32
|
-
|
85
|
+
<%= javascript_include_tag "responsive-image-tag-jquery" %>
|
86
|
+
|
87
|
+
or
|
88
|
+
|
89
|
+
<%= javascript_include_tag "responsive-image-tag-prototype" %>
|
90
|
+
|
33
91
|
After that, you can use the new tag like so, in a view:
|
34
92
|
|
35
93
|
<%= responsive_image_tag("small.jpg", "big.jpg") %>
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.3
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: responsive_image_tag
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 17
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 2
|
9
|
-
-
|
10
|
-
version: 0.2.
|
9
|
+
- 3
|
10
|
+
version: 0.2.3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Dave Hrycyszyn
|