论坛首页 Web前端技术论坛

一个的轮播插件。

浏览 3234 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (1) :: 隐藏帖 (0)
作者 正文
   发表时间:2012-05-06   最后修改:2012-05-07
先上图:

..省略css

<script type="text/javascript">

function $(id,tag){
	var re=(id&&typeof id!="string")?id:document.getElementById(id);
	if(!tag){
		return re;
	}
	else{
		return re.getElementsByTagName(tag);
	}
}

function move(moveWidth,pageItems,displayEl){
	var currentMoving=false;
	var carouselInterval;
	var changePageTimeout;
	var currentShowIndex=0;
	var currentHideIndex=1;
	var pShowEl=$(pageItems[currentShowIndex].itemId).parentNode;
	var initPageItemEvent=function(){
		var len=	pageItems.length;
		for(var i=0;i<len;i++){
			$(pageItems[i].btn).setAttribute("pageIndex",i);
			//按钮事件添加 可以做成配置项 添加自定义点击事件 此处没有处理
			$(pageItems[i].btn).onclick=	function(){
				var  cIndex=this.getAttribute("pageIndex")-0;
				if(currentMoving || currentShowIndex==cIndex){
					return false;
				}
				
				clearTimeout(changePageTimeout);
				currentHideIndex=cIndex;
				if(currentShowIndex>cIndex){
					changePaging(true);
				}else{
					changePaging(false);
				}
				
			}
		}
	};
	

	var changePosition=function(toRight){
		var currentItem=$(pageItems[currentShowIndex].itemId);
		var hidePage=$(pageItems[currentHideIndex].itemId);
		var hideLeft=hidePage.style.left;
		hideLeft=parseInt(hideLeft+0,10);
		var cWidth=Math.ceil(moveWidth/20);
	
		if(toRight){
			var hideMwidth=hideLeft+cWidth;
			var currMwidth=hideLeft+moveWidth;
		}else{
			var hideMwidth=hideLeft-cWidth;
			var currMwidth=hideLeft-moveWidth;
		}
		
		if((hideMwidth<=0 && !toRight)  ||  (toRight  && hideMwidth>=0)){
			hideMwidth=0;
			clearInterval(carouselInterval);
			setTimeout(function(){
				var currentItem=$(pageItems[currentShowIndex].itemId);
				currentItem.style.display="none";
				currentItem.style.left=moveWidth+"px";
				displayEl.appendChild(currentItem); 
				currentShowIndex=currentHideIndex;
				currentMoving=false;
				
			},0);
			changePageTimeout=setTimeout(function(){
						currentHideIndex++;
						if(!pageItems[currentHideIndex]){
							currentHideIndex=0;
						}
						changePaging();
				},5000);	
		}
//我测试用的left  
		currentItem.style.left=currMwidth+"px";
		hidePage.style.left =hideMwidth+"px";	
	};


	var changePaging=	function(toRight){
			currentMoving=true;
			$(pageItems[currentShowIndex].btn).className="";
			$(pageItems[currentHideIndex].btn).className="show";
			
			var hideItem=$(pageItems[currentHideIndex].itemId);
			var hideItemLeft=moveWidth;
			if(toRight){
				hideItemLeft=0-moveWidth;
				pShowEl.insertBefore(hideItem,$(pageItems[currentShowIndex].itemId)); 
			}else{
				pShowEl.insertBefore(hideItem,$(pageItems[currentShowIndex].itemId).nextSibling);
				
			}
			hideItem.style.left=hideItemLeft+"px";
			hideItem.style.display="";
			carouselInterval=setInterval(function(){changePosition(toRight);},100);
	};

	changePageTimeout=setTimeout(changePaging,5000);
	initPageItemEvent();
}

window.onload=function(){
	    //每次轮播移动的距离,每次移动按moveWidth./20计算
			var moveWidth=663;
		//轮播的数组  按钮和item dom对象的id  从0开始播放
		//对没有按钮的情况没有去兼容  
		//每次过5秒换图。多久播放下一幅图片没有做成配置选项 如修改可做成配置 也可直接替换5000
	var pageItems=[
		{
		btn:"btnPage1",
		itemId:"pageNum1"
		},{
		btn:"btnPage2",
		itemId:"pageNum2"
		},{
		btn:"btnPage3",
		itemId:"pageNum3"
		},{
		btn:"btnPage4",
		itemId:"pageNum4"
		},{
		btn:"btnPage5",
		itemId:"pageNum5"
		}
	];	
	//调用接口播放
	move(moveWidth,pageItems,$("displayDiv"));
};
</script>	
	
</head>

<body >

<div class="sub_box">
			<div id="p-select" class="sub_nav">
				<div class="sub_no" id="appNum">
					<ul >
						<li class="show"  id="btnPage1">1</li>
						<li class="" 	id="btnPage2">2</li>
						<li class=""	id="btnPage3">3</li>
						<li class=""	id="btnPage4">4</li>
						<li class=""	id="btnPage5">5</li>
					</ul>
				</div>
			</div>
			<div id="show" style="position:relative;">
				
					<div style="left:0px;position:absolute;" id="pageNum1">
														<img src="1.png">
					
					</div>
				

			</div>
</div>

<div id="displayDiv">
	<div style="left:663px;position:absolute;display:none;" id="pageNum2">			
									<img src="2.png">			
					</div>
	<div style="left:663px;position:absolute;display:none;" id="pageNum3">					
									<img src="3.png">
					
	</div>
	<div style="left:663px;position:absolute;display:none;" id="pageNum4">	
							<img src="4.png">	
	</div>
	
	<div style="left:663px;position:absolute;display:none;" id="pageNum5">				
								<img src="5.png">				
	</div>
<div>

	

</body>
</html>



附件上传在http://llq056.iteye.com/blog/1513077
论坛首页 Web前端技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics