阅读 0

封装

//封装typeof function myType(val) { var ty = typeof(val), toStr = Object.prototype.toString, res = { '[object Array]': 'array', '[object Object]': 'object', '[object Number]': 'object number', '[object String]': 'object string', '[object Boolean]': 'object boolean' }; if (val === null) { return 'null' } else if (ty === 'object') { var ko = toStr.call(val); return res[ko]; } return ty; }

//滚动条的兼容模式 function getscroll() { if (window.pageXOffset) { return { left: window.pageXOffset, top: window.pageYOffset } } else { return { left: document.body.scrollLeft + document.documentElement.scrollLeft, top: document.body.scrollTop + document.documentElement.scrollTop } } }

//可视区域的尺寸的兼容模式 function getviewport() { if (window.innerHeight) { return { widthX: window.innerWidth, heightY: window.innerHeight, } } else {

	if (document.compatMode === 'BackCompat') {
		return {

			widthX: document.body.clientWidth,
			heightY: document.body.clientHeight
		}
	} else {
		return {
			widthX: document.documentElement.clientWidth,
			heightY: document.documentElement.clientHeight
		}
	}
}
复制代码

} //封装查看计算样式 function getComstyle(ele, pro) { if (window.getComputedStyle) { if (pro) { return window.getComputedStyle(ele, null)[pro]; } else { return window.getComputedStyle(ele, null); } } else { if (pro) { return ele.currentStyle(ele, null)[pro]; } else { return ele.currentStyle(ele, null); } } } //深拷贝 function deepclone(origin, target) { var target = target || {}, toStr = Object.prototype.toString, arrType = '[object Array]'; for (var key in origin) { if (origin.hasOwnProperty(key)) { if (typeof(origin[key]) === 'object' && origin[key] !== null) { toStr.call(origin[key]) === arrType ? target[key] = [] : target[key] = {}; deepclone(origin[key], target[key]) } else { target[key] = origin[key]; } }

}
//封装事件监听处理程序
			function addEvent(ele,type,fn){
				if(ele.addEventlistener){
					ele.addEventlistener(type,fn,false)
				}else if(ele.attachEvent){
					ele.attachEvent("on"+type,function(){
						fn.call(ele)
					})
				}else{
					ele["on"+type]=fn;
				}
			}
			//封装取消冒泡事件
			function stopEvent(e){
				var e=e ||window.event;
				if(e.stopPropagation){
					e.stopPropagation()
				}else{
					e.cancelBubble=true;
				}
				
			}
			
//
		function getscrollPage(){
			if(window.pageXOffset){
				return {
					left:window.pageXOffset,
					top:window.pageYOffset
				}
			}else{
				return {
					left:document.body.scrollLeft + document.documentElement.scrollLeft,
					top:document.body.scrollTop + document.documentElement.scrollTop
				}
				
			}
		}
		function pagePos(e) {
			var sLeft=getscrollPage().left,
			    sTop=getscrollPage().top,
		       cLeft=document.documentElement.clientLeft || 0,
			   cTop=document.documentElement.clientTop || 0;
				return {
					X:e.clientX + sLeft -cLeft,
					Y:e.clientY + sTop -cTop
				}
			
		}
		
		//promiso
		function getpromose(fpath) {
		    var promise = new Promise(function(resolve, reject) {
		        fs.readFile(fpath, 'utf-8', (err, dataser) => {
		            if (err) return reject(err)
		            resolve(dataset)
		        })
		    })
		    return promise
		}
		vat go = getpromose('./1.text');
		go.then(function(data) {
		        console.log(data)
		    },
		    function(err) {
		        console.log(err)
		    }
		)
		$(function(){
			$('#btn').on('click',function(){
				$.ajax({
					url:'hu.json',
					type:'get',
					dataType:"json",
					success:function(data){
						console.log(data)
					}
				
				})
				
				
				     function getDate(type, url) {
				            var xhr = new XMLHttpRequest();
				            xhr.open(type, url, true);
				            xhr.send();
				            xhr.onreadystatechange = function() {
				                if (xhr.readyState === 4 && xhr.status === 200) {
				                    console.log(JSON.parse(xhr.responseText))
				                }
				            }
				        }
		
		        function ajax(option) {
		            var xhr = new XMLHttpRequest(),
		                type = option.type || "GET",
		                asy = option.asy || true,
		                data = option.data || {},
		                url = option.url,
		                str = '?';
		
		            for (var key in data) {
		                str += key + '=' + data[key] + "&"
		            }
		
		            str = str.slice(0, str.length - 1);
		            url += str;
		
		            xhr.onreadystatechange = function() {
		                if (xhr.readyState === 4 && xhr.status === 200) {
		                    var json = JSON.parse(xhr.responseText)
		                    option.success(json)
		                } else {
		                    option.err("获取数据失败")
		                }
		            }
		            
		            xhr.open(type, url, asy);
		            xhr.send();
		
		        }
		
		        ajax({
		            type: 'GET',
		            data: {
		                "pageNum": 1,
		                "pageSize": 10
		            },
		            url: "http://132.232.94.151:3000/api/film/getList",
		            success: function(data) {
		                for (var i = 0; i < data.films.length; i++) {
		                    var li = '<li>' + data.films[i].name + '</li>'
		                    list.insertAdjacentHTML('beforeend', li);
		                }
		            }
		
		        })

    function ajax(option) {
        var xhr = new XMLHttpRequest(),
            type = option.type || "GET",
            asy = option.asy || true,
            data = option.data || {},
            url = option.url,
            str = '?';

        for (var key in data) {
            str += key + '=' + data[key] + "&"
        }

        str = str.slice(0, str.length - 1);
        url += str;

        xhr.onreadystatechange = function() {
            if (xhr.readyState === 4 && xhr.status === 200) {
                var json = JSON.parse(xhr.responseText)
                option.success(json)
            } else {
                option.err("获取数据失败")
            }
        }
        
        xhr.open(type, url, asy);
        xhr.send();

    }
    ajax({
        type: 'GET',
        data: {
            "pageNum": 1,
            "pageSize": 10
        },
        url: "http://132.232.94.151:3000/api/film/getList",
        success: function(data) {
            for (var i = 0; i < data.films.length; i++) {
                var li = '<li>' + data.films[i].name + '</li>'
                list.insertAdjacentHTML('beforeend', li);
            }
        }

    })
复制代码
关注下面的标签,发现更多相似文章
评论