util.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //公共方法
  2. var util = (function($){
  3. /*提示组件 参数含义 1.状态 2.提示文字 3.多久消失*/
  4. function tipHtml(state,text,time){
  5. var html='<div class="tipHtml">'
  6. if(state){
  7. if(state=='loading'){
  8. html+='<div class="icon weui-icon_toast weui-icon-'+state+'"></div>'
  9. }else{
  10. html+='<div class="icon weui-icon-'+state+'"></div>'
  11. }
  12. }
  13. html+='<div>'+ text +'</div>'
  14. html+='</div>'
  15. $("body").append(html)
  16. if(time && typeof time=='number'){
  17. setTimeout(function(){
  18. $(".tipHtml").remove()
  19. },time)
  20. }
  21. }
  22. //旋转动画
  23. function loading(ele){
  24. html='<div class="loader-inner ball-clip-rotate">'
  25. html+='<div class="weui-icon-loading"></div>'
  26. html+='</div>';
  27. ele.append(html)
  28. }
  29. function hideLoading(){
  30. $(".loader-inner").remove()
  31. }
  32. //隐藏提示
  33. function hideTip(){
  34. $(".tipHtml").remove()
  35. }
  36. /* 传入的元素,屏蔽默认事件,
  37. 当多个元素滑动时,ios平台会出现bug ,
  38. 屏蔽默认事件,重新赋值事件 */
  39. function overscroll(el) {
  40. el.addEventListener('touchstart', function() {
  41. var top = el.scrollTop
  42. , totalScroll = el.scrollHeight
  43. , currentScroll = top + el.offsetHeight;
  44. if(top === 0) {
  45. el.scrollTop = 1;
  46. } else if(currentScroll === totalScroll) {
  47. el.scrollTop = top - 1;
  48. }
  49. })
  50. el.addEventListener('touchmove', function(evt) {
  51. if(el.offsetHeight < el.scrollHeight)
  52. evt._isScroller = true
  53. })
  54. document.body.addEventListener('touchmove', function(evt) {
  55. if(!evt._isScroller) {
  56. evt.preventDefault()
  57. }
  58. })
  59. }
  60. // 判断是否支持blob方式
  61. var hasBlobConstructor = typeof Blob !== "undefined" &&
  62. function() {
  63. try {
  64. return Boolean(new Blob)
  65. } catch (e) {
  66. return false
  67. }
  68. }();
  69. var hasArrayBufferViewSupport = hasBlobConstructor && typeof Uint8Array !== "undefined" &&
  70. function() {
  71. try {
  72. return new Blob([new Uint8Array(100)]).size === 100
  73. } catch (e) {
  74. return false
  75. }
  76. }();
  77. function toBlob(canvas, type){
  78. var dataURI = canvas.toDataURL(type);
  79. var dataURIParts = dataURI.split(",");
  80. var byteString = undefined;
  81. if (dataURIParts[0].indexOf("base64") >= 0) {
  82. byteString = atob(dataURIParts[1])
  83. } else {
  84. byteString = decodeURIComponent(dataURIParts[1])
  85. }
  86. var arrayBuffer = new ArrayBuffer(byteString.length);
  87. var intArray = new Uint8Array(arrayBuffer);
  88. for (var i = 0; i < byteString.length; i += 1) {
  89. intArray[i] = byteString.charCodeAt(i)
  90. }
  91. var mimeString = dataURIParts[0].split(":")[1].split(";")[0];
  92. var blob = null;
  93. if (hasBlobConstructor) {
  94. blob = new Blob([hasArrayBufferViewSupport ? intArray : arrayBuffer], {
  95. type: mimeString
  96. })
  97. } else {
  98. var bb = new BlobBuilder;
  99. bb.append(arrayBuffer);
  100. blob = bb.getBlob(mimeString)
  101. }
  102. return blob
  103. }
  104. //加载图片
  105. function loadImage(image, file, callback){
  106. if (typeof URL === "undefined") {
  107. var reader = new FileReader;
  108. reader.onload = function(evt) {
  109. image.src = evt.target.result;
  110. if (callback) {
  111. callback()
  112. }
  113. };
  114. reader.readAsDataURL(file)
  115. } else {
  116. image.src = URL.createObjectURL(file);
  117. if (callback) {
  118. callback()
  119. }
  120. }
  121. }
  122. //生成一个时间戳
  123. function returnDataTime(){
  124. return Date.parse(new Date())/1000
  125. }
  126. return {
  127. tipHtml:tipHtml,
  128. hideTip:hideTip,
  129. toBlob:toBlob,
  130. loadImage:loadImage,
  131. returnDataTime:returnDataTime,
  132. loading:loading,
  133. hideLoading:hideLoading
  134. }
  135. })(jQuery)
  136. module.exports = util;