首页 > 编程语言 > 详细

【JavaScript】分享一个我自己写的小工具类!

时间:2017-01-12 12:41:08      阅读:168      评论:0      收藏:0      [点我收藏+]

 

 

  1 function Alpha(){
  2 
  3 console.log(‘alpha:hello‘);
  4 
  5 this.lastclick = 0;
  6 this.hideNode = {};
  7 
  8 // 测试调用
  9 this.hello = function (){console.log(‘Hello, you called me.‘);}
 10 
 11 // 返回包含数组中所有键名的一个新数组
 12 this.array_keys = function (arr) {
 13     var RET = Array(), i = 0;
 14     for (var key in arr) {RET[i] = key;i++;}
 15     return RET;}
 16 
 17 // 判断是否数组
 18 this.isarray = function (arg) {
 19     if (arg === null) return false;
 20     if (typeof (arg) == "object") return true;
 21     return false;}
 22 
 23 // 页面跳转
 24 this.gocpage = function (cpage) {
 25     var ipage = document.getElementById(‘ipage‘);
 26     if (empty(ipage)) ipage = parent.document.getElementById(‘ipage‘);
 27     ipage.setAttribute("src", cpage.id + ".html" + suffix());
 28     function suffix() {return "?t=" + new Date().getTime();}}
 29 
 30 // 若变量已存在、非空字符串或者非零,则返回 false 值;反之返回 true。
 31 this.empty = function (arg) {
 32     if (arg === null) return true;
 33     var argtype = typeof (arg);
 34     switch (argtype) {
 35     case "undefined":return true;
 36     case "object":if (arg.length == 0) return true;return false;
 37     case "function":return false;
 38     case "boolean":if (arg === false) return true;return false;
 39     case "number":if (arg == 0) return true;return false;
 40     case "string":if (arg.length == 0) return true;return false;
 41     default:return false;}}
 42 
 43 // 隐藏自己
 44 this.hideself = function (node) {node.style.display = ‘none‘;}
 45 
 46 // 域名验证
 47 this.vali_domain = function (str) {
 48     var regdomain = /^[\w-]+\.[\w]+$|^[\w-]+\.[\w-]+\.[\w]+$|^[\w-]+\.[\w-]+\.[\w-]+\.[\w]+$/;
 49     if (regdomain.test(str)) return true;
 50     return false;}
 51 
 52 // 单击 vs 双击,传入2个回调函数,用户点击时自动触发
 53 this.clickcall = function (singleclick, doubleclick) {
 54     var clicktime = new Date().getTime();
 55     if (this.lastclick == 0) {
 56         this.lastclick = clicktime;
 57         window.setTimeout(cc, 360);}
 58     else { // 双击
 59         if ((clicktime - this.lastclick) < 300) {
 60             this.lastclick = 0;
 61             doubleclick();}}
 62     function cc() { // 单击
 63         if (this.lastclick == 0) return false;
 64         this.lastclick = 0;
 65         singleclick();}}
 66 
 67 // 返回url参数对象
 68 this.geturlarg = function () {
 69     var urlarg = new Object();
 70     var t_urlarg = document.URL.split(‘?‘);
 71     if (empty(t_urlarg[1])) return null;
 72     var tt_urlarg = t_urlarg[1].split(‘&‘);
 73     for (var i = 0; !empty(tt_urlarg[i]); i++) {
 74         var ttt_urlarg = tt_urlarg[i].split(‘=‘);
 75         if (empty(ttt_urlarg[0])) continue;
 76         urlarg[ttt_urlarg[0]] = empty(ttt_urlarg[1]) ? null : ttt_urlarg[1];}
 77     return urlarg;}
 78 
 79 // 鼠标覆盖
 80 this.mover = function (node) {node.style.background = ‘#CCCCCC‘;}
 81 
 82 // 鼠标离开
 83 this.mout = function (node) {node.style.background = ‘#ffffff‘;}
 84 
 85 // 获取字符串中的数字
 86 this.sel_num = function (str) {
 87     var regnum = /\d+/;
 88     var res = regnum.exec(str);
 89     return res[0];}
 90 
 91 // 计算2点间的距离
 92 this.cal_between = function (p1x, p1y, p2x, p2y) {return Math.sqrt(Math.pow(p1x - p2x, 2) + Math.pow(p1y - p2y, 2));}
 93 
 94 // 计算区域的中心
 95 this.cal_center = function (l, t, w, h) {
 96     var RES = new Object();
 97     RES[‘x‘] = l + w / 2;
 98     RES[‘y‘] = t + h / 2;
 99     return RES;}
100 
101 // ajax GET
102 this.get = function (url,callback){
103     if(this.empty(url)){console.log(‘url is undefined‘);return;}
104     if(this.empty(callback)){console.log(‘callback is undefined‘);return;}
105     var xmlhttp;
106     if (window.XMLHttpRequest) xmlhttp=new XMLHttpRequest();
107     else xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
108     xmlhttp.onreadystatechange=function(){
109         if(xmlhttp.readyState==4 && xmlhttp.status==200){
110             callback(eval(‘(‘ + xmlhttp.responseText + ‘)‘));}}
111     xmlhttp.open("GET",url,true);
112     xmlhttp.send();}
113     
114 // ajax POST
115 this.post = function (url,callback,arg){
116     if(this.empty(url)){console.log(‘url is undefined‘);return;}
117     if(this.empty(callback)){console.log(‘callback is undefined‘);return;}
118     var xmlhttp;
119     if (window.XMLHttpRequest) xmlhttp=new XMLHttpRequest();
120     else xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
121     xmlhttp.onreadystatechange=function(){
122         if(xmlhttp.readyState==4 && xmlhttp.status==200){
123             callback(eval(‘(‘ + xmlhttp.responseText + ‘)‘));}}
124     xmlhttp.open("POST",url,true);
125     xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
126     if(this.empty(arg)) xmlhttp.send();
127     else xmlhttp.send(arg);}
128 
129 // 隐藏body内的元素,呈现加载中
130 // 使用此方法需要加载 spin.js
131 this.loading = function(){
132     var spinmaxheight = window.screen.height;
133     var spinmaxwidth = window.screen.width;
134     var spintop = spinmaxheight/2;
135     var spinleft = spinmaxwidth/2;
136     if (spintop>100) spintop = 100;
137     var spinfooo = document.createElement(‘div‘);
138     spinfooo.id = ‘spinfooo‘;
139     var spinfooop = document.createElement(‘div‘);
140     spinfooop.appendChild(spinfooo);
141     spinfooop.style.position = ‘absolute‘;
142     spinfooop.style.top = spintop + ‘px‘;
143     spinfooop.style.left = spinleft + ‘px‘;
144     var opts = {
145         lines: 5,length: 12,width: 4,radius: 12,corners: 0.2,
146         rotate: 15,color: ‘#000‘,speed: 1.1,trail: 20,shadow: false,hwaccel: false,
147         className: ‘spinner‘,zIndex: 2e9,top: ‘auto‘,left: ‘auto‘};
148     for(var i=0,tmp;;){
149         tmp = document.body.children[i];
150         if (typeof(tmp) === ‘undefined‘) break;
151         if (tmp.style.display == ‘none‘) continue;
152         tmp.style.display = ‘none‘;
153         this.hideNode[i++] = tmp;}
154     document.body.appendChild(spinfooop);
155     var spinner = new Spinner(opts).spin(spinfooo);}
156 
157 // 呈现body内的元素,隐藏加载中
158 this.loaded = function(){
159     for(var i=0;;i++){
160         if (typeof(this.hideNode[i]) === ‘undefined‘) break;
161         this.hideNode[i].style.display = ‘‘;}
162     document.getElementById(‘spinfooo‘).style.display = ‘none‘;}
163 }

 

【JavaScript】分享一个我自己写的小工具类!

原文:http://www.cnblogs.com/myaliyun/p/6275363.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!