common.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * @Author: Len
  3. * @Date: 2018-09-05 09:58:27
  4. * @Last Modified by: Marte
  5. * @Last Modified time: 2019-07-23 18:10:14
  6. */
  7. //ajax对象构造函数
  8. /**
  9. * [Ajax description]
  10. * @param {[type]} options [请求方式(字符串)]
  11. * @param {[dataType]} options [返回数据的格式(字符串)]
  12. * @param {[data]} options [参数(对象)]
  13. * @param {[url]} options [请求地址(字符串)]
  14. * @param {[success]} options [成功回调(函数)]
  15. * @param {[error]} options [错误回调(函数)]
  16. * @param {[async]} options [是否同步(布尔)]
  17. * 例子:
  18. * ajax({
  19. url:
  20. data:
  21. type:
  22. dataType:
  23. success:
  24. error:
  25. async:
  26. });
  27. */
  28. function ajax(options){
  29. var _this = this;
  30. //异步请求对象的完成状态
  31. this.done = 0;
  32. this.format = function(){
  33. var now = new String(new Date().getTime());
  34. return now.substr(0,now.length-5);
  35. }
  36. //格式化参数
  37. this.formatParams = function(data) {
  38. //获取地址参数
  39. var arr = [];
  40. for (var name in data) {
  41. arr.push(encodeURIComponent(name) + "=" + encodeURIComponent(data[name]));
  42. }
  43. arr.push("t="+_this.format());//按分钟刷一次
  44. return arr.join("&");
  45. }
  46. //传入设置
  47. options = options || {};
  48. //请求方式
  49. options.type = (options.type || "GET").toUpperCase();
  50. options.dataType = options.dataType || "json";
  51. options.async = options.async || true;
  52. var params = _this.formatParams(options.data);
  53. //创建异步请求对象 - 第一步
  54. var xhr;
  55. //w3c标准
  56. if (window.XMLHttpRequest) {
  57. xhr = new XMLHttpRequest();
  58. }
  59. //兼容IE6及以下
  60. else if (window.ActiveObject) {
  61. xhr = new ActiveXObject('Microsoft.XMLHTTP');
  62. }
  63. //连接 和 发送 - 第二步
  64. //判断是那种类型的请求
  65. //若是get请求
  66. if (options.type == "GET") {
  67. //参数拼接
  68. if(options.url.indexOf("?")==-1) sp="?" ; else sp="&";
  69. //发送请求
  70. xhr.open("GET", options.url + sp + params,options.async);
  71. xhr.send(null);
  72. }
  73. //若是post请求
  74. else if (options.type == "POST") {
  75. //发送请求
  76. xhr.open("POST", options.url,options.async);
  77. //设置表单提交时的内容类型
  78. xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  79. //参数配置
  80. xhr.send(params);
  81. }
  82. //接收 - 第三步
  83. xhr.onreadystatechange = function() {
  84. if (xhr.readyState == 4) {
  85. //状态码
  86. var status = xhr.status;
  87. //状态码表示成功时,执行成功回调函数
  88. if (status >= 200 && status < 300 || status == 304) {
  89. //返回数据的格式
  90. //json字符串
  91. if (options.dataType == "json") {
  92. try{
  93. options.success && options.success(eval("("+xhr.responseText+")"));
  94. }
  95. catch(err){
  96. options.success && options.success(JSON.parse(xhr.responseText), xhr.responseXML);
  97. }
  98. }
  99. //普通字符串
  100. else {
  101. options.success && options.success(xhr.responseText, xhr.responseXML);
  102. }
  103. // 改变状态为完成
  104. _this.done = 1;
  105. }
  106. //如果状态码表示失败时调用错误处理回调函数
  107. else {
  108. options.error && options.error(status);
  109. // 改变状态为完成
  110. _this.done = 1;
  111. }
  112. }
  113. }
  114. }
  115. function setCookie(name,value,t){
  116. //document.cookie.setPath("/");
  117. var hour = t?t:8;
  118. var exp = new Date();
  119. exp.setTime(exp.getTime() + hour*60*60*1000);
  120. document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString()+";path=/";
  121. }
  122. function getCookie(name){
  123. //document.cookie.setPath("/");
  124. var arr, reg = new RegExp("(^| )"+name+"=([^;]*)(;|$)");
  125. if(arr=document.cookie.match(reg)){
  126. return unescape(arr[2]);
  127. }
  128. else{
  129. return null;
  130. }
  131. }
  132. /**
  133. * 获取url字段参数化
  134. * @param name 字段名
  135. */
  136. function getQueryString(name){
  137. var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  138. var r = window.location.search.substr(1).match(reg);
  139. if (r != null) return unescape(r[2]); return null;
  140. }
  141. //类名增加兼容
  142. function addClass(obj, cls){
  143. if (!hasClass(obj, cls)) obj.className += " " + cls
  144. }
  145. // 去除类名
  146. function removeClass(obj, cls){
  147. if (hasClass(obj, cls)) {
  148. var reg = new RegExp('(\\s|^)' + cls + '(\\s|$)');
  149. obj.className = obj.className.replace(reg, ' ')
  150. }
  151. }
  152. // 查看类名
  153. function hasClass(obj, cls){
  154. // console.log(obj,cls)
  155. return obj.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)'))
  156. }
  157. function tips(text, time) {//提示工具
  158. time = time ? time : 1500;
  159. var para = document.createElement("p");
  160. para.innerHTML = text;
  161. para.setAttribute("class", "w_tips");
  162. para.setAttribute("style", "display: block;border-radius: 9px;background-color: #287AFF;text-align: center;color: #FFF;padding: 7px 27px;font-size: 16px; position: fixed;left: 45%;top: 42%;z-index: 9999;");
  163. document.body.appendChild(para);
  164. para.style.marginLeft = -para.offsetWidth / 2+"px";
  165. setTimeout(function() {
  166. document.body.removeChild(para);
  167. }, time);
  168. };
  169. // 缓冲运动
  170. function animate(ele,opt,callback){
  171. //设置一个变量用于判断动画数量
  172. var timerLen = 0;
  173. for(var attr in opt){
  174. creatTimer(attr);
  175. //每加一个定时器,动画数加一
  176. timerLen++;
  177. }
  178. function creatTimer(attr){
  179. var timerName = attr + 'timer';console.log(timerName)
  180. var target = opt[attr];
  181. clearInterval(ele[timerName]);
  182. ele[timerName] = setInterval(function(){
  183. // 先获取当前值
  184. var current = getComputedStyle(ele)[attr];
  185. // 提取数值:单位
  186. // 根据当前值提取单位(单位在current最后面)
  187. var unit = current.match(/[a-z]+$/);
  188. if(unit){
  189. current = current.substring(0,unit.index)*1;
  190. unit = unit[0]
  191. }else{
  192. unit = '';
  193. current *= 1;
  194. }
  195. // 计算速度
  196. var speed = (target - current)/10;
  197. // 处理speed值,防止speed为小数而造成定时器无法完成的情况
  198. // 0.3=>1,-0.3=>-1
  199. speed = speed>0 ? Math.ceil(speed) : Math.floor(speed);
  200. //对于没有单位的属性单独处理
  201. if(attr == 'opacity'){
  202. speed = speed>0?0.05:-0.05;
  203. }
  204. if(current === target){console.log("清除定时器")
  205. clearInterval(ele[timerName]);
  206. current = target - speed;
  207. //每完成一个动画,timerLen减一
  208. timerLen--
  209. //最后若timerLen数量为零,则所有动画已经执行完再执行回调函数
  210. if(typeof callback ==='function'&&timerLen==0){
  211. callback();
  212. }
  213. }
  214. ele.style[attr] = current + speed + unit;
  215. },30)
  216. };
  217. }