//公共方法
var util = (function($){
/*提示组件 参数含义 1.状态 2.提示文字 3.多久消失*/
function tipHtml(state,text,time){
var html='
'
if(state){
if(state=='loading'){
html+='
'
}else{
html+='
'
}
}
html+='
'+ text +'
'
html+='
'
$("body").append(html)
if(time && typeof time=='number'){
setTimeout(function(){
$(".tipHtml").remove()
},time)
}
}
//旋转动画
function loading(ele){
html='';
ele.append(html)
}
function hideLoading(){
$(".loader-inner").remove()
}
//隐藏提示
function hideTip(){
$(".tipHtml").remove()
}
/* 传入的元素,屏蔽默认事件,
当多个元素滑动时,ios平台会出现bug ,
屏蔽默认事件,重新赋值事件 */
function overscroll(el) {
el.addEventListener('touchstart', function() {
var top = el.scrollTop
, totalScroll = el.scrollHeight
, currentScroll = top + el.offsetHeight;
if(top === 0) {
el.scrollTop = 1;
} else if(currentScroll === totalScroll) {
el.scrollTop = top - 1;
}
})
el.addEventListener('touchmove', function(evt) {
if(el.offsetHeight < el.scrollHeight)
evt._isScroller = true
})
document.body.addEventListener('touchmove', function(evt) {
if(!evt._isScroller) {
evt.preventDefault()
}
})
}
// 判断是否支持blob方式
var hasBlobConstructor = typeof Blob !== "undefined" &&
function() {
try {
return Boolean(new Blob)
} catch (e) {
return false
}
}();
var hasArrayBufferViewSupport = hasBlobConstructor && typeof Uint8Array !== "undefined" &&
function() {
try {
return new Blob([new Uint8Array(100)]).size === 100
} catch (e) {
return false
}
}();
function toBlob(canvas, type){
var dataURI = canvas.toDataURL(type);
var dataURIParts = dataURI.split(",");
var byteString = undefined;
if (dataURIParts[0].indexOf("base64") >= 0) {
byteString = atob(dataURIParts[1])
} else {
byteString = decodeURIComponent(dataURIParts[1])
}
var arrayBuffer = new ArrayBuffer(byteString.length);
var intArray = new Uint8Array(arrayBuffer);
for (var i = 0; i < byteString.length; i += 1) {
intArray[i] = byteString.charCodeAt(i)
}
var mimeString = dataURIParts[0].split(":")[1].split(";")[0];
var blob = null;
if (hasBlobConstructor) {
blob = new Blob([hasArrayBufferViewSupport ? intArray : arrayBuffer], {
type: mimeString
})
} else {
var bb = new BlobBuilder;
bb.append(arrayBuffer);
blob = bb.getBlob(mimeString)
}
return blob
}
//加载图片
function loadImage(image, file, callback){
if (typeof URL === "undefined") {
var reader = new FileReader;
reader.onload = function(evt) {
image.src = evt.target.result;
if (callback) {
callback()
}
};
reader.readAsDataURL(file)
} else {
image.src = URL.createObjectURL(file);
if (callback) {
callback()
}
}
}
//生成一个时间戳
function returnDataTime(){
return Date.parse(new Date())/1000
}
return {
tipHtml:tipHtml,
hideTip:hideTip,
toBlob:toBlob,
loadImage:loadImage,
returnDataTime:returnDataTime,
loading:loading,
hideLoading:hideLoading
}
})(jQuery)
module.exports = util;