如何使用JQ封裝輪播圖

恩阿發表於2020-12-19

css部分:

*{
			margin: 0;
			padding: 0;
		}
		#banner{
			width: 800px;
			height: 500px;
			margin: 30px auto;
			position: relative;
			overflow: hidden;
		}
		#box_wrap img{
			width: 100%;
			height: 500px;	
			cursor: pointer;	
		}
		.listdian{
			width: 150px;
			position: absolute;
			bottom: 20px;
			right: 30px;
			display: flex;
			justify-content: space-around;
		}
		.listdian span{
			width: 20px;
			height: 20px;
			border: 1px solid;
			text-align: center;
			border-radius: 50%;
			background: pink;
			color: white;
			cursor: pointer;
		}
		.anniu{
			position: relative;	
			cursor: pointer;
		}
		.anniu span{
			font-size: 50px;
			color: rgba(250, 150, 195, 0.5);
			background: rgba(0, 0, 0, 0.2);
		}
		.anniu .left{
			position: absolute;
			right: 95%;
			bottom: 230px;
		}
		.anniu .right{
			position: absolute;
			left: 95%;
			bottom: 230px;	
		}
		.listdian>.active{
			background-color: red;
		}

html部分:

		
			<div id="banner">
				<!--圖片部分-->
				 <div id="box_wrap">
					    <img class="item" src="./timg (1).jpg"  alt="">
					    <img class="item" src="./timg (2).jpg" alt="">
					    <img class="item" src="./timg (3).jpg" alt="">
					    <img class="item" src="./timg (4).jpg" alt="">
					    <img class="item" src="./timg.jpg" alt="">  
			 	 </div>
			 	 <!--圓點部分-->
			 	 <div class="listdian">
			 	 		<span class="tab" class="active">1</span>
			 	 		<span class="tab">2</span>
			 	 		<span class="tab">3</span>
			 	 		<span class="tab">4</span>
			 	 		<span class="tab">5</span>
			 	 </div>
			 	 <!--按鈕部分-->
			 	 <div class="anniu">
			 	 		<span class="left">&lt;</span>
			 	 		<span class="right">&gt;</span>
			 	 </div>
	 		 </div>
<script src="./jquery-3.4.1.min.js"></script>
		<script>
            $(document).ready(function(){
                var i=0;
                var timer;
				$(".item").eq(0).show().siblings(".item").hide();
				$(".tab").eq(0).addClass("active").siblings().removeClass("active")
                showTime();
                $("#box_wrap>.item").hover(function(){
					clearInterval(timer);  
                                       
                },function(){
                    showTime();
                });
                $(".left").click(function(){
					clearInterval(timer);					
                    if(i<1){
                        i=5
                    }
					i--;
                  show();                 
                })
                $(".right").click(function(){
					clearInterval(timer);
					
                    if(i==4){
                        i=-1;
                    }
                    i++;
                    show();                  
                })
                function showTime(){
                    timer=setInterval(function(){
                        show();
                        i++;
                        if(i==5){
                        i=0;
                        }
                    },2000);
				}
				$(".tab").click(function(){
					i=$(this).index()
					clearInterval(timer);
					show()
				})
                function show(){
                    $(".item").eq(i).fadeIn(0).siblings(".item").fadeOut(0);
                    $(".tab").eq(i).addClass("active").siblings(".tab").removeClass("active");
                }
            })
         </script>

相關文章