jquery 实现图片轮播

<script src="jquery-1.11.1.js"></script>
<script>
var $carousel = new function(){
	this.holderWidth = 0; //内容器宽度
	this.mills = 5000; //自动轮播间隔
	this.rollpos = 0;	//当前位置
	this.width = 0;	//外容器宽度
	this.height = 0; //外容器高度
	this.bAuto = false; //是否自动轮播
	this.timer = null; //
	this.bLoop = false; //是否循环
	this.init = function(params){
		if (params.width != null)
		{
			$("#carousel").css("width", params.width + "px");
		}
		if (params.height != null)
		{
			$("#carousel").css("height", params.height + "px");
		}
		if (params.bAuto != null)
		{
			this.bAuto = params.bAuto;
		}
		if (params.mills != null)
		{
			this.mills = params.mills;
		}
		if (params.bLoop != null)
		{
			this.bLoop = params.bLoop;
		}
		
		if (this.bAuto)
		{
			this.timer = setInterval(this.autoRoll, this.mills);
		}
		this.width = parseInt($("#carousel").css("width"));
		this.height = parseInt($("#carousel").css("height"));


		var width = 0;
		$("#holder").find("img").each(function(){
			width += parseInt($(this).css("width"));
		});
		this.holderWidth = width;
		$("#holder").css("width", this.holderWidth);
		
	}
	this.autoRoll = function(){
		$carousel.roll(1);
	}
	this.roll = function(flag){
		if (flag==1)
		{
			if (this.rollpos <= (-this.holderWidth + this.width))
			{
				if (this.bLoop)
				{
					this.rollpos = 0;
				}
			}
			else
			{
				this.rollpos += -this.width;
			}
			
		}
		else if (flag == 2)
		{
			if (this.rollpos < 0)
			this.rollpos += this.width;
		}
		$ul = $("#holder");
		$ul.animate({'left': this.rollpos + 'px'},"slow");
	}
	this.stopRoll = function(){
		if (this.timer != null)
		{
			clearInterval(this.timer);
		}
	}
}

$(function(){
	$carousel.init({bAuto:true,bLoop:true,width:348});
});

</script>
<style>
	
	#carousel{
		width:538px;
		border:1px solid red;
		overflow:hidden;
		position:relative;
	}
	#carousel ul{
	   position:relative;
	   list-style-type:none;
	   margin:0;
	   padding:0;
	}
	#carousel ul li{
		float:left;
	}
	
</style>


<body>
	<div id="carousel" style="padding:10px;float:left">
		<div style="position:relative;overflow:hidden;">
			<ul id="holder">
				<li><img src="ban1.gif"></li>
				<li><img src="ban2.gif"></li>
				<li><img src="ban3.gif"></li>
			</ul>
		</div>
	</div>


	<div>
		dddddddddddddddd
	</div>


	<button οnclick="$carousel.roll(2)"><<</button>
	<button οnclick="$carousel.roll(1)">>></button>
</body>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值