js實(shí)現(xiàn)輪播圖特效
本文實(shí)例為大家分享了js實(shí)現(xiàn)輪播圖特效的具體代碼,供大家參考,具體內(nèi)容如下
只需要修改圖片的src即可
html:
<body> <div id='rollImgBox'> <div class='photos clearfix'> <!--輪播圖里面首位多放最后一張與第一張圖片,以便順暢平滑切換--> <div class='move'><img src='http://www.4tl426be.cn/bcjs/img/timg%20(7).jpg' alt=''></div> <div class='move'><img src='http://www.4tl426be.cn/bcjs/img/timg%20(4).jpg' alt=''></div> <div class='move'><img src='http://www.4tl426be.cn/bcjs/img/timg%20(5).jpg' alt=''></div> <div class='move'><img src='http://www.4tl426be.cn/bcjs/img/timg%20(6).jpg' alt=''></div> <div class='move'><img src='http://www.4tl426be.cn/bcjs/img/timg%20(7).jpg' alt=''></div> <div class='move'><img src='http://www.4tl426be.cn/bcjs/img/timg%20(4).jpg' alt=''></div> </div> <!--points圓點(diǎn)導(dǎo)航,js動(dòng)態(tài)生成--> <div class='points'></div> <!--如果需要向左與向右的按鍵,引入方向圖片--> <span class='leftPoint'> < </span> <span class='rightPoint'> > </span> </div></body>
style:
*{ margin: 0; padding: 0;}.clearfix{ zoom: 1;}.clearfix:after{ content: ''; display: block; height: 0; visibility: hidden; clear: both;}#rollImgBox{ /*這里讓盒子居中,應(yīng)用到具體頁面刪除即可*/ margin: 20px auto; /*如果該輪播圖不是獨(dú)占一行,需要將其改為行內(nèi)塊元素*/ display: block; position: relative; /*在這里設(shè)置裝載圖片的框框的寬高*/ width: 947px; height: 585px; /*在這里設(shè)置邊框的樣式用outline,這樣就不會(huì)影響到后面的js了 /*加邊框,用outline即可,不會(huì)影響實(shí)際的距離*/ outline: 5px solid blue; overflow: hidden;}#rollImgBox .photos .move img{ /*在這里設(shè)置圖片的寬高,與邊框的寬高相同*/ width: 947px; height: 585px;}#rollImgBox .photos{ position: relative; /*移動(dòng)的是圖片的寬度,左移947px*/ left: -947px;}#rollImgBox:hover{ cursor: pointer;}#rollImgBox .photos div{ float: left;}#rollImgBox .points{ position: absolute; /*在這里修改圓點(diǎn)導(dǎo)航的位置*/ bottom: 30px; right: 170px;/*右下方*/ text-align: center;}#rollImgBox .points span{ display: inline-block; /*在這里可以更改圓點(diǎn)的大小*/ text-align: center; line-height: 66px; font-size: 24px; font-family: 微軟雅黑; width: 66px; height: 66px; background: rgba(112,117,112,.6); border-radius: 50%; margin-left: 15px;}#rollImgBox .points .pointsNow{ background: rgba(62,255,49,.6);}/*左右按鈕*/#rollImgBox .leftPoint{ width: 60px; height: 60px; background: rgba(0,0,0,.5); text-align: center; line-height: 60px; position: absolute; font-size: 30px; color: white; top: 290px; left: 0;}#rollImgBox .rightPoint{ width: 60px; height: 60px; background: rgba(0,0,0,.5); text-align: center; line-height: 60px; position: absolute; font-size: 30px; color: white; top: 290px; right: 0;}#rollImgBox .leftPoint:hover{ background: rgba(255,0,0,.5);}#rollImgBox .rightPoint:hover{ background: rgba(255,0,0,.5);}
script:
window.onload = function(){ let rollImgBox = document.querySelector('#rollImgBox'); let photos = document.querySelector('#rollImgBox .photos'); let allimg = document.querySelectorAll('#rollImgBox .move img'); let index = 2; //動(dòng)態(tài)設(shè)計(jì)移動(dòng)圖片的框框?qū)捀?//(rollImgBox.offsetWidth)是要剪去邊框的寬度 photos.style.width = (allimg.length)*(rollImgBox.offsetWidth) + 'px'; photos.style.height = rollImgBox.offsetHeight + 'px'; //動(dòng)態(tài)創(chuàng)建小圓點(diǎn) let point = new Array(); let points = document.querySelector('#rollImgBox .points'); for (let i=0;i<(allimg.length-2);i++){ point[i] = document.createElement('span'); point[i].innerHTML = (i+1); points.appendChild(point[i]); } point[0].className = 'pointsNow'; let rollImgIterval = setInterval(function () { //圖片的輪播 if (index === allimg.length){ photos.style.left = 0; index = 1; photos.style.transition = '0s'; point[0].className = 'pointsNow'; } else { photos.style.transition = '1.5s'; } photos.style.left = -(rollImgBox.offsetWidth)*index + 'px'; index++; //小圓點(diǎn)的變換 for (let j=0;j<(allimg.length-2);j++){ if (j === index-2){ point[j].className = 'pointsNow'; } else { point[j].className = ''; } } //這里是最后一張圖片(與展現(xiàn)的第一張一樣的圖)設(shè)置小圓點(diǎn)樣式 if (index === allimg.length){ point[0].className = 'pointsNow'; } },2000); //當(dāng)用戶把鼠標(biāo)放到rollImgBox盒子中,需要查看圖片,自動(dòng)輪播停止 rollImgBox.onmouseover = function () { clearInterval(rollImgIterval); }; rollImgBox.onmouseout = function () { rollImgIterval = setInterval(function () { //圖片的輪播 if (index === allimg.length){ photos.style.left = 0; index = 1; photos.style.transition = '0s'; point[0].className = 'pointsNow'; } else { photos.style.transition = '1.5s'; } photos.style.left = -(rollImgBox.offsetWidth)*index + 'px'; index++; //小圓點(diǎn)的變換 for (let j=0;j<(allimg.length-2);j++){ if (j === index-2){ point[j].className = 'pointsNow'; } else { point[j].className = ''; } } //這里是最后一張圖片(與展現(xiàn)的第一張一樣的圖)設(shè)置小圓點(diǎn)樣式 if (index === allimg.length){ point[0].className = 'pointsNow'; } },2000); }; //點(diǎn)擊小圓點(diǎn),跳轉(zhuǎn)到對(duì)應(yīng)的圖片位置 for (let k=0;k<(allimg.length-2);k++){ point[k].onmousedown = function () { photos.style.left = -(rollImgBox.offsetWidth)*(k+1) + 'px'; //小圓點(diǎn)的變換 for (let j=0;j<(allimg.length-2);j++){ if (j === k){ point[j].className = 'pointsNow'; } else { point[j].className = ''; } } //點(diǎn)擊小圓點(diǎn)之后更改index的值 index = k+2; } } //點(diǎn)擊左右方向鍵,對(duì)圖片進(jìn)行滑動(dòng) let leftPoint = document.querySelector(’#rollImgBox .leftPoint’); let rightPoint = document.querySelector(’#rollImgBox .rightPoint’); leftPoint.onclick = function () { photos.style.transition = '1s'; //向左滑動(dòng)一張圖片,并修改index的值(index--) let dis = index-2; //當(dāng)dis為1時(shí),圓點(diǎn)到達(dá)第一個(gè)位置,如果再往左移動(dòng)一個(gè),圓點(diǎn)應(yīng)該到達(dá)最后一個(gè)位置 if (dis < 1){ dis = allimg.length-2; photos.style.left = 0; point[dis-1].className = 'pointsNow'; point[0].className = ''; index = allimg.length; } else { photos.style.left = -(rollImgBox.offsetWidth)*dis + 'px'; point[dis-1].className = 'pointsNow'; point[dis].className = ''; } //從第一張順滑切換到最后一張 setTimeout(function () { if (photos.style.left === ’0px’){ photos.style.left = -(rollImgBox.offsetWidth)*(allimg.length-2) + 'px'; photos.style.transition = ’0s’; index = allimg.length-1; } },1000); index--; }; rightPoint.onclick = function () { photos.style.transition = '1s'; //向右滑動(dòng)一張圖片,并修改index的值(index++) let dis = index-1; //當(dāng)dis為5時(shí),圓點(diǎn)到達(dá)最后一個(gè)位置,如果再往右移動(dòng)一個(gè),圓點(diǎn)應(yīng)該到達(dá)第一個(gè)位置 if (dis >= (allimg.length-2)){ photos.style.left = -(rollImgBox.offsetWidth)*(allimg.length-1) + 'px'; point[0].className = 'pointsNow'; point[allimg.length-3].className = ''; index = 1; } else { photos.style.left = -(rollImgBox.offsetWidth)*index + 'px'; point[dis].className = 'pointsNow'; point[dis-1].className = ''; } //從最后一張順滑切換到第一張 setTimeout(function () { if (photos.style.left === ((-(rollImgBox.offsetWidth)*(allimg.length-1))+’px’)){ photos.style.left = -(rollImgBox.offsetWidth) + 'px'; photos.style.transition = ’0s’; index = 2; } },1000); index++; };};
更多關(guān)于輪播圖效果的專題,請(qǐng)點(diǎn)擊下方鏈接查看學(xué)習(xí)
javascript圖片輪播效果匯總
jquery圖片輪播效果匯總
Bootstrap輪播特效匯總
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng),大家繼續(xù)關(guān)注更多精彩焦點(diǎn)輪播圖。
相關(guān)文章:
1. python爬蟲實(shí)戰(zhàn)之制作屬于自己的一個(gè)IP代理模塊2. 基于javaweb+jsp實(shí)現(xiàn)企業(yè)財(cái)務(wù)記賬管理系統(tǒng)3. .NET6打包部署到Windows Service的全過程4. 解決ajax請(qǐng)求后臺(tái),有時(shí)收不到返回值的問題5. HTML 絕對(duì)路徑與相對(duì)路徑概念詳細(xì)6. Python編寫nmap掃描工具7. Ajax返回值類型與用法實(shí)例分析8. .Net Core和RabbitMQ限制循環(huán)消費(fèi)的方法9. 使用FormData進(jìn)行Ajax請(qǐng)求上傳文件的實(shí)例代碼10. 如何在jsp界面中插入圖片
