JS常用片段
Cookie
$.cookie('username');
$.cookie('username', 'Refueling CodeFish');
$.cookie('username', 'Refueling CodeFish', { expires: 3 });
$.cookie('username', '', { expires: -1 });
$.cookie('username', null);
/**
* cookie
* @param name cookie名称
* @param value 保存的内容,如果要保存对象或数组可以先转义成字符串
* @param options 保存时间 例:{expires:6}保存6天,{-1为删除cookie}
* @returns {*}
*/
$.cookie=function(name,value,options){
if(typeof value!='undefined'){
options=options||{};
if(value===null){
value='';
options.expires=-1;
}
var expires='';
if(options.expires&&(typeof options.expires=='number'||options.expires.toUTCString)){
var date;
if(typeof options.expires=='number'){
date=new Date();
date.setTime(date.getTime()+(options.expires * 24 * 60 * 60 * 1000));
}else{
date=options.expires;
}
expires=';expires='+date.toUTCString();
}
var path=options.path?';path='+options.path:'';
var domain=options.domain?';domain='+options.domain:'';
var secure=options.secure?';secure':'';
document.cookie=[name,'=',encodeURIComponent(value),expires,path,domain,secure].join('');
}else{
var cookieValue=null;
if(document.cookie&&document.cookie!=''){
var cookies=document.cookie.split(';');
for(var i=0;i<cookies.length;i++){
var cookie=jQuery.trim(cookies[i]);
if(cookie.substring(0,name.length+1)==(name+'=')){
cookieValue=decodeURIComponent(cookie.substring(name.length+1));
break;
}
}
}
return cookieValue;
}
};
游戏开发常用
角度计算
function getAngle(px,py,mx,my){
var x = Math.abs(px-mx);
var y = Math.abs(py-my);
var z = Math.sqrt(Math.pow(x,2)+Math.pow(y,2));
var cos = y/z;
var radina = Math.acos(cos);
var angle = Math.floor(180/(Math.PI/radina));
if(mx>px&&my>py){
angle = 180 - angle;
}
if(mx==px&&my>py){
angle = 180;
}
if(mx>px&&my==py){
angle = 90;
}
if(mx<px&&my>py){
angle = 180+angle;
}
if(mx<px&&my==py){
angle = 270;
}
if(mx<px&&my<py){
angle = 360 - angle;
}
return angle;
}