Subversion Repositories Integrator Subversion

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 espaco 1
/*!
2
        Zoom v1.7.11 - 2013-11-12
3
        Enlarge images on click or mouseover.
4
        (c) 2013 Jack Moore - http://www.jacklmoore.com/zoom
5
        license: http://www.opensource.org/licenses/mit-license.php
6
*/
7
(function ($) {
8
        var defaults = {
9
                url: false,
10
                callback: false,
11
                target: false,
12
                duration: 120,
13
                on: 'mouseover', // other options: grab, click, toggle
14
                touch: true, // enables a touch fallback
15
                onZoomIn: false,
16
                onZoomOut: false,
17
                magnify: 1
18
        };
19
 
20
        // Core Zoom Logic, independent of event listeners.
21
        $.zoom = function(target, source, img, magnify) {
22
                var targetHeight,
23
                        targetWidth,
24
                        sourceHeight,
25
                        sourceWidth,
26
                        xRatio,
27
                        yRatio,
28
                        offset,
29
                        position = $(target).css('position');
30
 
31
                // The parent element needs positioning so that the zoomed element can be correctly positioned within.
32
                $(target).css({
33
                        position: /(absolute|fixed)/.test(position) ? position : 'relative',
34
                        overflow: 'hidden'
35
                });
36
 
37
                img.style.width = img.style.height = '';
38
 
39
                $(img)
40
                        .addClass('zoomImg')
41
                        .css({
42
                                position: 'absolute',
43
                                top: 0,
44
                                left: 0,
45
                                opacity: 0,
46
                                width: img.width * magnify,
47
                                height: img.height * magnify,
48
                                border: 'none',
49
                                maxWidth: 'none'
50
                        })
51
                        .appendTo(target);
52
 
53
                return {
54
                        init: function() {
55
                                targetWidth = $(target).outerWidth();
56
                                targetHeight = $(target).outerHeight();
57
 
58
                                if (source === target) {
59
                                        sourceWidth = targetWidth;
60
                                        sourceHeight = targetHeight;
61
                                } else {
62
                                        sourceWidth = $(source).outerWidth();
63
                                        sourceHeight = $(source).outerHeight();
64
                                }
65
 
66
                                xRatio = (img.width - targetWidth) / sourceWidth;
67
                                yRatio = (img.height - targetHeight) / sourceHeight;
68
 
69
                                offset = $(source).offset();
70
                        },
71
                        move: function (e) {
72
                                var left = (e.pageX - offset.left),
73
                                        top = (e.pageY - offset.top);
74
 
75
                                top = Math.max(Math.min(top, sourceHeight), 0);
76
                                left = Math.max(Math.min(left, sourceWidth), 0);
77
 
78
                                img.style.left = (left * -xRatio) + 'px';
79
                                img.style.top = (top * -yRatio) + 'px';
80
                        }
81
                };
82
        };
83
 
84
        $.fn.zoom = function (options) {
85
                return this.each(function () {
86
                        var
87
                        settings = $.extend({}, defaults, options || {}),
88
                        //target will display the zoomed image
89
                        target = settings.target || this,
90
                        //source will provide zoom location info (thumbnail)
91
                        source = this,
92
                        img = document.createElement('img'),
93
                        $img = $(img),
94
                        mousemove = 'mousemove.zoom',
95
                        clicked = false,
96
                        touched = false,
97
                        $urlElement;
98
 
99
                        // If a url wasn't specified, look for an image element.
100
                        if (!settings.url) {
101
                                $urlElement = $(source).find('img');
102
                                if ($urlElement[0]) {
103
                                        settings.url = $urlElement.data('src') || $urlElement.attr('src');
104
                                }
105
                                if (!settings.url) {
106
                                        return;
107
                                }
108
                        }
109
 
110
                        img.onload = function () {
111
                                var zoom = $.zoom(target, source, img, settings.magnify);
112
 
113
                                function start(e) {
114
                                        zoom.init();
115
                                        zoom.move(e);
116
 
117
                                        // Skip the fade-in for IE8 and lower since it chokes on fading-in
118
                                        // and changing position based on mousemovement at the same time.
119
                                        $img.stop()
120
                                        .fadeTo($.support.opacity ? settings.duration : 0, 1, $.isFunction(settings.onZoomIn) ? settings.onZoomIn.call(img) : false);
121
                                }
122
 
123
                                function stop() {
124
                                        $img.stop()
125
                                        .fadeTo(settings.duration, 0, $.isFunction(settings.onZoomOut) ? settings.onZoomOut.call(img) : false);
126
                                }
127
 
128
                                // Mouse events
129
                                if (settings.on === 'grab') {
130
                                        $(source)
131
                                                .on('mousedown.zoom',
132
                                                        function (e) {
133
                                                                if (e.which === 1) {
134
                                                                        $(document).one('mouseup.zoom',
135
                                                                                function () {
136
                                                                                        stop();
137
 
138
                                                                                        $(document).off(mousemove, zoom.move);
139
                                                                                }
140
                                                                        );
141
 
142
                                                                        start(e);
143
 
144
                                                                        $(document).on(mousemove, zoom.move);
145
 
146
                                                                        e.preventDefault();
147
                                                                }
148
                                                        }
149
                                                );
150
                                } else if (settings.on === 'click') {
151
                                        $(source).on('click.zoom',
152
                                                function (e) {
153
                                                        if (clicked) {
154
                                                                // bubble the event up to the document to trigger the unbind.
155
                                                                return;
156
                                                        } else {
157
                                                                clicked = true;
158
                                                                start(e);
159
                                                                $(document).on(mousemove, zoom.move);
160
                                                                $(document).one('click.zoom',
161
                                                                        function () {
162
                                                                                stop();
163
                                                                                clicked = false;
164
                                                                                $(document).off(mousemove, zoom.move);
165
                                                                        }
166
                                                                );
167
                                                                return false;
168
                                                        }
169
                                                }
170
                                        );
171
                                } else if (settings.on === 'toggle') {
172
                                        $(source).on('click.zoom',
173
                                                function (e) {
174
                                                        if (clicked) {
175
                                                                stop();
176
                                                        } else {
177
                                                                start(e);
178
                                                        }
179
                                                        clicked = !clicked;
180
                                                }
181
                                        );
182
                                } else if (settings.on === 'mouseover') {
183
                                        zoom.init(); // Preemptively call init because IE7 will fire the mousemove handler before the hover handler.
184
 
185
                                        $(source)
186
                                                .on('mouseenter.zoom', start)
187
                                                .on('mouseleave.zoom', stop)
188
                                                .on(mousemove, zoom.move);
189
                                }
190
 
191
                                // Touch fallback
192
                                if (settings.touch) {
193
                                        $(source)
194
                                                .on('touchstart.zoom', function (e) {
195
                                                        e.preventDefault();
196
                                                        if (touched) {
197
                                                                touched = false;
198
                                                                stop();
199
                                                        } else {
200
                                                                touched = true;
201
                                                                start( e.originalEvent.touches[0] || e.originalEvent.changedTouches[0] );
202
                                                        }
203
                                                })
204
                                                .on('touchmove.zoom', function (e) {
205
                                                        e.preventDefault();
206
                                                        zoom.move( e.originalEvent.touches[0] || e.originalEvent.changedTouches[0] );
207
                                                });
208
                                }
209
 
210
                                if ($.isFunction(settings.callback)) {
211
                                        settings.callback.call(img);
212
                                }
213
                        };
214
 
215
                        img.src = settings.url;
216
 
217
                        $(source).one('zoom.destroy', function(){
218
                                $(source).off(".zoom");
219
                                $img.remove();
220
                        });
221
                });
222
        };
223
 
224
        $.fn.zoom.defaults = defaults;
225
}(window.jQuery));