source: extensions/imgpreview/js/imgpreview.js @ 11079

Last change on this file since 11079 was 11079, checked in by flop25, 13 years ago

first commit :
works but may need to restrict the dimension

File size: 7.1 KB
Line 
1/*
2 * imgPreview jQuery plugin
3 * Copyright (c) 2009 James Padolsey
4 * j@qd9.co.uk | http://james.padolsey.com
5 * Dual licensed under MIT and GPL.
6 * Updated: 09/02/09
7 * @author James Padolsey
8 * @version 0.22
9 */
10/*
11(function(c){c.expr[':'].linkingToImage=function(a,g,e){return!!(c(a).attr(e[3])&&c(a).attr(e[3]).match(/\.(gif|jpe?g|png|bmp)$/i))};c.fn.imgPreview=function(j){var b=c.extend({imgCSS:{},distanceFromCursor:{top:10,left:10},preloadImages:true,onShow:function(){},onHide:function(){},onLoad:function(){},containerID:'imgPreviewContainer',containerLoadingClass:'loading',thumbPrefix:'',srcAttr:'href'},j),d=c('<div/>').attr('id',b.containerID).append('<img/>').hide().css('position','absolute').appendTo('body'),f=c('img',d).css(b.imgCSS),h=this.filter(':linkingToImage('+b.srcAttr+')');function i(a){return a.replace(/(\/?)([^\/]+)$/,'$1'+b.thumbPrefix+'$2')}if(b.preloadImages){(function(a){var g=new Image(),e=arguments.callee;g.src=i(c(h[a]).attr(b.srcAttr));g.onload=function(){h[a+1]&&e(a+1)}})(0)}h.mousemove(function(a){d.css({top:a.pageY+b.distanceFromCursor.top+'px',left:a.pageX+b.distanceFromCursor.left+'px'})}).hover(function(){var a=this;d.addClass(b.containerLoadingClass).show();f.load(function(){d.removeClass(b.containerLoadingClass);f.show();b.onLoad.call(f[0],a)}).attr('src',i(c(a).attr(b.srcAttr)));b.onShow.call(d[0],a)},function(){d.hide();f.unbind('load').attr('src','').hide();b.onHide.call(d[0],this)});return this}})(jQuery);
12*/
13/*
14 * imgPreview jQuery plugin
15 * Copyright (c) 2009 James Padolsey
16 * j@qd9.co.uk | http://james.padolsey.com
17 * Dual licensed under MIT and GPL.
18 * Updated: 09/02/09
19 * @author James Padolsey
20 * @version 0.22
21 *
22 *
23 * !!!!!!! -> This plugin contains ADDITIONS. 'considerBorders' param. You will have to add
24 * it yourself to newest version, if you use this param
25 */
26(function($){
27
28    $.expr[':'].linkingToImage = function(elem, index, match){
29        // This will return true if the specified attribute contains a valid link to an image:
30        return !! ($(elem).attr(match[3]) && $(elem).attr(match[3]).match(/\.(gif|jpe?g|png|bmp)$/i));
31    };
32
33    $.fn.imgPreview = function(userDefinedSettings){
34
35        var s = $.extend({
36
37            /* DEFAULTS */
38            minWidth: 32,
39            imgSrcHide: 'about:blank',
40            // CSS to be applied to image:
41            imgCSS: {},
42            // Distance between cursor and preview:
43            distanceFromCursor: {top:20, left:20},
44            // Boolean, whether or not to preload images:
45            preloadImages: true,
46            // Callback: run when link is hovered: container is shown:
47            onShow: function(){},
48            // Callback: container is hidden:
49            onHide: function(){},
50            // Callback: Run when image within container has loaded:
51            onLoad: function(){},
52            // ID to give to container (for CSS styling):
53            containerID: 'imgPreviewContainer',
54            // Class to be given to container while image is loading:
55            containerLoadingClass: 'loading',
56            // Prefix (if using thumbnails), e.g. 'thumb_'
57            thumbPrefix: '',
58            // Where to retrieve the image from:
59            srcAttr: 'href',
60            //The script moves the box depending on window borders
61            // Added by Ujeen
62            considerBorders: 'false'
63
64        }, userDefinedSettings),
65
66        $container = $('<div/>').attr('id', s.containerID)
67                        .append('<img/>').hide()
68                        .css('position','absolute')
69                        .appendTo('body'),
70
71        $img = $('img', $container).css(s.imgCSS),
72
73        // Get all valid elements (linking to images / ATTR with image link):
74        $collection = this.filter(':linkingToImage(' + s.srcAttr + ')');
75
76        // Re-usable means to add prefix (from setting):
77        function addPrefix(src) {
78            return src.replace(/(\/?)([^\/]+)$/,'$1' + s.thumbPrefix + '$2');
79        }
80
81        if (s.preloadImages) {
82            (function(i){
83                var tempIMG = new Image(),
84                    callee = arguments.callee;
85                tempIMG.src = addPrefix($($collection[i]).attr(s.srcAttr));
86                tempIMG.onload = function(){
87                    $collection[i + 1] && callee(i + 1);
88                };
89            })(0);
90        }
91
92        $collection
93            .mousemove(function(e){
94                // 'considerBorders' functionality
95                if (s.considerBorders == 'true') {
96                  if (($img.width() > 0) && ($img.height() > 0) || 1==1) {
97                      if (($img.width() > 0) && ($img.height() > 0))
98                        $img.css({'display':'block'});
99                    var cur_bot_dist = $(window).height() - e.clientY;
100                    var cur_lr_dist = $(window).width() - e.clientX;
101                    var position_y = e.pageY+s.distanceFromCursor.top+"px";
102                    var position_x = e.pageX+s.distanceFromCursor.left+"px";
103                    var box_height = $container.height()+50;
104                    var box_width = $container.width()+50;
105                   
106                    if (cur_bot_dist < (box_height)) {
107                      position_y = e.pageY-(box_height-cur_bot_dist)+"px";
108                    }
109                    if (e.clientY < s.distanceFromCursor.top) {
110                      position_y = e.pageY+"px";
111                    }
112                    if (cur_lr_dist < (box_width)) {
113                      position_x = e.pageX-(box_width+15)+"px";
114                    }
115                    $container.css({
116                        top: position_y,
117                        left: position_x
118                    });
119                  }
120                  // #--considerBorders
121                } else {
122                  $container.css({
123                      top: e.pageY + s.distanceFromCursor.top + 'px',
124                      left: e.pageX + s.distanceFromCursor.left + 'px'
125                  });
126                }
127
128            })
129            .hover(function(){
130
131                var link = this;
132                $container
133                    .addClass(s.containerLoadingClass)
134                    .show();
135                $img
136                    .load(function(){
137                        $container.removeClass(s.containerLoadingClass);
138                        $img.show();
139                        $container.width($img.width());
140                        s.onLoad.call($img[0], link);
141                    })
142                    .attr( 'src' , addPrefix($(link).attr(s.srcAttr)) )
143                    .$(function(){
144                      // 'considerBorders' functionality
145                      if (s.considerBorders == 'true') {
146                        $(this).css({'display':'none'});
147                      }
148                      // #--considerBorders
149                    });
150                   
151                s.onShow.call($container[0], link);
152
153            }, function(){
154
155                $container.hide();
156                $img.unbind('load').attr('src', s.imgSrcHide).hide();
157                $container.width(s.minWidth);
158                s.onHide.call($container[0], this);
159
160            });
161         
162        // Return full selection, not $collection!
163        return this;
164
165    };
166
167})(jQuery);
Note: See TracBrowser for help on using the repository browser.