123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- //公共方法
- var util = (function($){
- /*提示组件 参数含义 1.状态 2.提示文字 3.多久消失*/
- function tipHtml(state,text,time){
- var html='<div class="tipHtml">'
- if(state){
- if(state=='loading'){
- html+='<div class="icon weui-icon_toast weui-icon-'+state+'"></div>'
- }else{
- html+='<div class="icon weui-icon-'+state+'"></div>'
- }
- }
- html+='<div>'+ text +'</div>'
- html+='</div>'
- $("body").append(html)
- if(time && typeof time=='number'){
- setTimeout(function(){
- $(".tipHtml").remove()
- },time)
- }
- }
- //旋转动画
- function loading(ele){
- html='<div class="loader-inner ball-clip-rotate">'
- html+='<div class="weui-icon-loading"></div>'
- html+='</div>';
- 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;
|