javascript常用函数封装——运动、cookie、ajax、获取行内样式兼容写法、拖拽
运动、cookie、ajax、获取行内样式兼容写法、拖拽封装大合集。
//url,data,type,timeout,success,error
function ajax(options){
//-1 整理options
options=options||{};
options.data=options.data||{};
options.timeout=options.timeout||0;
options.type=options.type||'get'; //0 整理data
var arr=[];
for(var key in options.data){
arr.push(key+'='+encodeURIComponent(options.data[key]));
}
var str=arr.join('&'); //1 创建ajax对象
if(window.XMLHttpRequest){
var oAjax=new XMLHttpRequest();//[object XMLHttpRequest]
}else{
var oAjax=new ActiveXObject('Microsoft.XMLHTTP')
} if(options.type=='get'){
//2.
oAjax.open('get',options.url+'?'+str,true);
//3.
oAjax.send();
}else{
//2.
oAjax.open('post',options.url,true);
//设置请求头
oAjax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
oAjax.send(str);//身子
} //3.5 超时
if(options.timeout){
var timer=setTimeout(function(){
alert('超时了');
oAjax.abort();//中断ajax请求
},options.timeout);
} //4.
oAjax.onreadystatechange=function(){//当准备状态改变时
if(oAjax.readyState==4){//成功失败都会有4
clearTimeout(timer);
if(oAjax.status>=200 && oAjax.status<300 || oAjax.status==304){
options.success && options.success(oAjax.responseText);
}else{
options.error && options.error(oAjax.status);//http 0
}
}
}; };
function ajax(url,fnWin,fnFaild){
//1.创建ajax对象
var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
//2.与服务器建立连接
xhr.open("GET",url,true);
//3.发送请求
xhr.send();
//4.接收服务器返回的信息
xhr.onreadystatechange = function(){
if(xhr.readyState == 4){
if(xhr.status == 200){
fnWin && fnWin(xhr.responseText);
}
}else{
fnFaild && fnFaild();
}
}
}
//创建cookie
function createCookie(key,value,expires,domain,secure){
var cookieText = encodeURIComponent(key) + "=" + encodeURIComponent(value) + ";path=/";
if(!isNaN(expires)){
if(typeof expires == "number" && expires > 0){
var date = new Date();
date.setDate(date.getDate() + expires);
cookieText += ";expires=" + date;
}
}
if(domain){
cookieText += ";domain=" + domain;
}
if(secure){
cookieText += ";secure";
}
document.cookie = cookieText;
} //获取cookie
function getCookie(key){
var keyText = encodeURIComponent(key) + "="
var start = document.cookie.indexOf(keyText); //找到开始位置
if(start != -1){
var end = document.cookie.indexOf(";",start); //找到结束位置
if(end == -1){
end = document.cookie.length;
}
var cookieValue = decodeURIComponent(document.cookie.substring(start + keyText.length,end));
}
return cookieValue;
} //删除cookie
function removeCookie(key){
document.cookie = key + "=;expires=" + new Date(0) + ";path=/";
}
function drag(obj){
var arr = [];
obj.onmousedown = function(event){
arr = []; //清空数组
var e = event || window.event;
var disX = e.offsetX;
var disY = e.offsetY;
document.onmousemove = function(evt){
var e = evt || window.event;
var L = e.clientX - disX;
var T = e.clientY - disY;
if(L < 0){
L = 0; //左边界
}else if(L > document.documentElement.clientWidth - obj.offsetWidth){ //右边界
L = document.documentElement.clientWidth - obj.offsetWidth;
}
if(T < 0){
T = 0; //上边界
}else if(T > document.documentElement.clientHeight - obj.offsetHeight){ //下边界
T = document.documentElement.clientHeight - obj.offsetHeight;
}
obj.style.left = L + "px";
obj.style.top = T + "px";
arr.push({"left":obj.offsetLeft,"top":obj.offsetTop});
}
document.onmouseup = function(){
document.onmousemove = null;
}
}
}
function drag(obj){
obj.onmousedown = function(evt){
var e = evt || window.event;
//获取鼠标当前的相对坐标值
var disX = e.offsetX;
var disY = e.offsetY;
document.onmousemove = function(evt){
var e = evt || window.event;
obj.style.left = e.clientX - disX + "px";
obj.style.top = e.clientY - disY + "px";
}
document.onmouseup = function(){
document.onmousemove = null;
}
}
//去掉拖拽的默认行为(禁拖行为);
document.ondragstart = function(){
return false;
}
}
/*
* 一、获取非行内样式
* 1. 兼容
* IE 对象.currentStyle.属性
* 标准 getComputedStyle(对象,1).属性
* 二、运动框架
* 1.计时器
* 1》设置开关:作用何时退出计时器
* 2》遍历json
* (1)获取对象属性的当前值
* 注:兼容
* 透明度(处理小数)
* 其它属性值(处理单位)
* (2)计算速度
* 匀速运动(处理停止条件)
* 缓冲运动(处理小数)
* (3)判断json中所有的属性是否达到目标值
* (4)设置运动(改变CSS值)
* 3》检测停止(当开关为true时,停止计时器)
*/
//获取非行内样式的兼容
function getStyle(obj,attr){
return obj.currentStyle ? obj.currentStyle[attr] : getComputedStyle(obj,true)[attr];
}
function sport(obj,json,fn){
clearInterval(obj.timer);
obj.timer = setInterval(function(){
var stop = true;
for(var attr in json){
//获取当前值
var current = 0;
if(attr == "opacity"){
current = parseInt(parseFloat(getStyle(obj,attr)) * 100);
}else{
current = parseInt(getStyle(obj,attr));
}
//计算速度
//缓冲运动
var speed = (json[attr] - current) / 8;
speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
if(current != json[attr]){
stop = false;
}
if(attr == "opacity"){
obj.style.opacity = (current + speed) / 100;
obj.style.filter = "alpha(opacity=" + (current + speed) + ")";
}else{
obj.style[attr] = current + speed + "px";
}
}
if(stop){
clearInterval(obj.timer);
fn && fn();
}
},30);
}
javascript常用函数封装——运动、cookie、ajax、获取行内样式兼容写法、拖拽的更多相关文章
- 【封装函数】原生js 获取行内外联样式-兼容IE
var dom=document.getElementsByTagName("div")[0]; console.log(getStyle(dom,"padding-to ...
- JavaScript的DOM_操作行内样式
一.检测浏览器是否支持css CSS 作为(X)HTML 的辅助,可以增强页面的显示效果.但不是每个浏览器都能支持最新的 CSS 能力.CSS 的能力和 DOM 级别密切相关,所以我们有必要检测当前浏 ...
- php文件操作(最后进行文件常用函数封装)
文件信息相关API $filename="./1-file.php"; //filetype($filename):获取文件的类型,返回的是文件的类型 echo '文件类型为:', ...
- JavaScript常用函数
JavaScript常用函数 常规函数 数组函数 日期函数 数学函数 字符串函数 常规函数 (1)alert函数:显示一个警告对话框,包括一个OK按钮.(alert("输入错误") ...
- 【javascript】javascript常用函数大全
javascript函数一共可分为五类: •常规函数 •数组函数 •日期函数 •数学函数 •字符串函数 1.常规函数 javascript常规函数包括以下9个函数: ( ...
- js获取非行内样式
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- window的三大功能,行内样式获取的讲解 getcomputerStyle
window的三大功能: js引擎 dom渲染 获取行内中css的属性: chrome和Firefox加上 ie9+(所谓的高级浏览器)用的方法: window.getComeputerStyle(d ...
- 【前端】Util.js-ES6实现的常用100多个javaScript简短函数封装合集(持续更新中)
Util.js (持续更新中...) 项目地址: https://github.com/dragonir/Util.js 项目描述 Util.js 是对常用函数的封装,方便在实际项目中使用,主要内容包 ...
- Javascript:常用函数封装
//cookie function setCookie(name, value, iDay) { if(iDay!==false) { var oDate=new Date(); oDate.setD ...
随机推荐
- 第十四节、FAST角点检测(附源码)
在前面我们已经陆续介绍了许多特征检测算子,我们可以根据图像局部的自相关函数求得Harris角点,后面又提到了两种十分优秀的特征点以及他们的描述方法SIFT特征和SURF特征.SURF特征是为了提高运算 ...
- Lucas定理学习笔记(没有ex_lucas)
题目链接\(Click\) \(Here\) \(ex\_lucas\)实在是不能学的东西...简单学了一下\(Lucas\)然后打算就这样鸽着了\(QwQ\)(奶一口不可能考) 没什么复杂的,证明的 ...
- (链表 双指针) leetcode 19. Remove Nth Node From End of List
Given a linked list, remove the n-th node from the end of list and return its head. Example: Given l ...
- go的net/rpc用法
一:PRC是什么? RPC(Remote Procedure Call) 远程过程调用,是一个计算通信协议.该协议允许一台计算机上的程序调用另外一台计算机上的程序.远程过程调用就是2个不在同一台计算机 ...
- maven-compiler-plugin报错
[INFO] Scanning for projects... [INFO] [INFO] --------------------------< cn.x:credittest >--- ...
- spring中IOC和AOP原理
IoC(Inversion of Control): (1)IoC(Inversion of Control)是指容器控制程序对象之间的关系,而不是传统实现中,由程序代码直接操控.控制权由应用代码中转 ...
- maven依赖包下载地址
http://maven.org http://mvnrepository.com/
- 2017-12-18python全栈9期第三天第一节之昨天内容回顾与作业讲解用户三次机会再试试
#!/user/bin/python# -*- coding:utf-8 -*-username = "zd"password = "123"i = 3whil ...
- 最接近原点的K个点
一.题目描述 我们有一个由平面上的点组成的列表 points.需要从中找出 K 个距离原点 (0, 0) 最近的点 这里,平面上两点之间的距离是欧几里德距离 你可以按任何顺序返回答案.除了点坐标的顺序 ...
- java io系列09之 FileDescriptor总结
本章对FileDescriptor进行介绍 转载请注明出处:http://www.cnblogs.com/skywang12345/p/io_09.html FileDescriptor 介绍 Fil ...