ishoock.tools.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. //author: C, @ishoock group
  2. //www.ishoock.com
  3. //获取地址栏参数
  4. //query: query string
  5. function GetUriQyery(query) {
  6. var reg = new RegExp("(^|&)" + query + "=([^&]*)(&|$)");
  7. var r = decodeURI(window.location.href.split("?")[1]).match(reg);
  8. if (r != null) return unescape(r[2]); return null;
  9. }
  10. //预加载
  11. //Progress:function handler(progress){//使用当前进度},
  12. //Complete:function handler(){//加载完成后执行},
  13. //imglist:[uri1,uri2,uri3,....],
  14. //delay:nubmer
  15. function Load(Progress, Complete, imglist, delay) {
  16. var complete = 0;
  17. var count = imglist.length;
  18. var currentProgress = 0;
  19. function _animate() {
  20. var completeProgres = complete / count;
  21. currentProgress = (currentProgress + 0.01) < completeProgres ? (currentProgress + 0.01) : completeProgres;
  22. if (Math.floor(currentProgress * 100) < 100) {
  23. Progress(currentProgress);
  24. setTimeout(_animate, delay);
  25. } else {
  26. setTimeout(function () {
  27. Progress(1);
  28. setTimeout(function () {
  29. Complete();
  30. }, 200);
  31. }, delay);
  32. }
  33. }
  34. this.Start = function () {
  35. for (var i = 0; i < count; i++) {
  36. var img = new Image();
  37. img.onload = function () {
  38. complete++;
  39. if (!Progress && complete == count) {
  40. Complete();
  41. }
  42. };
  43. img.src = imglist[i];
  44. }
  45. if (Progress) {
  46. _animate();
  47. }
  48. }
  49. }
  50. //手势v0.2.1
  51. //*element:dom,
  52. //option:{
  53. // pointcount:1|2(default:1),
  54. // direction:'x'|'y'|'both'(default:'y'),p.s. pointcount:1 only,
  55. // step:number(default:50),
  56. // delay:ms(default:1000),
  57. // click:function,
  58. //},
  59. //complete:funtion
  60. function Gesture(element, option, complete) {
  61. var element = element ? element : document.body;
  62. var pointcount = option.pointcount ? (option.pointcount > 2 ? 2 : option.pointcount) : 1;
  63. var direction = option.direction ? option.direction : "y";
  64. var step = option.step ? option.step : 50;
  65. var delay = option.delay ? option.delay : 1000;
  66. var click = option.click;
  67. var points = [];
  68. var spacing = 0;
  69. var action = false;
  70. var delaying = false;
  71. var result;
  72. function _Start() {
  73. action = true;
  74. for (var i = 0; i < pointcount; i++) {
  75. points[i] = { x: event.touches[0].clientX, y: event.touches[0].clientY };
  76. }
  77. if (pointcount == 2) {
  78. var x = Math.abs(points[0].x - points[1], x);
  79. var y = Math.abs(points[0].y - points[1], y);
  80. spacing = Math.sqrt(x * x + y * y);
  81. }
  82. return false;
  83. }
  84. function _Move() {
  85. event.preventDefault();
  86. event.stopPropagation();
  87. if (action && !delaying) {
  88. var _points = [];
  89. for (var i = 0; i < pointcount; i++) {
  90. _points[i] = { x: event.touches[0].clientX, y: event.touches[0].clientY };
  91. }
  92. result = 0;
  93. if (pointcount == 1) {
  94. var move;
  95. if (direction == "both") {
  96. if (Math.abs(points[0].x - _points[0].x) > Math.abs(points[0].y - _points[0].y)) {
  97. move = points[0].x - _points[0].x;
  98. if (move > step) {
  99. result = 1;
  100. } else if (move < -step) {
  101. result = -1;
  102. }
  103. result = { "code": result, "direction": "x" };
  104. } else {
  105. move = points[0].y - _points[0].y;
  106. if (move > step) {
  107. result = 1;
  108. } else if (move < -step) {
  109. result = -1;
  110. }
  111. result = { "code": result, "direction": "y" };
  112. }
  113. } else {
  114. move = direction == "x" ? points[0].x - _points[0].x : points[0].y - _points[0].y;
  115. if (move > step) {
  116. result = 1;
  117. } else if (move < -step) {
  118. result = -1;
  119. }
  120. }
  121. } else if (pointcount == 2) {
  122. var x = Math.abs(_points[0].x - _points[1].x);
  123. var y = Math.abs(_points[0].y - _points[1].y);
  124. var _spacing = Math.sqrt(x * x + y * y);
  125. var change = spacing - _spacing;
  126. if (change < -step) {
  127. result = 1;
  128. } else if (change > step) {
  129. result = -1;
  130. }
  131. }
  132. if (direction != "both") {
  133. if (result != 0) {
  134. delaying = true;
  135. action = false;
  136. complete(result);
  137. setTimeout(function () {
  138. delaying = false;
  139. }, delay);
  140. }
  141. } else {
  142. if (result.code != 0) {
  143. delaying = true;
  144. action = false;
  145. complete(result);
  146. setTimeout(function () {
  147. delaying = false;
  148. }, delay);
  149. }
  150. }
  151. }
  152. return false;
  153. }
  154. function _End() {
  155. action = false;
  156. if (click) {
  157. click(event);
  158. }
  159. return false;
  160. }
  161. this.Init = function () {
  162. $(element).on("touchstart", _Start);
  163. $(element).on("touchmove", _Move);
  164. $(element).on("touchend", _End);
  165. return this;
  166. };
  167. this.Kill = function () {
  168. $(element).off("touchstart");
  169. $(element).off("touchmove");
  170. $(element).off("touchend");
  171. }
  172. }
  173. // 限制字符串长度(区分大小写);
  174. String.prototype.Limit = function (max) {
  175. var _length = 0, len = this.length, charCode = -1;
  176. var str = "";
  177. for (var i = 0; i < len; i++) {
  178. charCode = this.charCodeAt(i);
  179. if (charCode >= 0 && charCode <= 128) {
  180. _length += 1;
  181. }
  182. else {
  183. _length += 2;
  184. }
  185. if (_length <= max) {
  186. str += this[i];
  187. } else {
  188. break;
  189. }
  190. }
  191. return str;
  192. }
  193. //字符串真实长度
  194. String.prototype.Length= function () {
  195. var _length = 0, len = this.length, charCode = -1;
  196. var str = "";
  197. for (var i = 0; i < len; i++) {
  198. charCode = this.charCodeAt(i);
  199. if (charCode >= 0 && charCode <= 128) {
  200. _length += 1;
  201. }
  202. else {
  203. _length += 2;
  204. }
  205. }
  206. return _length;
  207. }
  208. //设置cookies
  209. function setCookie(name, value) {
  210. var Days = 30;
  211. var exp = new Date();
  212. exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
  213. document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
  214. }
  215. //读取cookies
  216. function getCookie(name) {
  217. var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
  218. if (arr = document.cookie.match(reg))
  219. return unescape(arr[2]);
  220. else
  221. return null;
  222. }
  223. //获取手机系统
  224. // function GetDevice() {
  225. // var u = navigator.userAgent;
  226. // if (u.indexOf('Android') > -1 || u.indexOf('Linux') > -1) {
  227. // return "android";
  228. // } else if (u.indexOf('iPhone') > -1) {
  229. // return "iphone";
  230. // } else if (u.indexOf('Windows Phone') > -1) {
  231. // return "windowsphone";
  232. // } else if (u.indexOf('iPad') > -1) {
  233. // return "ipad";
  234. // } else {
  235. // return "other";
  236. // }
  237. // }
  238. //扇形
  239. CanvasRenderingContext2D.prototype.sector = function (x, y, radius, sDeg, eDeg) {
  240. this.save();
  241. this.translate(x, y);
  242. this.beginPath();
  243. this.arc(0, 0, radius, sDeg, eDeg);
  244. this.save();
  245. this.rotate(eDeg);
  246. this.moveTo(radius, 0);
  247. this.lineTo(0, 0);
  248. this.restore();
  249. this.rotate(sDeg);
  250. this.lineTo(radius, 0);
  251. this.closePath();
  252. this.restore();
  253. return this;
  254. }