jQuery輪播圖功能實(shí)現(xiàn)方法
本文實(shí)例為大家分享了jQuery輪播圖功能的實(shí)現(xiàn)代碼,供大家參考,具體內(nèi)容如下
jQuery輪播(無(wú)animation)
html布局
<!-- 整個(gè)輪播區(qū)域 --> <div class="container"> <!-- 輪播圖 --> <ul class="items" style="left:-200px"> <!-- 實(shí)際上只輪播5張圖,將實(shí)際上的第一張放在最后一張,實(shí)際上的最后一張放在第一張,障眼法 --> <li>5</li> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> <li>1</li> </ul> <!-- 左右翻頁(yè)按鈕 --> <span class="left"><</span> <span class="right">></span> <!-- 圓點(diǎn) --> <ul class="points"> <li class="current"></li> <li></li> <li></li> <li></li> <li></li> </ul> </div>
CSS
<style> /* 輪播區(qū)域 */ .container { width: 200px; height: 100px; margin: 100px auto; overflow: hidden; position: relative; } ul { position: absolute; list-style-type: none; width: 1400px; padding: 0; margin: 0; } /* 輪播圖片 */ .items li { width: 200px; height: 100px; margin: 0; padding: 0; float: left; background-color: pink; text-align: center; } /* 左右翻頁(yè)span */ span { display: block; width: 20px; height: 30px; background-color: rgba(70, 130, 180, 0.3); position: absolute; top: 50%; transform: translateY(-50%); line-height: 30px; } span:hover { cursor: pointer; } .left { left: 0; } .right { right: 0; } /* 圓點(diǎn) */ .points { width: 45px; margin: 0; padding: 0; bottom: 3px; left: 50%; transform: translateX(-50%); } .points li { float: left; width: 7px; height: 7px; border-radius: 50%; margin: 1px; background-color: rgba(0, 0, 0, 0.5); } .points li:hover { background-color: rgba(255, 250, 205, 1); } .points .current { background-color: rgba(255, 250, 205, 1); } </style>
jQuery
<script type="text/javascript"> // 1. 點(diǎn)擊按鈕左右切換頁(yè)面------輪播+動(dòng)畫(huà),鼠標(biāo)進(jìn)入,播放暫停,鼠標(biāo)移出,播放繼續(xù) // 2. 頁(yè)面每隔3秒自動(dòng)切換 // 3. 圓點(diǎn)跟著一起切換樣式 // 左切換 let $left = $('.left') // 右切換 let $right = $('.right') // 圖片li let $list = $('.items') let $items = $list.children() // 大容器 let $container = $('.container') // 圓點(diǎn) let $points = $('.points').children() const length = $points.length // 設(shè)置的總偏移量=li.width const itemWidth = 200 // 設(shè)置每次動(dòng)畫(huà)時(shí)間ms const time = 50 // 移動(dòng)次數(shù) const n = 20 // list最大偏移量-(length+1)*li.width const long = -(length + 1) * itemWidth // 規(guī)定是否在翻頁(yè),默認(rèn)沒(méi)有在翻頁(yè)------>解決翻頁(yè)時(shí)點(diǎn)擊翻頁(yè),出現(xiàn)位置偏差 let moveFlag = false // 定時(shí)器移動(dòng)的時(shí)間 const TIME = 3000 // 向左切換 $left.click(function() { changeItem(true) }) // 向右切換 $right.click(function() { changeItem(false) }) // 自動(dòng)切換 let timer = setInterval(function() { changeItem(false) }, TIME) // 鼠標(biāo)進(jìn)入,播放暫停,鼠標(biāo)移出,播放繼續(xù) $container.hover(function() { clearInterval(timer) }, function() { timer = setInterval(function() { changeItem(false) }, TIME) }) //點(diǎn)擊圓點(diǎn)翻頁(yè) $points.click(function() { //獲取當(dāng)前點(diǎn)擊元素的index let index = $(this).index() // 跳轉(zhuǎn)到對(duì)應(yīng)的index圖 changeItem(index) // 圓點(diǎn)其他兄弟樣式取消 $points.eq(index).addClass('current').siblings().removeClass('current') }) // 左右切換函數(shù)封裝 function changeItem(flag) { // 如果當(dāng)前在翻頁(yè),直接返回 if (moveFlag) { return } // 如果當(dāng)前沒(méi)在翻頁(yè),執(zhí)行代碼,且將moveFlag改為true,標(biāo)識(shí)正在翻頁(yè) moveFlag = true // offset是偏移量 let offset = 0; // let currentLeft = parseInt($list.position().left) // 如果傳入的是boolean型,表示是左右平滑翻頁(yè) // 如果是數(shù)字型,就表示是點(diǎn)擊圓點(diǎn)翻頁(yè) if (typeof flag == 'boolean') { // 判斷是左翻還是右翻,設(shè)置相應(yīng)的位移 offset = flag ? itemWidth : -itemWidth } else { // 點(diǎn)擊圓點(diǎn)翻頁(yè) // -(flag + 1)*itemWidth是目標(biāo)位移,currentLeft是當(dāng)前距離 offset = -(flag + 1) * itemWidth - currentLeft } // 用來(lái)累計(jì)執(zhí)行的次數(shù) let i = 0 /* 動(dòng)畫(huà)效果切換:按照次數(shù)來(lái)計(jì)算 總距離=總偏移量=offset 每次時(shí)間設(shè)置time */ // 每次移動(dòng)的距離 itemOffset let itemOffset = offset / n // 獲取現(xiàn)在的left // 定時(shí)器函數(shù) const timer = setInterval(function() { // 每執(zhí)行一次就加一,直到i===n,表示次數(shù)足夠就停止定時(shí)器 i++ currentLeft += itemOffset // 設(shè)置left值 // 必須先設(shè)置值,再去判斷 $list.css('left', currentLeft) if (i === n) { // 位移足夠,清除定時(shí)器 clearInterval(timer) // 翻頁(yè)結(jié)束 moveFlag = false // 圓點(diǎn)隨之改變 $points.eq(Math.abs(currentLeft / itemWidth) - 1).addClass('current').siblings().removeClass('current') // 當(dāng)定位到最后一張時(shí),換到第二張去,視覺(jué)是輪播 if (currentLeft == long) { $list.css('left', -itemWidth) // 圓點(diǎn)設(shè)置到實(shí)際的第一張圖上 $points.eq(0).addClass('current').siblings().removeClass('current') // 如果已經(jīng)到達(dá)最后一張圖的墊底圖,就返回實(shí)際意義上的第一張圖 } else if (currentLeft == 0) { $list.css('left', -length * itemWidth) // 圓點(diǎn)設(shè)置到實(shí)際的最后一張圖上 $points.eq(length - 1).addClass('current').siblings().removeClass('current') } } }, time) } </script>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- jquery 實(shí)現(xiàn)輪播圖詳解及實(shí)例代碼
- 利用jquery寫(xiě)的左右輪播圖特效
- jquery實(shí)現(xiàn)左右無(wú)縫輪播圖
- jQuery插件slides實(shí)現(xiàn)無(wú)縫輪播圖特效
- jQuery實(shí)現(xiàn)輪播圖及其原理詳解
- jQuery自適應(yīng)輪播圖插件Swiper用法示例
- jquery實(shí)現(xiàn)左右滑動(dòng)式輪播圖
- JQuery和html+css實(shí)現(xiàn)帶小圓點(diǎn)和左右按鈕的輪播圖實(shí)例
- jquery實(shí)現(xiàn)的簡(jiǎn)單輪播圖功能【適合新手】
- 基于jQuery實(shí)現(xiàn)淡入淡出效果輪播圖
相關(guān)文章
jquery實(shí)現(xiàn)戶(hù)籍地選擇下拉框
這篇文章主要為大家詳細(xì)介紹了jquery實(shí)現(xiàn)戶(hù)籍地選擇下拉框,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06兩個(gè)多選select(multiple左右)添加、刪除選項(xiàng)和取值實(shí)例
這篇文章主要介紹了兩個(gè)多選select(multiple左右)添加、刪除選項(xiàng)和取值實(shí)例,使用jquery實(shí)現(xiàn),需要的朋友可以參考下2014-05-05

jQuery控制網(wǎng)頁(yè)打印指定區(qū)域的方法

jQuery添加/改變/移除CSS類(lèi)及判斷是否已經(jīng)存在CSS

jQuery開(kāi)發(fā)者都需要知道的5個(gè)小技巧

jQuery實(shí)現(xiàn)MSN中文網(wǎng)滑動(dòng)Tab菜單效果代碼

jQuery實(shí)現(xiàn)頁(yè)面倒計(jì)時(shí)并刷新效果

jQuery中toggleClass()方法用法實(shí)例