First, the layout
<style>. container{position:relative; width:500px; height:180px; margin:100px Auto; }. Container ul{Position:absolute; bottom:0; width:100%; height:30px; Background:rgba (0,0,0,0.4); }. Container li{width:10px; height:10px; MARGIN:9PX 18px; border-radius:50%; line-height:10px; margin-right:10px; border:1px solid red; Background:rgba (255,255,255,1); Float:left; }. Container li.active{border:1px solid red; Background:rgba (255,0,0,1); }. Container. arrow{Position:absolute; top:50%; Margin-top: -25px; width:30px; height:50px; line-height:50px; font-size:30px; Background:rgba (255,255,255,0.4); }. container #leftArrow {left:0; }. container #rightArrow {right:0; } </style>
<body> <div class= "container" > <div class= "Imgbox" > </div> <ul> <li> 1</li> <li> 2</li> <li> 3</li> <li> 4 </li> <li> 5 </li> <li> 6</li> </ul> <div class= "Arrow" id= "Leftarrow" > < </div> <div class= "Arrow" id= "RightArrow" > > </div> </div></body>
Second, JavaScript implementation
<script>
/* JavaScript picture carousel: 1 Timer set auto loop Playback 2 mouse stops at the dot below (Li tag) to display the corresponding subscript on the Picture 3 button click to display the corresponding previous picture
1. Timer settings Auto Loop playback: Change the src attribute of the image and the corresponding dot (li tag) highlight the style
2. The mouse stops at the small dots below (li tag) to display the corresponding subscript picture, always hover the picture does not move (note: The timer stops playing the picture when hovering over the mouse)
When the mouse moves out, start the timer loop to play the picture (note: Start playing from the next picture of the hover, not the order of the original timer to play the picture)
3. Click the button to display the current picture corresponding to the next picture */
var srcarr= ["Imgplay/dd_scroll_1.jpg", "imgplay/dd_scroll_2.jpg", "imgplay/dd_scroll_3.jpg", "ImgPlay/dd_scrol L_4.jpg "," imgplay/dd_scroll_5.jpg "," imgplay/dd_scroll_6.jpg "]; var img = document.getelementsbyclassname ("container") [0].getelementsbytagname ("img") [0]; var Arrli = document.getelementsbyclassname ("container") [0].getelementsbytagname ("Li"); var arrarrow = document.getelementsbyclassname ("container") [0].getelementsbyclassname ("Arrow"); Li custom subscript for (var i = 0; i<arrli.length; i++) {arrli[i].index = i; }//Initialize var lastactiveli =null; var activeindex = 0; IMG.SRC = srcarr[Activeindex]; arrli[activeindex].classname= "Active"; Lastactiveli = arrli[Activeindex]; Img.timer = null; Scroll 1: Turn on timer picture scrolling Img.timer =window.setinterval (function () {lastactiveli.classname= ""; activeindex= (++activeindex)% Srcarr.length; IMG.SRC = srcarr[Activeindex]; arrli[Activeindex]. classname= "Active"; Lastactiveli = arrli[Activeindex]; }, 1000); Scroll 2: Mouse moves on Li to move out for (var i = 0; i<arrli.length; i++) {arrli[i].onmouseover = function () {LASTACTI Veli.classname= ""; Activeindex=this.index; IMG.SRC = srcarr[Activeindex]; arrli[activeindex].classname= "Active"; Lastactiveli = arrli[Activeindex]; Window.clearinterval (img.timer); }; Arrli[i].onmouseout = function () {Img.timer =window.setinterval (function () {Lastactiveli.class Name= ""; activeindex= (++activeindex)% Srcarr.length; IMG.SRC = srcarr[Activeindex]; arrli[activeindex].classname= "Active"; Lastactiveli = arrli[Activeindex]; }, 1000); }; }//Scroll 3: Click the left and right arrows on the next picture Arrarrow[0].onclick = function () {lastactiveli.classname= ""; activeindex= (--activeindex +srcarr.length)% Srcarr.length; IMG.SRC = srcarr[Activeindex]; arrli[activeindex].classname= "Active"; Lastactiveli = arrli[Activeindex]; } Arrarrow[1].onclick = function () {lastactiveli.classname= ""; activeindex= (++activeindex)% Srcarr.length; IMG.SRC = srcarr[Activeindex]; arrli[activeindex].classname= "Active"; Lastactiveli = arrli[Activeindex]; }</script>
Third, JavaScript function encapsulation
var srcarr= ["Imgplay/dd_scroll_1.jpg", "imgplay/dd_scroll_2.jpg", "imgplay/dd_scroll_3.jpg", "ImgPlay/dd_scrol L_4.jpg "," imgplay/dd_scroll_5.jpg "," imgplay/dd_scroll_6.jpg "]; var img = document.getelementsbyclassname ("container") [0].getelementsbytagname ("img") [0]; var Arrli = document.getelementsbyclassname ("container") [0].getelementsbytagname ("Li"); var arrarrow = document.getelementsbyclassname ("container") [0].getelementsbyclassname ("Arrow"); Custom functions function Playimg (activeindex) {lastactiveli.classname= ""; IMG.SRC = srcarr[Activeindex]; arrli[activeindex].classname= "Active"; Lastactiveli = arrli[Activeindex]; }//li the custom subscript for (var i = 0; i<arrli.length; i++) {arrli[i].index = i; }//Initialize var lastactiveli =null; var activeindex = 0; IMG.SRC = srcarr[Activeindex]; arrli[activeindex].classname= "Active"; Lastactiveli = arrli[Activeindex]; Img.timer = null; //scroll 1: Turn on timer picture scrolling Img.timer =window.setinterval (function () {activeindex= (++activeindex)% Srcarr.length; Playimg (Activeindex); }, 1000); Scroll 2: Mouse moves on Li to move out for (var i = 0; i<arrli.length; i++) {arrli[i].onmouseover = function () {Activein Dex=this.index; Playimg (Activeindex);Window.clearinterval (img.timer);}; Arrli[i].onmouseout = function () {Img.timer =window.setinterval (function () {activeindex= (++ac Tiveindex)% Srcarr.length; Playimg (Activeindex); }, 1000); }; }//Scroll 3: Click the left and right arrows on the next picture Arrarrow[0].onclick = function () {activeindex= (--activeindex +srcarr.length)% Srca Rr.length; Playimg (Activeindex); } Arrarrow[1].onclick = function () {activeindex= (++activeindex)% Srcarr.length; Playimg (Activeindex); }
JavaScript Picture Carousel