jQuery輪播圖功能實(shí)現(xiàn)方法
更新時間:2021年05月18日 09:04:04 作者:睡個好覺_
這篇文章主要為大家詳細(xì)介紹了jQuery輪播圖功能實(shí)現(xiàn)方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了jQuery輪播圖功能的實(shí)現(xiàn)代碼,供大家參考,具體內(nèi)容如下
jQuery輪播(無animation)
html布局
<!-- 整個輪播區(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> <!-- 左右翻頁按鈕 --> <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; } /* 左右翻頁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)擊按鈕左右切換頁面------輪播+動畫,鼠標(biāo)進(jìn)入,播放暫停,鼠標(biāo)移出,播放繼續(xù) // 2. 頁面每隔3秒自動切換 // 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è)置每次動畫時間ms const time = 50 // 移動次數(shù) const n = 20 // list最大偏移量-(length+1)*li.width const long = -(length + 1) * itemWidth // 規(guī)定是否在翻頁,默認(rèn)沒有在翻頁------>解決翻頁時點(diǎn)擊翻頁,出現(xiàn)位置偏差 let moveFlag = false // 定時器移動的時間 const TIME = 3000 // 向左切換 $left.click(function() { changeItem(true) }) // 向右切換 $right.click(function() { changeItem(false) }) // 自動切換 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)翻頁 $points.click(function() { //獲取當(dāng)前點(diǎn)擊元素的index let index = $(this).index() // 跳轉(zhuǎn)到對應(yīng)的index圖 changeItem(index) // 圓點(diǎn)其他兄弟樣式取消 $points.eq(index).addClass('current').siblings().removeClass('current') }) // 左右切換函數(shù)封裝 function changeItem(flag) { // 如果當(dāng)前在翻頁,直接返回 if (moveFlag) { return } // 如果當(dāng)前沒在翻頁,執(zhí)行代碼,且將moveFlag改為true,標(biāo)識正在翻頁 moveFlag = true // offset是偏移量 let offset = 0; // let currentLeft = parseInt($list.position().left) // 如果傳入的是boolean型,表示是左右平滑翻頁 // 如果是數(shù)字型,就表示是點(diǎn)擊圓點(diǎn)翻頁 if (typeof flag == 'boolean') { // 判斷是左翻還是右翻,設(shè)置相應(yīng)的位移 offset = flag ? itemWidth : -itemWidth } else { // 點(diǎn)擊圓點(diǎn)翻頁 // -(flag + 1)*itemWidth是目標(biāo)位移,currentLeft是當(dāng)前距離 offset = -(flag + 1) * itemWidth - currentLeft } // 用來累計執(zhí)行的次數(shù) let i = 0 /* 動畫效果切換:按照次數(shù)來計算 總距離=總偏移量=offset 每次時間設(shè)置time */ // 每次移動的距離 itemOffset let itemOffset = offset / n // 獲取現(xiàn)在的left // 定時器函數(shù) const timer = setInterval(function() { // 每執(zhí)行一次就加一,直到i===n,表示次數(shù)足夠就停止定時器 i++ currentLeft += itemOffset // 設(shè)置left值 // 必須先設(shè)置值,再去判斷 $list.css('left', currentLeft) if (i === n) { // 位移足夠,清除定時器 clearInterval(timer) // 翻頁結(jié)束 moveFlag = false // 圓點(diǎn)隨之改變 $points.eq(Math.abs(currentLeft / itemWidth) - 1).addClass('current').siblings().removeClass('current') // 當(dāng)定位到最后一張時,換到第二張去,視覺是輪播 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>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- jquery 實(shí)現(xiàn)輪播圖詳解及實(shí)例代碼
- 利用jquery寫的左右輪播圖特效
- jquery實(shí)現(xiàn)左右無縫輪播圖
- jQuery插件slides實(shí)現(xiàn)無縫輪播圖特效
- jQuery實(shí)現(xiàn)輪播圖及其原理詳解
- jQuery自適應(yīng)輪播圖插件Swiper用法示例
- jquery實(shí)現(xiàn)左右滑動式輪播圖
- JQuery和html+css實(shí)現(xiàn)帶小圓點(diǎn)和左右按鈕的輪播圖實(shí)例
- jquery實(shí)現(xiàn)的簡單輪播圖功能【適合新手】
- 基于jQuery實(shí)現(xiàn)淡入淡出效果輪播圖
相關(guān)文章
兩個多選select(multiple左右)添加、刪除選項(xiàng)和取值實(shí)例
這篇文章主要介紹了兩個多選select(multiple左右)添加、刪除選項(xiàng)和取值實(shí)例,使用jquery實(shí)現(xiàn),需要的朋友可以參考下2014-05-05

jQuery添加/改變/移除CSS類及判斷是否已經(jīng)存在CSS
正如標(biāo)題所言會用到removeClass移除CSS類、addClass添加CSS類、toggleClass添加或者移除CSS類,hasClass判斷是否已經(jīng)存在CSS
2014-08-08 
jQuery實(shí)現(xiàn)MSN中文網(wǎng)滑動Tab菜單效果代碼
這篇文章主要介紹了jQuery實(shí)現(xiàn)MSN中文網(wǎng)滑動Tab菜單效果代碼,基于jQuery鼠標(biāo)事件實(shí)現(xiàn)頁面元素屬性動態(tài)切換的功能,具有一定參考借鑒價值,需要的朋友可以參考下
2015-09-09 
jQuery中toggleClass()方法用法實(shí)例
這篇文章主要介紹了jQuery中toggleClass()方法用法,實(shí)例分析了toggleClass()方法的功能、定義及對添加或移除匹配元素的一個或多個類進(jìn)行切換的使用技巧,需要的朋友可以參考下
2015-01-01