day03

/**
* Created by zhouyan on 15/4/26.
*/


//前台调用
var $=function(args){
return new Base(args);
}


//基础库
function Base(args){

//创建一个数组,来保存获取的节点和节点数组
this.elements=[];

if(typeof args =='string'){
//css模拟
if(args.indexOf(' ')!=-1){
var elements = args.split(' ');
var childElements = [];
var node = [];
for(var i=0;i<elements.length;i++){
if(node.length == 0) node.push(document);
switch(elements[i].charAt(0)){
case '#':
childElements =[];
childElements.push(this.getId(elements[i].substring(1)));
node = childElements;
break;
case '.':
childElements = [];
for(var j=0;j<node.length;j++){
var temps = this.getClass(elements[i].substring(1),node[j]);
for(var k=0;k<temps.length;k++){
childElements.push(temps[k]);
}
}
node = childElements;
break;
default:
childElements = [];
for(var j=0;j<node.length;j++){
var temps = this.getTagName(elements[i],node[j]);
for(var k=0;k<temps.length;k++){
childElements.push(temps[k]);
}
}
node = childElements;
}
}

}else {
//find模拟
switch (args.charAt(0)) {
case '#':
this.elements.push(this.getId(args.substring(1)));
break;
case '.':
this.elements = this.getClass(args.substring(1));
break;
default:
this.elements = this.getTagName(args.substring(1));
}
}
}else if(args == 'object'){
this.elements[0]=args;
}

}


//获取ID节点
Base.prototype.getId=function(id){
return document.getElementById(id);
};

//获取元素节点
Base.prototype.getTagName=function(tagName,parentNode){
var node = null;
var temps=[];
if(parentNode != undefined){
node = parentNode;
}else{
node = document;
}

var tags=node.getElementsByTagName(tagName);
for(var i=0;i<tags.length;i++){
temps.push(tags[i]);
}

return temps;
};

//获取Class节点
Base.prototype.getClass=function(className,parentNode){
var node = null;
var temps=[];
if(parentNode != undefined){
node = parentNode;
}else{
node = document;
}
var all=node.getElementsByTagName('*');
for(var i=0;i<all.length;i++) {
if (all[i].className == className) {
temps.push(all[i])
}
}
return temps;
}


//设置css
Base.prototype.css=function(attr,value){
for(var i=0;i<this.elements.length;i++) {
if(arguments.length == 1){
return getStyle(this.elements[i],attr);
}
this.elements[i].style[attr] = value;
}
return this;
}

//设置innerHTML
Base.prototype.html=function(str){
for(var i=0;i<this.elements.length;i++) {
if(arguments.length == 0){
return this.elements[i].innerHTML;
}
this.elements[i].innerHTML = str;
}
return this;
}

//触发点击事件
Base.prototype.click=function(fn){
for(var i=0;i<this.elements.length;i++) {
this.elements[i].onclick=fn;
}
return this;
}

//获取某一个节点,并且Base对象
Base.prototype.getElement =function(num){
return this.elements[num];
}

//获取某一个节点,并返回这个节点对象
Base.prototype.eq = function(num){
var element =this.elements[num];
this.elements = [];
this.elements[0] =element;
return this;
}

//添加Class
Base.prototype.addClass = function(className){
for(var i=0;i<this.elements.length;i++){
if(!hasClass(this.elements[i],className)) {
this.elements[i].className += ' '+className;
}
}
return this;
}

//移除Class
Base.prototype.removeClass = function(className){
for(var i=0;i<this.elements.length;i++){
if(hasClass(this.elements[i],className)) {
this.elements[i].className=this.elements[i].className.replace(new RegExp('(\\s|^)'+className+'(\\s|$)'),'');
}
}
return this;
}

//添加link或style的css规则
Base.prototype.addRule = function(num,selectorText,cssText,pos){
var sheet = document.styleSheets[num];
insertRule(sheet,electorText,cssText,pos);
return this;
}

//移除link或style的css规则
Base.prototype.removeRule = function(num,pos){
var sheet = document.styleSheets[num];
deleteRule(sheet,pos);
return this;
}



//设置鼠标移入移出方法
Base.prototype.hover = function(over,out){
for(var i=0;i<this.elements.length;i++){
addEvent(this.elements[i],'mouseover',over);
addEvent(this.elements[i],'mouseout',out);
}
return this;
}

//设置显示
Base.prototype.show =function(){
for(var i=0;i<this.elements.length;i++){
this.elements[i].style.display="block";
}
return this;
}

//设置隐藏
Base.prototype.hide =function(){
for(var i=0;i<this.elements.length;i++){
this.elements[i].style.display="none";
}
return this;
}

//设置物体居中
Base.prototype.center = function(width,height){
var top = (getInner().height - height)/2;
var left = (getInner().width - width)/2;
for(var i=0;i<this.elements.length;i++){
this.elements[i].style.top=top+'px';
this.elements[i].style.left=left+'px';
}
return this;
}

//触发浏览器窗口事件
Base.prototype.resize = function(fn){
for(var i=0;i<this.elements.length;i++){
var element=this.elements[i];
addEvent(window,'resize',function(){
fn();
if(element.offsetLeft > getInner().width - element.offsetWidth){
element.style.left=getInner().width - element.offsetWidth+'px';
}
if(element.offsetTop > getInner().height - element.offsetHeight){
element.style.top=getInner().height - element.offsetHeight+'px';
}
})

}
return this;
}

//锁屏功能
Base.prototype.lock = function(){
for(var i=0;i<this.elements.length;i++){
this.elements[i].style.display='block';
this.elements[i].style.width=getInner().width+'px';
this.elements[i].style.height=getInner().height+'px';
document.documentElement.style.overflow='hidden';
}

addEvent(window,'scroll',scrollTop);
return this;
}

//解屏功能
Base.prototype.unlock = function(){
for(var i=0;i<this.elements.length;i++){
this.elements[i].style.display='none';
document.documentElement.style.overflow='auto';
}
removeEvent(window,'scroll',scrollTop);
return this;
}


//插件入口
Base.prototype.extend = function(name,fn){
Base.prototype[name]=fn;
}

//设置CSS选择器子节点
Base.prototype.find = function(str){
var childElements=[];
for(var i=0;i<this.elements.length;i++){
switch(str.charAt(0)){
case '#':
childElements.push(this.getId(str.substring(1)));
break;
case '.':
var temps = this.getClass(str.substring(1),this.elements[i]);
for(var j=0;j<temps.length;j++){
childElements.push(temps[j]);
}
break;
default:
var temps = this.getTagName(str,this.elements[i]);
for(var j=0;j<temps.length;j++){
childElements.push(temps[j]);
}
}
}

this.elements=childElements;
return this;
}




//滚动条清零
function scrollTop(){
document.documentElement.scrollTop = 0;
document.body.scrollTop = 0;
}





/**
* Created by zhouyan on 15/4/27.
*/


//跨浏览器获取视口大小
function getInner(){
if(typeof window.innerWidth != 'undefined'){
return {
width: window.innerWidth,
height: window.innerHeight
}
}else{
return {
width: document.documentElement.clientWidth,
height: document.documentElement.clientHeight
}
}
}

//跨浏览器获取Style
function getStyle(element,attr){
if(typeof window.getComputedStyle !="undefined"){//W3C
return window.getComputedStyle(element,null)[attr];
}else if(typeof element.currentStyle != 'undefined'){//IE
return element.currentStyle[attr];
}
}

//判断Class是否存在
function hasClass(element,className){
element.className.match(new RegExp('(\\s|^)'+className+'(\\s|$)'));
}

//跨浏览器添加link规则
function insertRule(sheet,selectorText,cssText,pos){
if(typeof sheet.insertRule != 'undefined'){ //w3c
sheet.insertRule(selectorText+'{'+cssText+'}',pos);
}else if(typeof sheet.addRule != 'undefined'){ //Ie
sheet.addRule(selectorText,cssText,pos);
}
}

//跨浏览器删除link规则
function deleteRule(sheet,pos){
if(typeof sheet.deleteRule != 'undefined'){ //w3c
sheet.deleteRule(pos);
}else if(typeof sheet.removeRule != 'undefined'){ //Ie
sheet.removeRule(pos);
}
}

//获取Event对象
function getEvent(event){
return event|| window.event;
}

//阻止默认行为
function preDef(event){
var e=getEvent(event);
if(typeof e.preventDefault != 'undefined'){
e.preventDefault();
}else{
e.returnValue = false;
}
}

//为每个事件分配以一个计数器
addEvent.ID=1;
//跨浏览器事件绑定
function addEvent(obj,type,fn){
if(typeof obj.addEventListener != 'undefined'){
obj.addEventListener(type,fn,false);
}else{
if(!obj.events) obj.events = {};
if(!obj.events[type]){
obj.events[type]=[];
obj.events[type][0]=fn;
}else{
for(var i in obj.events[type]){
if(obj.events[type][i] != fn){
obj.events[type][addEvent.ID++] = fn;
}
}
}

obj['on'+type]=addEvent.exec;
}
}

//执行事件处理函数
addEvent.exec = function(event){
var e=event || addEvent.fixEvent(window.event);
var es=this.events[e.type];
for(var i in es){
es[i].call(this,e);
}
}

//把IE常用的Event对象配对到W3C中去
addEvent.fixEvent =function(event){
event.preventDefault = addEvent.fixEvent.preventDefault;
event.stopPropagation = addEvent.fixEvent.stopPropagation;
event.targrt=event.srcElement;
return event;
}

//IE 阻止默认行为
addEvent.fixEvent.preventDefault =function(){
this.returnValue=false;
}

//IE 取消冒泡
addEvent.fixEvent.stopPropagation =function(){
this.cancelBubble =true;
}


//跨浏览器删除事件
function removeEvent(obj,type,fn){
if(typeof obj.removeEventListener != 'undefined'){
obj.removeEventListener(type,fn,false);
}else{
if(obj.events[type]) {
for (var i in obj.events[type]) {
if (obj.events[type][i] == fn) {
delete obj.events[type][i];
}
}
}
}
}

//删除空格
function trim(str){
return str.replace(/(^\s*)|(\s*$)/g);
}





posted @ 2015-04-28 21:52  zhouyan_jsj  Views(138)  Comments(0Edit  收藏  举报