The example in this article shares with you the implementation of image carousel effects using JavaScript for your reference. The specific content is as follows
1. Realizing the effect
As shown above:
1. The pictures are automatically rotated in sequence. Each time a picture is rotated, the corresponding small icon below will have a red border and the corresponding picture name will be displayed
2. When the mouse is placed on the large picture, the picture stops rotating and the current picture is always displayed; after the mouse is moved away, the picture continues to rotate
3. When the mouse is moved over the small icon, the corresponding large image will be displayed in the large picture area; when the mouse is moved away, the rotation will continue from the current picture
2. Code
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>带小图标的JS图片轮换</title> <style type="text/css"> *{ margin: 0px; padding: 0px; } #content{ width: 700px; height: 538px; margin: 0px auto; /*使所有内容居中*/ border: solid #F0F0F0; } /*定义下面小图标处样式*/ #nav1 table{ width: 100%; height: 35px; margin-top: -4px; } #nav1 td{ width: 35px; height: 35px; text-align: center; /*文字居中*/ color: #ffffff; } #text{ } #_text{ width: 100%; height: 100%; background-color: #F0F0F0; border: none; text-align: center; font-size: 18px; color: #000000; font-weight: bold; } </style> </head> <body onload="changeImg()"> <div id="content"> <div id="images"> <img id="_photoes" src="../images/textPhoto/1.jpg" height="500px" width="700px" onmouseover="over1()" onmouseout="out1()"> </div> <div id="nav1"> <table border="0"> <tr> <td id="text" bgcolor="#F0F0F0" colspan="15"><input type="text" id="_text" readonly></td> <td id="img1" class="sImg" bgcolor="#db7093" onmouseover="over2(1)" onmouseout="out2(1)">1</td> <td id="img2" class="sImg" bgcolor="#da70d6" onmouseover="over2(2)" onmouseout="out2(2)">2</td> <td id="img3" class="sImg" bgcolor="#9acd32" onmouseover="over2(3)" onmouseout="out2(3)">3</td> <td id="img4" class="sImg" bgcolor="#adff2f" onmouseover="over2(4)" onmouseout="out2(4)">4</td> <td id="img5" class="sImg" bgcolor="#00bfff" onmouseover="over2(5)" onmouseout="out2(5)">5</td> </tr> </table> </div> </div> <script type="text/javascript"> //将轮换的大图片放入数组中 var arr = new Array(); arr[0] = "../images/textPhoto/1.jpg"; arr[1] = "../images/textPhoto/2.jpg"; arr[2] = "../images/textPhoto/3.jpg"; arr[3] = "../images/textPhoto/4.jpg"; arr[4] = "../images/textPhoto/5.jpg"; //将input区域变换的文字放在数组里 var text = new Array(); text[0] = "焦点图1"; text[1] = "焦点图2"; text[2] = "焦点图3"; text[3] = "焦点图4"; text[4] = "焦点图5"; var smallImg = document.getElementsByClassName("sImg"); //将页面上所有小图片存放在一个数组 var num = 0; function changeImg() { document.getElementById("_photoes").src = arr[num]; document.getElementById("_text").value = text[num]; //当前小图标增加边框样式 for(var i=0;i<arr.length;i++) { if(i==num) smallImg[num].style.border = "red solid"; //大图标对应的小图标增加边框样式 else smallImg[i].style.border = "none"; } if (num == arr.length - 1) num = 0; //如果显示的是最后一张图片,则图片序号变为第一张的序号 else num += 1; //图片序号加一 } var setID = setInterval("changeImg()",1000); //这样写任然会不断调用changeImg()函数 /*当鼠标滑到大图标上时*/ function over1() { clearInterval(setID); //图片停止轮换 // smallImg[n-1].style.border = "red solid"; } /*当鼠标离开大图标时*/ function out1() { setID = setInterval("changeImg()",1000); //图片继续轮换 // smallImg[n-1].style.border = "none"; //大图标对应的小图标边框样式取消 } /*当鼠标滑到小图标上时*/ function over2(n) { document.getElementById("_photoes").src = arr[n-1]; //只要鼠标滑到小图标上,大图区域就显示对应的图片 document.getElementById("_text").value = text[n-1]; clearInterval(setID); //图片停止轮换 //当前小图标增加边框样式 for(var i=0;i<arr.length;i++) { if(i==n-1) smallImg[n-1].style.border = "red solid"; else smallImg[i].style.border = "none"; } } /*当鼠标离开小图标时*/ function out2(n) { if(n!=arr.length) num = n; //从当前图片开始轮换 else num = 0; setID = setInterval("changeImg()",1000); //图片继续轮换 // smallImg[n-1].style.border = "none"; //小图标边框样式取消 } </script> </body> </html>
3. Multiple image rotation debugging notes
js source code:
//实现几张图片的轮换 var num = 0; //显示的图片序号,刚开始时是第一张图片 function changeImg1() { var arr = new Array(); arr[0]="../images/hao123/7.jpg"; arr[1]="../images/hao123/8.jpg"; arr[2]="../images/hao123/9.jpg"; var photo = document.getElementById("topPhoto"); if (num == arr.length - 1) num = 0; //如果显示的是最后一张图片,则图片序号变为第一张的序号 else num += 1; //图片序号加一 photo.src = arr[num]; } setInterval("changeImg1()",5000); //每隔5000毫秒调用一次changImg1()函数
HTML code:
When using it, it is best to define the style of the picture and unify the length and width of the picture, so that the dynamic display effect of the picture will be better.
The above is the entire content of this article. I hope it will be helpful to everyone in learning javascript programming.