20131213原生js代码

window.onload=function () {
	fnImg();
	window.onscroll=function() {
		fnImg();
	}
	picTab();
	menu();
	searchValue();
	picScrollList();
	fnOver($('topRNavSpan'),$('topRBox'),'topRNav');
	fnOver($('topRNavSpan2'),$('topRBox2'),'topRService');
	fnOver($('shopA'),$('shopMyBox1'),'shopMy');
	fnOver($('shopA2'),$('shopMyBox2'),'shopCart');

	var oTxt=getByClass('searchTxt')[0];
	var oSul=getByClass('searchList')[0];
	oTxt.onkeyup=function() {
		if(this.value=='') {
			oSul.style.display='none';	
		}
		if(this.value!='') {
			var oScript=document.createElement('script');
			oScript.src='http://dd.search.jd.com/?key='+this.value+'&uid=&callback=jd';
			oScript.type='text/javascript';
			document.body.appendChild(oScript);	
		}
	};

};

//获取id
function $(id) {
	return document.getElementById(id);	
}

//幻灯片
function picTab() {
	var	oUl= document.getElementById('picTab')	
	var aLi=oUl.getElementsByTagName('li');
	var oL=document.getElementById('picBtn');
	var aBtn=oL.getElementsByTagName('li');
	var iNum=0;
	var timer=null;
	for(var i=0;i<aBtn.length;i++) {
		aBtn[i].index=i;
		aBtn[i].onmouseover=function() {
			iNum=this.index;
			if(this.className=='active') return;
			fnTab();
			alphaMove();
		}
	}
	function fnTab() {
		for(var i=0;i<aBtn.length;i++) {
				aBtn[i].className='';
				aLi[i].className='';;	
			}
			aBtn[iNum].className='active';
			aLi[iNum].className='cur';	
	}
	timer=setInterval(function() {
		iNum++;
		iNum%=aBtn.length;
		fnTab();
		alphaMove();
	},2000);
	function alphaMove() {
		oUl.style.filter='alpha(opacity=0)';
		oUl.style.opacity=0;
		startMove(oUl,{
				opacity	:	100
		});
	};
	oUl.onmouseover=oUl.parentNode.onmouseover=function() {
		clearInterval(timer);	
	};
	oUl.parentNode.onmouseout=function() {
		timer=setInterval(function() {
		iNum++;
		iNum%=aBtn.length;
		fnTab();
		alphaMove();
		},2000);	
	};
};
//主菜单
function menu() {

	var oUl=document.getElementById('menu');
	var aLi=oUl.children;
	var aClose=getByClass('close');
	for(var i=0;i<aLi.length;i++) {
		aLi[i].index=i;
		aLi[i].onmouseover=function() {
			this.className='curr';
			for(var j=0;j<aClose.length;j++) {
				aClose[j].onclick=function(){
					this.parentNode.parentNode.parentNode.className='';	
				}
			}
		}
		aLi[i].onmouseout=function() {
			this.className='';	
		}
	}

}
//获取class
function getByClass(sClass,parent){

	var aEles = (parent||document).getElementsByTagName('*');
	var arr = [];

	for(var i=0; i<aEles.length; i++){

		var aClass = aEles[i].className.split(' ');

		for(var j=0; j<aClass.length; j++){

			if(aClass[j] == sClass){

				arr.push(aEles[i]);
				break;

			}

		}

	}

	return arr;
}
//幻灯片ul运动
function picScrollList() {
	var oParent=document.getElementById('picScroll');
	var oUl=document.getElementById('picScrollList');
	var aLi=oUl.getElementsByTagName('li');
	var oBtnL=getByClass('picScrollBtnL',oParent)[0];
	var oBtnR=getByClass('picScrollBtnR',oParent)[0];
	var iNum=0;
	var iWidth=aLi[0].offsetWidth*3;
	var iBtn=true;
		aLi[0].style.position='relative';	//初始第0个li定位属性
		aLi[1].style.position='relative';
		aLi[2].style.position='relative';
		oUl.style.width=aLi[0].offsetWidth*aLi.length+'px';

		oBtnL.onclick=function() {
			if(!iBtn) return;
			iBtn=false;
			iNum++;
			startMove(oUl,{
					left	:	-iNum*iWidth
			},function() {
				if(iNum==aLi.length/3-1) {
					aLi[0].style.left=oUl.offsetWidth+'px';
					aLi[1].style.left=oUl.offsetWidth+'px';
					aLi[2].style.left=oUl.offsetWidth+'px';
				}
				if(iNum==aLi.length/3) {
					aLi[0].style.left=0;
					aLi[1].style.left=0;
					aLi[2].style.left=0;
					oUl.style.left=0;
					iNum=0;	
				}
				iBtn=true;
			})
		};
		oBtnR.onclick=function() {
			if(!iBtn) return;
			iBtn=false;
			iNum--;
			if(iNum<0) {
				aLi[0].style.left=oUl.offsetWidth+'px';
				aLi[1].style.left=oUl.offsetWidth+'px';
				aLi[2].style.left=oUl.offsetWidth+'px';
				oUl.style.left=-oUl.offsetWidth+'px';
				iNum=aLi.length/3-1;
			}

			startMove(oUl,{
					left:-iNum*iWidth
				},function() {
					if(iNum<2) {
					aLi[0].style.left=0;
					aLi[1].style.left=0;
					aLi[2].style.left=0;
					}
					iBtn=true;
				})
		}
};

//经过显示,移开隐藏
function fnOver(n,m,oldClass) {
	n.onmouseover=m.onmouseover=function() {
		if(n.parentNode.className==oldClass) {
			n.parentNode.className=oldClass+' '+'curr';
		}
		m.style.display='block';
	}
	n.onmouseout=m.onmouseout=function() {
		if(n.parentNode.className==oldClass+' '+'curr') {
			n.parentNode.className=oldClass;
		}
		m.style.display='none';
	}
}

//图片延时加载
function fnImg() {

	var aImg=document.getElementsByTagName('img');
	var scrollY=document.documentElement.scrollTop||document.body.scrollTop;
	var iH=document.documentElement.clientHeight+scrollY;
	for( var i=0;i<aImg.length;i++) {
		if(getPos(aImg[i])<iH && !aImg[i].s) {
			aImg[i].src=aImg[i].getAttribute('_src');
			aImg[i].style.opacity=0;
			aImg[i].style.filter=0;
			startMove(aImg[i],{
				opacity:100	
			});
			aImg[i].s=true;	
		}
	}
}

function getPos(obj) {
	var iTop=0;
	while(obj) {
		iTop+=obj.offsetTop;
		obj=obj.offsetParent;	
	}
	return iTop;
}

function getByClass(sClass,parent){

	var aEles = (parent||document).getElementsByTagName('*');
	var re = new RegExp('\\b' + sClass + '\\b');
	var arr = [];

	for(var i=0; i<aEles.length; i++){

		if(re.test(aEles[i].className)){
			arr.push(aEles[i]);
		}
	}

	return arr;
}

// 添加search
function jd(date) {
	var result=date;
	var oTxt=getByClass('searchTxt')[0];
	var oSul=getByClass('searchList')[0];
	var oBtn=getByClass('searchBtn')[0];
	var aLi=oSul.getElementsByTagName('li');

	if(result.length) {
		oSul.style.display='block';
		oSul.innerHTML='';
		for(var i=0;i<result.length;i++) {
			var oLi=document.createElement('li');
			oLi.innerHTML='<strong>'+result[i].keyword+'</strong><span>约'+(result[i].oamount||result[i].amount)+'个商品</span>';
			oSul.appendChild(oLi);
			oLi.url='http://search.jd.com/Search?keyword='+result[i].keyword+'&enc=utf-8&suggest=2'

		}
		document.onclick=function() {
			oSul.style.display='none';	
		}
		for(var i=0;i<aLi.length;i++) {
			aLi[i].onmouseover=function() {
				this.style.background='#ffdfc6';	
			}
			aLi[i].onmouseout=function() {
				this.style.background='';	
			}
			aLi[i].onclick=function() {
				window.open(this.url,'_blank');	
			}
		}
		oBtn.onclick=function() {
			oBtn.url='http://search.jd.com/Search?keyword='+oTxt.value+'&enc=utf-8&suggest=2'
			window.open(this.url,'_blanck');	
		}

	}
}

function searchValue() {
	var onOff=true;
	var oTxt=getByClass('searchTxt')[0];
	oTxt.onfocus=function() {
			if(onOff) {
				this.value=''; 
				this.style.color='#aaa'; 
				onOff=false; 
			}	
		}
		oTxt.onblur=function() {
			if(this.value=='') {
				this.value='前端博客 lvhui.org'
				onOff=true;	
			}
		}	
}

来自:http://lvhui.org/web/

原创文章:20131213原生js代码 ,未经许可,禁止转载,©版权所有
原文出处:前端开发博客 (http://caibaojian.com/20131213%e5%8e%9f%e7%94%9fjs%e4%bb%a3%e7%a0%81.html)

发表评论

请登录后发表评论: