jquery轮播图

效果图如下:


HTML部分:

<div class="main">
		<div class="view">
			<ul class="imageBox">
				<li><img src="../images/0.jpg"></li>
				<li><img src="../images/1.jpg"></li>
				<li><img src="../images/2.jpg"></li>
				<li><img src="../images/3.jpg"></li>
			</ul>
			<div class="btn">
				<div class="prev"></div>
				<div class="next"></div>
			</div>
		</div>
		<ul class="imgIcon">
			<li><img src="../images/0.jpg"></li>
			<li><img src="../images/1.jpg"></li>
			<li><img src="../images/2.jpg"></li>
			<li style="margin-right: 0"><img src="../images/3.jpg"></li>
		</ul>

js部分:

$(function(){
			let $imageBox=$(".imageBox"),
				$btn=$(".btn"),
				imgWidth=$(".imageBox img").width(),
				$imgIcon=$(".imgIcon").find("img"),
				$main=$(".main"),
				index=0,
				timer;
			//下方小图样式的改变
			let show=()=>{
				$imgIcon.eq(index).addClass("change").parent("li").siblings().children().removeClass("change");
			}
			//动画
			let animate=(num1)=>{
				if(!$imageBox.is(":animated")){
					//将index=index+num1写在animate方法中是防止快速连续点击按钮引发错误
					index=index+num1;
					let Left=`${-index*imgWidth}px`;
					$imageBox.animate({
						left:Left
					});
					show();
				}
			}
			//自动播放轮播图
			let auto=()=>{
				timer=setInterval(function(){
					$btn.find(".next").trigger("click");
				},2000);
			}
			//改变下方第一张小图片的样式
			$imgIcon.eq(0).addClass("change");
			//右方按钮
			$btn.find(".next").click(function(){
				if (index==3) {
					index=-1;
				}
				animate(1);
			});
			//左方按钮
			$btn.find(".prev").click(function(){
				if (index==0) {
					index=4;
				}
				animate(-1);
			});
			//下方小图的点击
			$imgIcon.click(function(){
				index=$(this).parent("li").index();
				animate(0);
			});
			auto();
			//移入停止自动轮播,移出开始轮播
			$main.hover(function(){
				clearInterval(timer);
			},function(){
				auto();
			});
		})

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值