原生手动实现轮播图
本文最后更新于:2023年3月19日 晚上
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> </head> <body> <div> <div class="swiper-container"> <div class="swiper-list" id="swiperList"> <div class="swiper-item"> <img src="https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=518601442,146924060&fm=26&gp=0.jpg" alt="1" /> </div> <div class="swiper-item"> <img src="https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=518601442,146924060&fm=26&gp=0.jpg" alt="2" /> </div> <div class="swiper-item"> <img src="https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=518601442,146924060&fm=26&gp=0.jpg" alt="3" /> </div> </div> </div> <div class="swiper-dot" id="dotList"></div> <div> <button onclick="handlePrev()">上一张</button> <button onclick="handleNext()">下一张</button> </div> </div>
<script> let imageCount = 0; let index = 0; let interval = 2000;
setInterval(() => { handleNext(); }, interval);
const handlePrev = () => { index = index === 0 ? imageCount - 1 : index - 1; change(); };
const handleNext = () => { index = index === imageCount - 1 ? 0 : index + 1; change(); };
const change = () => { const dotItem = document.querySelector("#dotList").children; const activeSwiperItem = document.querySelector("#swiperList"); console.log(dotItem); for (let i = 0; i < dotItem.length; i++) { if (i === index) dotItem[i].className = "swiper-dot-item active"; else dotItem[i].className = "swiper-dot-item"; } const left = 600 * index; activeSwiperItem.setAttribute("style", `left:-${left}px;`); };
const countImageNum = () => { const imageDoms = document.querySelector("#swiperList"); imageCount = imageDoms.children.length; console.log(imageCount); };
const creatDotDoms = () => { const dotDom = document.querySelector("#dotList"); console.log(dotDom);
for (let i = 0; i < imageCount; i++) { let div = document.createElement("div"); div.className = "swiper-dot-item"; dotDom.appendChild(div); } };
window.onload = function () { countImageNum(); creatDotDoms(); change(); }; </script>
<style> .swiper-container { width: 600px; height: 300px; overflow: hidden; position: relative; } .swiper-list { display: flex; flex-wrap: nowrap; position: absolute; transition: left 1s; left: 0; }
.swiper-item { list-style-type: none; } .swiper-item img { width: 600px; height: 300px; }
.swiper-dot { width: 600px; display: flex; justify-content: space-around; }
.swiper-dot-item { width: 20px; height: 10px; border-radius: 5px; background-color: #000; }
.active { background-color: #f00; } </style> </body> </html>
|