image-preview.js
1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
this.imagePreview = function()
{
xOffset = 30;
yOffset = 30;
that = this;
to = null;
function show (el, e)
{
el = $(el);
that.t = el.attr('data-title') || "";
that.d = el.attr('data-content') || "";
var c = "<h4>" + that.t + "</h4>";
var d = that.d ? "<p>" + that.d + "</p>" : '';
$("body").append("<div id='image-preview'><img src='"+ el.attr('data-image-preview') +"' alt='Image preview' class='img-responsive' />"+ c + d +"</div>");
$("#image-preview")
.css("top",(e.pageY - xOffset) + "px")
.css("left",(e.pageX + yOffset) + "px")
.fadeIn("fast");
moveIntoView('#image-preview', e.pageX, e.pageY, xOffset);
}
$('[data-toggle="image-preview"]').not('.kstych_init').addClass('kstych_init').on('mousemove', function(e)
{
that.t = "";
that.d = "";
tthat = this;
that.to = clearTimeout(that.to);
that.to = setTimeout(function(){
show(tthat, e);
}, 300);
})
.on('mouseleave', function()
{
that.t = "";
that.d = "";
that.to = clearTimeout(that.to);
$("#image-preview").remove();
});
function moveIntoView (elem, pageX, pageY, xOffset)
{
var w = $(window),
elem = $(elem),
docViewTop = w.scrollTop(),
docViewBottom = docViewTop + w.height(),
elTop = pageY,
elBottom = elTop + elem.outerHeight();
if(pageX + elem.outerWidth() > w.width())
{
elem.css("left", pageX - elem.outerWidth() - xOffset);
}
if((docViewTop <= elTop) && (docViewBottom >= elBottom))
{
elem.css("top", pageY - xOffset);
return;
}
elem.css("top", pageY - (elBottom - docViewBottom) - xOffset);
}
};
$(document).ready(function()
{
if (!Modernizr.touch)
imagePreview();
});