Skip to content

Instantly share code, notes, and snippets.

@alanerzhao
Last active December 20, 2015 11:09
Show Gist options
  • Save alanerzhao/6120894 to your computer and use it in GitHub Desktop.
Save alanerzhao/6120894 to your computer and use it in GitHub Desktop.
/*
* @return rgb(xxx,xxx,xxx)
*
*/
function getColor () {
var rgb = [];
for(var i = 0; i < 3; i++) {
//返回最接近的数
rgb[i] = Math.round(255 * Math.random());
};
//rgb(255,255,255)
return 'rgb('+rgb.join(',') + ')';
};
//遍历dom
function walkDOM (n) {
do {
console.log(n);
if(n.hasChildNodes()) {
walkDOM(n.firstChild);
}
} while (n = n.nextSibling);
}
function removeAll(n) {
while (n.firstChild) {
n.removeChild(n.firstChild);
}
}
function bindEvent(obj, ev, fn) {
if (obj.addEventListener) {
obj.addEventListener(ev, fn, false);
} else if(obj.attachEvent) {
obj.attachEvent('on' + ev, function() {
fn.call(obj);
})
} else {
obj.onclick = fn;
}
}
function css(obj, attr) {
if (obj.currentStyle) {
return obj.currentStyle[attr];
} else {
return getComputedStyle(obj, false)[attr];
}
}
function setCookie(key, value, t) {
var oDate = new Date();
oDate.setDate(oDate.getDate() + t);
document.cookie = key + '= ' + encodeURI(value) + ';expires=' + oDate;
}
function getCookie(key) {
var arr = document.cookie.split('; ');
for (var i = 0; i < arr.length; i++) {
var arr2 = arr[i].split('=');
if (arr2[0] == key) {
return decodeURI(arr2[1]);
}
}
}
function delCookie(key) {
setCookie(key, '', - 1);
}
function dragDrop(obj) {//参数
obj.onmousedown = function (event) {
var oEvent = event || window.event;
var disX = oEvent.clientX - obj.offsetLeft;
var disY = oEvent.clientY - obj.offsetTop;
if(obj.setCapture) {//全局捕获
obj.setCapture();
}
obj.style.cursor = 'move';//移动指针
document.onmousemove = function (event) {
var oEvent = event || window.event;
var moveL = oEvent.clientX - disX;//距离
var moveR = oEvent.clientY - disY;
console.log(moveL);
if(moveL < 0) {
moveL = 0;
} else if( moveL > document.documentElement.clientWidth - obj.offsetWidth) {
moveL = document.documentElement.clientWidth - obj.offsetWidth;
}
if (moveR < 0) {
moveR = 0;
} else if( moveR > document.documentElement.clientHeight - obj.offsetHeight) {
moveR = document.documentElement.clientHeight - obj.offsetHeight;
}
obj.style.left = moveL + 'px';
obj.style.top = moveR + 'px';
}
document.onmouseup = function () {
document.onmousemove = document.onmouseup = null
if(obj.setCapture) {//抬起取消全局捕获
obj.releaseCapture();
}
}
return false;//解决空标签
}
}
function ajax(method, url, data, fnSuc, charset) {
if (!charset) {
charset = 'utf-8';
}
if (method == 'jsonp') {
var oScript = document.createElement('script');
if (data) {
url += '?' + data;
}
oScript.src = url;
oScript.charset = charset;
fnSuc && fnSuc();
document.body.appendChild(oScript);
return ;
}
var oAjax = null;
if (window.XMLHttpRequest) {
oAjax = new XMLHttpRequest();
} else {
oAjax = new ActiveXObject('Microsoft.XMLHTTP');
}
if (method == 'get') {
url += '?' + data;
}
oAjax.open(method, url, true);
if (method == 'get') {
oAjax.send();
} else {
oAjax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
oAjax.send(data);
}
oAjax.onreadystatechange = function() {
if (oAjax.readyState == 4) {
if (oAjax.status == 200) {
alert(oAjax.responseText);
fnSuc && fnSuc(oAjax.responseText);
}
}
}
}
// JavaScript Document
function startMove(obj,json,endFn){
clearInterval(obj.timer);
obj.timer = setInterval(function(){
var bBtn = true;
for(var attr in json){
var iCur = 0;
if(attr == 'opacity'){
if(Math.round(parseFloat(getStyle(obj,attr))*100)==0){
iCur = Math.round(parseFloat(getStyle(obj,attr))*100);
}
else{
iCur = Math.round(parseFloat(getStyle(obj,attr))*100) || 100;
}
}
else{
iCur = parseInt(getStyle(obj,attr)) || 0;
}
var iSpeed = (json[attr] - iCur)/8;
iSpeed = iSpeed >0 ? Math.ceil(iSpeed) : Math.floor(iSpeed);
if(iCur!=json[attr]){
bBtn = false;
}
if(attr == 'opacity'){
obj.style.filter = 'alpha(opacity=' +(iCur + iSpeed)+ ')';
obj.style.opacity = (iCur + iSpeed)/100;
}
else{
obj.style[attr] = iCur + iSpeed + 'px';
}
}
if(bBtn){
clearInterval(obj.timer);
if(endFn){
endFn.call(obj);
}
}
},30);
}
function getStyle(obj,attr){
if(obj.currentStyle){
return obj.currentStyle[attr];
}
else{
return getComputedStyle(obj,false)[attr];
}
}
function stopMove(obj){
clearInterval(obj.timer);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment