js點擊按鈕實現(xiàn)多張圖片循環(huán)切換
更新時間:2022年01月25日 10:48:38 作者:丁七歲
這篇文章主要為大家詳細介紹了js點擊按鈕實現(xiàn)多張圖片循環(huán)切換,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了js點擊按鈕實現(xiàn)多張圖片循環(huán)切換的具體代碼,供大家參考,具體內(nèi)容如下
代碼:
<!DOCTYPE html> <html lang="ch"> ? ? <head> ? ? ? ? <meta charset="UTF-8"> ? ? ? ? <title>點擊按鈕實現(xiàn)多張圖片的循環(huán)切換</title> ? ? ? ? <style type="text/css"> ? ? ? ? ? ? * { ? ? ? ? ? ? ? ? margin: 0; ? ? ? ? ? ? ? ? padding: 0; ? ? ? ? ? ? } ? ? ? ? ? ? .img-wrapper { ? ? ? ? ? ? ? ? width: 520px; ? ? ? ? ? ? ? ? height: 520px; ? ? ? ? ? ? ? ? background-size: contain; ? ? ? ? ? ? ? ? overflow: hidden; ? ? ? ? ? ? ? ? margin: 50px auto; ? ? ? ? ? ? ? ? background-color: green; ? ? ? ? ? ? } ? ? ? ? ? ? .img-wrapper img{ ? ? ? ? ? ? ? ? width: 533px; ? ? ? ? ? ? ? ? height: 300px; ? ? ? ? ? ? } ? ? ? ? ? ? .img-wrapper p { ? ? ? ? ? ? ? ? text-align: center; ? ? ? ? ? ? ? ? height: 20px; ? ? ? ? ? ? ? ? line-height: 20px; ? ? ? ? ? ? ? ? font-size: 16px; ? ? ? ? ? ? ? ? margin-bottom: 10px; ? ? ? ? ? ? ? ? margin-top: 8px; ? ? ? ? ? ? } ? ? ? ? ? ? .img-wrapper button { ? ? ? ? ? ? ? ? margin: 12px 93px; ? ? ? ? ? ? ? ? font-size: 18px; ? ? ? ? ? ? } ? ? ? ? </style> ? ? ? ? <script type="text/javascript"> ? ? ? ? ? ? window.onload = function () { ? ? ? ? ? ? ? ? let prev = document.getElementById("prev"); ? ? ? ? ? ? ? ? let next = document.getElementById("next"); ? ? ? ? ? ? ? ? let img = document.getElementsByTagName("img")[0]; ? ? ? ? ? ? ? ? let info = document.getElementById("info"); ? ? ? ? ? ? ? ? //創(chuàng)建一個數(shù)組存儲照片的路徑 ? ? ? ? ? ? ? ? let imgArr = ["img/111.jpg", "img/222.jpg", "img/333.jpg", "img/444.jpg", "img/555.jpg", "img/666.jpg"]; ? ? ? ? ? ? ? ? let index = 0; ? ? ? ? ? ? ? ? info.innerText = "一共有" + imgArr.length + "張照片,現(xiàn)在是第" + (index + 1) + "張"; ? ? ? ? ? ? ? ? prev.onclick = function () { ? ? ? ? ? ? ? ? ? ? index--; ? ? ? ? ? ? ? ? ? ? prev.style.backgroundColor="#ff4c31"; ? ? ? ? ? ? ? ? ? ? if (index < 0) { ? ? ? ? ? ? ? ? ? ? ? ? index = imgArr.length - 1; ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? img.src = imgArr[index]; ? ? ? ? ? ? ? ? ? ? info.innerText = "一共有" + imgArr.length + "張照片,現(xiàn)在是第" + (index + 1) + "張"; ? ? ? ? ? ? ? ? }; ? ? ? ? ? ? ? ? next.onclick = function () { ? ? ? ? ? ? ? ? ? ? index++; ? ? ? ? ? ? ? ? ? ? next.style.backgroundColor="#ff4c31"; ? ? ? ? ? ? ? ? ? ? if (index > imgArr.length - 1) { ? ? ? ? ? ? ? ? ? ? ? ? index = 0; ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? img.src = imgArr[index]; ? ? ? ? ? ? ? ? ? ? info.innerText = "一共有" + imgArr.length + "張照片,現(xiàn)在是第" + (index + 1) + "張"; ? ? ? ? ? ? ? ? }; ? ? ? ? ? ? }; ? ? ? ? </script> ? ? </head> ? ? <body> ? ? ? ? <div class="img-wrapper"> ? ? ? ? ? ? <p id="info"></p> ? ? ? ? ? ? <img src="img/111.jpg"> ? ? ? ? ? ? <button id="prev">上一張</button> ? ? ? ? ? ? <button id="next">下一張</button> ? ? ? ? </div> ? ? </body> </html>
效果:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Three.js中網(wǎng)格對象MESH的屬性與方法詳解
三維開發(fā)渲染最多的對象大概是網(wǎng)格mesh了,Webgl開發(fā)三維也不例外,下面這篇文章主要給大家介紹了關(guān)于Three.js中網(wǎng)格對象MESH的屬性與方法,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面來一起看看吧。2017-09-09JavaScript實現(xiàn)url地址自動檢測并添加URL鏈接示例代碼
寫一個簡單的聊天系統(tǒng),發(fā)出Htpp的Url實現(xiàn)跳轉(zhuǎn)加上a標簽,下面是具體的實現(xiàn),感興趣的朋友不要錯過2013-11-11微信小程序?qū)崿F(xiàn)用戶登錄模塊服務(wù)器搭建
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)用戶登錄模塊服務(wù)器搭建,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05Js+Dhtml:WEB程序員簡易開發(fā)工具包(預(yù)先體驗版)
Js+Dhtml:WEB程序員簡易開發(fā)工具包(預(yù)先體驗版)...2006-11-11webpack踩坑系列之less-loader6.0.0的javascriptEnabled報錯問題
這篇文章主要介紹了webpack踩坑系列之less-loader6.0.0的javascriptEnabled報錯問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01