| 1 |
(function ($) { |
| 2 |
$.fn.imageTooltip = function (options) { |
| 3 |
|
| 4 |
var defaults = { |
| 5 |
imgWidth: 'initial', |
| 6 |
backgroundColor: '#fff' |
| 7 |
}; |
| 8 |
|
| 9 |
if (typeof (options) === 'object') { |
| 10 |
options = $.extend(defaults, options); |
| 11 |
} else { |
| 12 |
var tempOptions = {}; |
| 13 |
tempOptions.imgWidth = arguments[0] || defaults.imgWidth; |
| 14 |
tempOptions.backgroundColor = arguments[1] || defaults.backgroundColor; |
| 15 |
options = tempOptions; |
| 16 |
} |
| 17 |
|
| 18 |
function calLeft(x, imgWidth) { |
| 19 |
return window.innerWidth - x > imgWidth ? x : x - imgWidth; |
| 20 |
} |
| 21 |
|
| 22 |
function calTop(y, imgHeight) { |
| 23 |
return window.innerHeight - y > imgHeight ? y + 25 : y - imgHeight - 25; |
| 24 |
} |
| 25 |
|
| 26 |
return this.each(function () { |
| 27 |
|
| 28 |
var imgContainer = $('<p>', { |
| 29 |
css: { |
| 30 |
display: 'none', |
| 31 |
backgroundColor: options.backgroundColor, |
| 32 |
padding: '5px', |
| 33 |
position: 'fixed', |
| 34 |
'max-width': '350px', |
| 35 |
'z-index': '9999' |
| 36 |
} |
| 37 |
}); |
| 38 |
|
| 39 |
var img = $('<img>', { |
| 40 |
src: $(this).data('image-tooltip') || $(this).attr('src'), |
| 41 |
alt: 'Image Not Available', |
| 42 |
width: options.imgWidth |
| 43 |
}); |
| 44 |
|
| 45 |
imgContainer.append(img); |
| 46 |
|
| 47 |
$(this).hover( |
| 48 |
function (e) { |
| 49 |
imgContainer.css({ |
| 50 |
left: calLeft(e.clientX, imgContainer.outerWidth()) + 'px', |
| 51 |
top: calTop(e.clientY, imgContainer.outerHeight()) + 'px' |
| 52 |
}); |
| 53 |
$('body').append(imgContainer); |
| 54 |
imgContainer.fadeIn('fast'); |
| 55 |
}, |
| 56 |
function () { |
| 57 |
imgContainer.remove(); |
| 58 |
} |
| 59 |
).mousemove(function (e) { |
| 60 |
imgContainer.css({ |
| 61 |
left: calLeft(e.clientX, imgContainer.outerWidth()) + 'px', |
| 62 |
top: calTop(e.clientY, imgContainer.outerHeight()) + 'px' |
| 63 |
}); |
| 64 |
}); |
| 65 |
}); |
| 66 |
}; |
| 67 |
}(jQuery)); |