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

Last change on this file since 14919 was 13923, checked in by flop25, 12 years ago

features added :

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