使用JavaScript實(shí)現(xiàn)輪播圖效果
本文實(shí)例為大家分享了JavaScript實(shí)現(xiàn)輪播圖效果的具體代碼,供大家參考,具體內(nèi)容如下
純js,不使用輪播圖控件怎么做輪播圖呢,往下看吧
效果圖:
1.可點(diǎn)擊小圓點(diǎn)切換圖片
2.可點(diǎn)擊左右按鈕切換圖片
3.可自動(dòng)播放
先上CSS和HTML代碼:
<body> ? ? <div class="main"> ? ? ? ? <div class="focus fl"> ? ? ? ? ? ? <a href="javascript:;" class="arrow-l"><</a> ? ? ? ? ? ? <a href="javascript:;" class="arrow-r"> > </a> ? ? ? ? ? ? <ul> ? ? ? ? ? ? ? ? <li> ? ? ? ? ? ? ? ? ? ? <a href="#" ><img src="../02/15輪播圖制作/upload/focus.jpg" alt=""></a> ? ? ? ? ? ? ? ? </li> ? ? ? ? ? ? ? ? <li> ? ? ? ? ? ? ? ? ? ? <a href="#" ><img src="../02/15輪播圖制作/upload/focus1.jpg" alt=""></a> ? ? ? ? ? ? ? ? </li> ? ? ? ? ? ? ? ? <li> ? ? ? ? ? ? ? ? ? ? <a href="#" ><img src="../02/15輪播圖制作/upload/focus2.jpg" alt=""></a> ? ? ? ? ? ? ? ? </li> ? ? ? ? ? ? ? ? <li> ? ? ? ? ? ? ? ? ? ? <a href="#" ><img src="../02/15輪播圖制作/upload/focus3.jpg" alt=""></a> ? ? ? ? ? ? ? ? </li> ? ? ? ? ? ? ? </ul> ? ? ? ? ? ? <ol class="circle"> ? ? ? ? ? ? ? </ol> ? ? ? ? </div> ? ? </div> ? </body>
<style> ? ? ? ? * { ? ? ? ? ? ? padding: 0; ? ? ? ? ? ? margin: 0; ? ? ? ? } ? ? ? ?? ? ? ? ? a { ? ? ? ? ? ? text-decoration: none; ? ? ? ? } ? ? ? ?? ? ? ? ? ol { ? ? ? ? ? ? list-style: none; ? ? ? ? } ? ? ? ?? ? ? ? ? .main { ? ? ? ? ? ? width: 980px; ? ? ? ? ? ? height: 455px; ? ? ? ? ? ? margin-left: 440px; ? ? ? ? ? ? margin-top: 10px; ? ? ? ? } ? ? ? ?? ? ? ? ? .focus { ? ? ? ? ? ? position: relative; ? ? ? ? ? ? width: 721px; ? ? ? ? ? ? height: 455px; ? ? ? ? ? ? background-color: purple; ? ? ? ? ? ? overflow: hidden; ? ? ? ? } ? ? ? ?? ? ? ? ? .focus ul { ? ? ? ? ? ? position: absolute; ? ? ? ? ? ? top: 0; ? ? ? ? ? ? left: 0; ? ? ? ? ? ? width: 600%; ? ? ? ? } ? ? ? ?? ? ? ? ? .focus ul li { ? ? ? ? ? ? float: left; ? ? ? ? ? ? list-style: none; ? ? ? ? } ? ? ? ?? ? ? ? ? .arrow-l, ? ? ? ? .arrow-r { ? ? ? ? ? ? display: none; ? ? ? ? ? ? position: absolute; ? ? ? ? ? ? top: 50%; ? ? ? ? ? ? margin-top: -20px; ? ? ? ? ? ? width: 24px; ? ? ? ? ? ? height: 40px; ? ? ? ? ? ? background: rgba(0, 0, 0, .3); ? ? ? ? ? ? text-align: center; ? ? ? ? ? ? line-height: 40px; ? ? ? ? ? ? color: #fff; ? ? ? ? ? ? font-family: 'icomoon'; ? ? ? ? ? ? font-size: 18px; ? ? ? ? ? ? z-index: 2; ? ? ? ? } ? ? ? ?? ? ? ? ? .arrow-r { ? ? ? ? ? ? right: 0; ? ? ? ? } ? ? ? ?? ? ? ? ? .circle { ? ? ? ? ? ? position: absolute; ? ? ? ? ? ? bottom: 10px; ? ? ? ? ? ? left: 50px; ? ? ? ? } ? ? ? ?? ? ? ? ? .circle li { ? ? ? ? ? ? float: left; ? ? ? ? ? ? width: 8px; ? ? ? ? ? ? height: 8px; ? ? ? ? ? ? /*background-color: #fff;*/ ? ? ? ? ? ? border: 2px solid rgba(255, 255, 255, 0.5); ? ? ? ? ? ? margin: 0 3px; ? ? ? ? ? ? border-radius: 50%; ? ? ? ? ? ? /*鼠標(biāo)經(jīng)過顯示小手*/ ? ? ? ? ? ? cursor: pointer; ? ? ? ? } ? ? ? ?? ? ? ? ? .current { ? ? ? ? ? ? background-color: #fff; ? ? ? ? } ?</style>
最后就是JS代碼部分了
window.addEventListener('load', function() { ? ? // 獲取要使用到的元素 ? ? var arrow_l = document.querySelector('.arrow-l'); ? ? var arrow_r = document.querySelector('.arrow-r'); ? ? var focus = document.querySelector('.focus'); ? ? var focusWidth = focus.offsetWidth; ? ? //鼠標(biāo)移動(dòng)到圖片上,顯示左右切換的按鈕 ? ? focus.addEventListener('mouseenter', function() { ? ? ? ? arrow_l.style.display = 'block'; ? ? ? ? arrow_r.style.display = 'block'; ? ? //清除定時(shí)器,不再自動(dòng)播放 ? ? ? ? clearInterval(timer); ? ? ? ? timer = null; //清除定時(shí)器變量 ? ? }); ? ? //鼠標(biāo)離開,左右切換按鈕隱藏 ? ? focus.addEventListener('mouseleave', function() { ? ? ? ? arrow_l.style.display = 'none'; ? ? ? ? arrow_r.style.display = 'none'; ? ? ? ? timer = setInterval(function() { ? ? ? ? ? ? arrow_r.click(); ? ? ? ? }, 2000) ? ? }); ? ? var ul = focus.querySelector('ul'); ? ? var ol = focus.querySelector('.circle'); ? ? console.log(ol); ? ? for (var i = 0; i < ul.children.length; i++) { ? ? ? ? var li = document.createElement('li'); ? ? //創(chuàng)建自定義屬性index ? ? ? ? li.setAttribute('index', i); ? ? //根據(jù)li(圖片)的個(gè)數(shù)自動(dòng)添加左下角的小圓點(diǎn) ? ? ? ? ol.appendChild(li); ? ? ? ? li.addEventListener('click', function() { ? ? ? ? ? ? for (var i = 0; i < ol.children.length; i++) { ? ? ? ? ? ? ? ? ol.children[i].className = ''; ? ? ? ? ? ? } ? ? ? ? ? ? this.className = 'current'; ? ? ? ? ? ? var index = this.getAttribute('index'); ? ? ? ? ? ? num = index; ? ? ? ? ? ? circle = index; ? ? ? ? ? ? animate(ul, -index * focusWidth); ? ? ? ? }) ? ? } ? ? ol.children[0].className = 'current'; ? ? var first = ul.children[0].cloneNode(true); ? ? ul.appendChild(first); ? ? var num = 0; ? ? var circle = 0; ? ? var flag = true; ? ? //點(diǎn)擊'>'進(jìn)行圖片向右切換 ? ? arrow_r.addEventListener('click', function() { ? ? ? ? if (num == ul.children.length - 1) { ? ? ? ? ? ? ul.style.left = 0; ? ? ? ? ? ? num = 0; ? ? ? ? } ? ? ? ? num++; ? ? ? ? animate(ul, -num * focusWidth); ? ? ? ? circle++; ? ? ? ? if (circle == ol.children.length) { ? ? ? ? ? ? circle = 0; ? ? ? ? } ? ? ? ? circleChange(); ? ? }); ? ? //點(diǎn)擊'<'進(jìn)行圖片向左切換 ? ? arrow_l.addEventListener('click', function() { ? ? ? ? if (num == 0) { ? ? ? ? ? ? num = ul.children.length - 1; ? ? ? ? ? ? ul.style.left = -num * focusWidth + 'px'; ? ? ? ? } ? ? ? ? num--; ? ? ? ? animate(ul, -num * focusWidth); ? ? ? ? circle--; ? ? ? ? circle = circle < 0 ? ol.children.length - 1 : circle; ? ? ? ? circleChange() ? ? }); ? ? //清除樣式函數(shù),排他思想 ? ? function circleChange() { ? ? ? ? for (var i = 0; i < ol.children.length; i++) { ? ? ? ? ? ? ol.children[i].className = ''; ? ? ? ? } ? ? ? ? ol.children[circle].className = 'current'; ? ? } ? ? //定時(shí)器+click()實(shí)現(xiàn)自動(dòng)播放 ? ? var timer = setInterval(function() { ? ? ? ? arrow_r.click(); ? ? }, 2000) ? })
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 原生js實(shí)現(xiàn)輪播圖的示例代碼
- js實(shí)現(xiàn)輪播圖的完整代碼
- js實(shí)現(xiàn)支持手機(jī)滑動(dòng)切換的輪播圖片效果實(shí)例
- JS輪播圖實(shí)現(xiàn)簡單代碼
- js實(shí)現(xiàn)點(diǎn)擊左右按鈕輪播圖片效果實(shí)例
- 使用html+js+css 實(shí)現(xiàn)頁面輪播圖效果(實(shí)例講解)
- JS實(shí)現(xiàn)自動(dòng)輪播圖效果(自適應(yīng)屏幕寬度+手機(jī)觸屏滑動(dòng))
- 原生js實(shí)現(xiàn)無限循環(huán)輪播圖效果
- JS實(shí)現(xiàn)左右無縫輪播圖代碼
- js實(shí)現(xiàn)從左向右滑動(dòng)式輪播圖效果
相關(guān)文章
JavaScript事件Event對象詳解(屬性、方法、自定義事件)
Event對象代表事件的狀態(tài),比如事件在其中發(fā)生的元素、鍵盤按鍵的狀態(tài)、鼠標(biāo)的位置、鼠標(biāo)按鈕的狀態(tài),這篇文章主要給大家介紹了關(guān)于JavaScript事件Event對象(屬性、方法、自定義事件)的相關(guān)資料,需要的朋友可以參考下2024-01-01javascript實(shí)現(xiàn)全角轉(zhuǎn)半角的方法
這篇文章主要介紹了javascript實(shí)現(xiàn)全角轉(zhuǎn)半角的方法,涉及JavaScript字符串遍歷與編碼轉(zhuǎn)換的相關(guān)技巧,需要的朋友可以參考下2016-01-01JS生成不重復(fù)隨機(jī)數(shù)組的函數(shù)代碼
這篇文章主要介紹了JS生成不重復(fù)隨機(jī)數(shù)組的函數(shù)代碼,需要的朋友可以參考下2014-06-06uniapp開發(fā)小程序的開發(fā)規(guī)范總結(jié)
uni-app 是一個(gè)使用 vue.js 開發(fā)跨平臺(tái)應(yīng)用的前端框架,下面這篇文章主要給大家介紹了關(guān)于uniapp開發(fā)小程序的開發(fā)規(guī)范,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07