Skip to content

Instantly share code, notes, and snippets.

@wong2 wong2/wong2.js
Created Jan 17, 2012

Embed
What would you like to do?
备份。。
/**
* Copyright 2011 wong2 <wonderfuly@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
(function(){
var wong2 = {};
wong2.$ = function(id){
return document.getElementById(id);
}
// dom模块
wong2.dom = {
// 一个通过class获取元素的函数
getElementsByClassName: document.getElementsByClassName || function(classname, node){
node = node || document.body;
var i, j;
var result = [];
var re = new RegExp('\\b' + classname + '\\b');
var child_nodes = node.getElementsByTagName("*");
for(i=0,j=child_nodes.length; i<j; i++){
if(re.test(child_nodes[i].className)){
result.push(child_nodes[i]);
}
}
return result;
},
// 获取页面元素相对于整个文档左上角的位置
getPosition: function(id_or_node){
var node = typeof id_or_node=="string"?document.getElementById(id_or_node):id_or_node;
var x = node.offsetLeft;
var y = node.offsetTop;
while (node = node.offsetParent) {
x += node.offsetLeft;
y += node.offsetTop;
}
return {'x': x, 'y': y};
},
// 为element添加新的class
addClass: function(element, name){
element.className += " " + name;
},
// 为element移除一个class
removeClass: function(element, classname){
element.className = wong2.string.trim(
element.className.replace(classname, "")
);
}
};
wong2.event = {
// 添加事件处理函数
addHandler: function(element, type, handler){
if (element.addEventListener){
element.addEventListener(type, handler, false);
} else if (element.attachEvent){
element.attachEvent("on" + type, handler);
} else {
element["on" + type] = handler;
}
},
// 移除事件处理函数
removeHandler: function(element, type, handler){
if (element.removeEventListener){
element.removeEventListener(type, handler, false);
} else if (element.detachEvent) {
element.detachEvent("on" + type, handler);
} else {
element["on" + type] = null;
}
},
// 获取event对象
getEvent: function(event){
return event || window.event;
},
getTarget: function(event){
return event.target || event.srcElement;
},
// 取消默认行为
preventDefault: function(event){
if(event.preventDefault){
event.preventDefault();
} else {
event.returnValue = false;
}
},
stopPropagation: function(event){
if(event.stopPropagation){
event.stopPropagation();
} else {
event.cancelBubble = true;
}
},
// 获取鼠标按键
getButton: function(event){
if(document.implementation.hasFeature("MouseEvents", "2.0")){
return event.button;
} else {
switch(event.button){
case 0:
case 1:
case 3:
case 5:
case 7:
return 0;
case 2:
case 6:
return 1;
case 4:
return 2;
}
}
},
// 获取键盘按键的码
getCharCode: function(event){
return event.charCode || event.keyCode;
}
};
wong2.bom = {
// copied from lifesinger,判断是否IE
isIE876 : !+'\v1',
isIE76 : !'0'[0],
isIE6 : this.isIE76 && !window.XMLHttpRequest
};
wong2.cookie = {
setCookie: function(name, value){
document.cookie = name + "=" + encodeURIComponent(value);
},
getCookie: function(name){
var allcookies = document.cookie;
var pos = allcookies.indexOf(name+"=");
if(pos != -1){
var start = pos + name.length + 1;
var end = allcookies.indexOf(";", start);
if (end == -1) {
end = allcookies.length;
}
var value = allcookies.substring(start, end);
return decodeURIComponent(value);
}
return -1;
},
deleteAllCookies: function(){
var cookies = document.cookie.split(";");
for (var i = 0; i < cookies.length; i++) {
var cookie = cookies[i];
var eqPos = cookie.indexOf("=");
var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
}
}
};
wong2.string = {
// 移除字符串首尾空白
trim: function(s){
return s.replace(/^\s+|\s+$/g, "");
}
};
wong2.ajax = {
ajax: function(method, url, data, callback){
var xhr = typeof XMLHttpRequest !== "undefined"?
(new XMLHttpRequest())
:(new ActiveXObject("Msxml2.XMLHTTP"));
xhr.onreadystatechange = function(){
if(xhr.readyState == 4){
if(xhr.status>=200 && xhr.status<300 || xhr.status==304){
callback.apply(null, xhr);
}
}
}
if(method == "get"){
xhr.open("get", url, true);
xhr.send(null);
} else if (method == "post") {
xhr.open("post", url, true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send(data);
}
},
get: function(url, callback){
wong2.ajax.ajax("get", url, null, callback);
},
post: function(url, data, callback){
wong2.ajax.ajax("post", url, data, callback);
}
};
wong2.style = {
get: function(obj, attr){
return obj.style[attr];
},
set: function(obj, attr, value){
obj.style[attr] = value;
},
// 获取元素的opacity属性
getOpacity :function(obj){
var opacity_pattern = /opacity=(\d+)/;
var opacity = 1;
if(!wong2.bom.isIE876){
var tmp = obj.style.opacity;
if(tmp){
opacity = tmp;
}
}
else{
var tmp = obj.style.filter.match(opacity_pattern);
if(tmp){
opacity = tmp[1] / 100;
}
}
opacity = parseFloat(opacity);
var fixed = opacity.toFixed(2);
return parseFloat(fixed);
},
// 设置元素的opacity属性
setOpacity: function(obj, opacity){
var style_obj = obj.style;
var opacity_pattern = /opacity=(\d+)/;
if(!wong2.bom.isIE876){
style_obj.opacity = "" + opacity;
}
else{
var filter = style_obj.filter;
var opacity = Math.floor(opacity*100);
if(opacity_pattern.test(filter)){
style_obj.filter = filter.replace(opacity_pattern, "opacity=" + opacity);
}
else{
style_obj.filter = "alpha(opacity=" + opacity + ")";
}
}
}
};
wong2.effect = {
animate: function(obj, option){
var step = 2;
var attr = option.attr;
var value = option.value;
var suffix = option.suffix || "";
var interval = option.interval || 1;
var func = option.func || function(t){return t;};
if(obj.timeout){
clearInterval(obj.timeout)
}
obj.timeout = null;
function perform(determin, calc){
clearInterval(obj.timeout);
obj.timeout = setInterval(function(){
step = func(step);
var current_value = parseFloat(wong2.style.get(obj, attr) || "0");
if(determin(current_value, value)){
clearInterval(obj.timeout);
}
wong2.style.set(obj, attr, calc(current_value, step) + suffix);
}, interval);
}
var old_value = parseFloat(wong2.style.get(obj, attr));
if(Math.abs(value-old_value) <= 5){
return;
}
else if(value > old_value){
perform(function(a, b){
return a >= b;
}, function(a, b){
return a + b;
});
}
else{
perform(function(a, b){
return a <= b;
}, function(a, b){
return a - b;
});
}
}
};
window.wong2 = wong2;
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.