jquery.hp.plugin.js
2.28 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/**
* 对myPlugin 进行扩展了一些工具类
*/
$.myPlugin = {
/**
* bootstrap中的样式
*/
bootstrapClass : {
DEFAULT : "default",
PRIMARY : "primary",
SUCCESS : "success",
INFO : "info",
WARNING : "warning",
DANGER : "danger",
LINK : "link"
},
/**
* 显示
* @param jq
* @param option
* @returns
*/
show : function(jq, option) {
var opt = jq.data("panel");
if (!option) {
$(jq).show();
} else if (option.effect === "slide") {
$(jq).slideDown(option.speed, option.callback);
} else if (option.effect === "fade") {
$(jq).fadeIn(option.speed, option.callback);
} else {
$(jq).show(option.speed, option.callback);
}
},
/**
* 隐藏
* @param jq
* @param option
* @returns
*/
hide : function(jq, option) {
var opt = jq.data("panel");
if (!option) {
$(jq).hide();
} else if (option.effect === "slide") {
$(jq).slideUp(option.speed, option.callback);
} else if (option.effect === "fade") {
$(jq).fadeOut(option.speed, option.callback);
} else {
$(jq).hide(option.speed, option.callback);
}
},
/**
* 图片转为base64
*/
convertImgToBase64_bak : function(url, callback) {
var canvas = document.createElement('CANVAS');
var ctx = canvas.getContext('2d');
var img = new Image;
img.crossOrigin = '';
img.src = url;
img.onload = function() {
canvas.height = img.height;
canvas.width = img.width;
ctx.drawImage(img, 0, 0, img.width, img.height);
var ext = img.src.substring(img.src.lastIndexOf(".")+1).toLowerCase();
var dataURL = canvas.toDataURL("image/"+ext);
callback.call(this, dataURL);
//canvas = null;
};
},
/**
* 图片转为base64
*/
convertImgToBase64 : function(files, callback) {
var file = $(files).get(0);
if (!file || file.files.length == 0) {
return;
}
var arr = [];
var self = this;
$(file.files).each(function(index, item) {
var imgFile = new FileReader();
imgFile.readAsDataURL(item);
imgFile.onload = function () {
var imgData = this.result; //base64数据
//callback.call(this, imgData);
arr.push({
url : imgData,
name : item.name
});
}
});
var interval = window.setInterval(function() {
if (arr.length == file.files.length) {
clearInterval(interval);
callback.call(self, arr);
}
}, 100);
}
};