-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjpreview.js
executable file
·74 lines (65 loc) · 2.11 KB
/
jpreview.js
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
/**
* 图片预览
* @author yangjian
*/
(function($) {
$.fn.jpreview = function() {
$.each(this, function(i, item) {
if($(item).attr('event-bind') == 'yes') {
return true;
}
$(item).attr('event-bind', 'yes');
$(item).on("click", function() {
var src = $(this).attr('data-src');
if ( !src ) {
alert("图片地址不存在,无法预览.");
return;
}
var timer = 500;
var $lock = $('<div style="position: absolute; background: #000000; opacity: .6; z-index: 666; display: none;"></div>');
var $imgBox = $('<div style="display: none; position: absolute; z-index: 999; "></div>');
var $img = $('<img src="" />');
$img.css({
"max-width" : $(window).width() - 100 + "px",
"max-height" : $(window).height() - 100 + "px"
});
var $closeBtn = $('<span style="position: absolute; font-size: 30px; border-radius: 40px; font-weight: bold;' +
'height: 40px; width: 40px; line-height: 35px; z-index: 999; background: #ffffff; text-align: center; cursor: pointer;">x</span>');
$closeBtn.css({
top : -20,
right : -20
});
$closeBtn.on('click', function() {
$lock.fadeOut(timer, function() {$lock.remove()});
$imgBox.fadeOut(timer, function() {$imgBox.remove()});
});
$lock.css({
left : 0,
top : 0,
width : $(document).width(),
height : $(document).height()
});
$lock.on('click', function() {
$lock.fadeOut(timer, function() {$lock.remove()});
$imgBox.fadeOut(timer, function() {$imgBox.remove()});
});
var _scrollTop = window.document.body.scrollTop || window.document.documentElement.scrollTop;
$imgBox.css({
top : $(window).height() / 2 + _scrollTop,
left : $(window).width() / 2,
});
$img.attr('src', src);
$img.on("load", function() {
$imgBox.css({
top : ($(window).height() - $imgBox.height()) / 2 + _scrollTop,
left : ($(window).width() - $imgBox.width()) / 2,
}).fadeIn(timer);
});
$imgBox.append($img);
$imgBox.append($closeBtn);
$('body').append($lock.fadeIn(timer));
$('body').append($imgBox);
});
});
}
})(jQuery);