js實(shí)現(xiàn)圖片輪播效果
本文實(shí)例講解了js實(shí)現(xiàn)圖片輪播效果代碼,分享給大家供大家參考,具體內(nèi)容如下
運(yùn)行代碼如下
具體代碼如下
插件是基于jQuery寫的,主要實(shí)現(xiàn)的功能:自動(dòng)播放、鼠標(biāo)懸停、左右箭頭控制+禁止點(diǎn)擊
CSS樣式:
<style> .cooperation-box { position: relative; height: 91px; border-bottom: 1px solid #E0DED9; overflow: hidden; } .cooperation { position: relative; left: 0; height: 50px; padding: 20px 0; } .cooperation li { float: left; width: 205px; text-align: center; } .cooperation li a { display: block; } .cooperation li img { height: 100%; } .cooperation-box>a { display: block; position: absolute; top: 0; z-index: 9; width: 22px; height: 100%; background: #f5f5f5; font-family: '宋體'; font-size: 18px; color: #aaa; font-weight: bold; text-align: center; line-height: 91px; } .cooperation-box>a:hover { background: #e5e5e5; } .cooperation-box .prev { left: 0; border-right: 1px solid #E0DED9; } .cooperation-box .next { right: 0; border-left: 1px solid #E0DED9; } .cooperation-box .prev.disabled, .cooperation-box .next.disabled { background: #fbfbfb; color: #ddd; } .cooperation-box .prev.disabled:hover, .cooperation-box .next.disabled:hover { background: #fbfbfb; } </style>
HTML布局( a標(biāo)簽最好加個(gè)title屬性 ):
<div class="cooperation-box"> <a class="prev" href="javascript:;"><</a> <a class="next" href="javascript:;">></a> <ul class="cooperation"> <li><a href="javascript:;" target="_blank"><img src="images/img-demo3.jpg" alt=""></a></li> <li><a href="javascript:;" target="_blank"><img src="images/img-demo3.jpg" alt=""></a></li> <li><a href="javascript:;" target="_blank"><img src="images/img-demo3.jpg" alt=""></a></li> <li><a href="javascript:;" target="_blank"><img src="images/img-demo3.jpg" alt=""></a></li> <li><a href="javascript:;" target="_blank"><img src="images/img-demo3.jpg" alt=""></a></li> <li><a href="javascript:;" target="_blank"><img src="images/img-demo3.jpg" alt=""></a></li> <li><a href="javascript:;" target="_blank"><img src="images/img-demo3.jpg" alt=""></a></li> <li><a href="javascript:;" target="_blank"><img src="images/img-demo3.jpg" alt=""></a></li> </ul> </div>
JS腳本插件:
<script> $.extend({ /* 圖片輪播效果 效果: 1、自動(dòng)滾動(dòng) 2、鼠標(biāo)懸停 3、左右控制+禁止點(diǎn)擊 調(diào)用:$.scroll({box: '父容器', scrollbox: 'ul', time: 1500}); */ scroll: function(options) { // 默認(rèn)配置 var defaults = { box: '.cooperation-box', // 父容器 scrollbox: '.cooperation', // ul容器 time: 1500 // 切換時(shí)間 }; // 擴(kuò)展配置 var conf = $.extend({}, defaults, options); // 獲取li的個(gè)數(shù) var liSize = $(conf.box).find('li').size(); // 獲取li的寬度 var liWidth = $(conf.box).find('li:first').width(); // 定義ul的寬度 $(conf.scrollbox).width(liWidth*liSize); // 右箭頭初始化(不可點(diǎn)) $(conf.box).find('.next').addClass('disabled'); // 定義一個(gè)全局變量index索引變量 var index = 0; // 切換函數(shù) function switchFunc() { index++; if(index > liSize-5) { // 必須有5個(gè)顯示在上面 index=liSize-5; // 把滾動(dòng)過的添加到后面,初始left值為0 index值為0 var scrolledLi = $(conf.box).find('li:lt('+index+')'); $(conf.scrollbox).append(scrolledLi); $(conf.scrollbox).css('left', 0); index = 0; } $(conf.scrollbox).stop(true, true).animate( {'left': -liWidth*index}, 500, function() { $(conf.box).find('.next').removeClass('disabled'); } ); } // 自動(dòng)播放 var autoPlay = setInterval(function() {switchFunc();}, conf.time); // 鼠標(biāo)浮上暫停 $(conf.box).mouseover(function() { clearInterval(autoPlay); }); // 鼠標(biāo)離開繼續(xù) $(conf.box).mouseout(function() { autoPlay = setInterval(function() {switchFunc();}, conf.time); }); // 點(diǎn)擊左箭頭 $(conf.box).find('.prev').click(function() { index++; if(index >= liSize-5) { index=liSize-5; $(this).addClass('disabled'); } $(conf.scrollbox).stop(true, true).animate( {'left': -liWidth*index}, 500, function() { $(conf.box).find('.next').removeClass('disabled'); } ); }); // 點(diǎn)擊右箭頭 $(conf.box).find('.next').click(function() { index--; if(index <= 0) { index = 0; $(this).addClass('disabled'); } $(conf.scrollbox).stop(true, true).animate( {'left': -liWidth*index}, 500, function() { $(conf.box).find('.prev').removeClass('disabled'); } ); }); } }); </script>
頁面調(diào)用:
<script> $(function() { $.scroll({time: 1500}); }); </script>
希望本文所述對(duì)大家學(xué)習(xí)javascript程序設(shè)計(jì)有所幫助。
相關(guān)文章
JavaScript實(shí)現(xiàn)tab欄切換效果
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)tab欄切換效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-03-03基于javascript實(shí)現(xiàn)最簡單選項(xiàng)卡切換
這篇文章主要為大家詳細(xì)介紹了基于javascript實(shí)現(xiàn)最簡單選項(xiàng)卡切換,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02JS實(shí)現(xiàn)拖動(dòng)滑塊驗(yàn)證
這篇文章主要為大家詳細(xì)介紹了JS實(shí)現(xiàn)拖動(dòng)滑塊驗(yàn)證,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03js解析xml字符串和xml文檔實(shí)現(xiàn)原理及代碼(針對(duì)ie與火狐)
分別針對(duì)ie和火狐分別作了對(duì)xml文檔和xml字符串的解析,考慮到了瀏覽器的兼容性,至于在ajax環(huán)境下解析xml,其實(shí)原理是一樣的,只不過放在了ajax里,還是要對(duì)返回的xml進(jìn)行解析,感興趣的朋友可以了解下,或許對(duì)你學(xué)習(xí)js解析xml有所幫助2013-02-02JavaScript實(shí)現(xiàn)一個(gè)多少秒后自動(dòng)跳轉(zhuǎn)的頁面(案例代碼)
最近遇到這樣一個(gè)需求是用js簡單實(shí)現(xiàn)一個(gè)多少秒后自動(dòng)跳轉(zhuǎn)的頁面,實(shí)現(xiàn)代碼非常簡單,對(duì)js自動(dòng)跳轉(zhuǎn)頁面相關(guān)知識(shí)感興趣的朋友一起看看吧2023-01-01JavaScript簡單實(shí)現(xiàn)的仿微博留言功能示例
這篇文章主要介紹了JavaScript簡單實(shí)現(xiàn)的仿微博留言功能,涉及javascript頁面元素屬性動(dòng)態(tài)操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-01-01Canvas實(shí)現(xiàn)數(shù)字雨和放大鏡效果的代碼示例
這篇文章主要介紹了如何Canvas實(shí)現(xiàn)數(shù)字雨和放大鏡效果,文中有完整的代碼示例,文章通過代碼介紹的非常清楚,感興趣的小伙伴跟著小編一起來看看吧2023-07-07Javascript下的urlencode編碼解碼方法附decodeURIComponent
而本文,就大概說說如何在js中通過系統(tǒng)自帶的函數(shù)去解決這個(gè)問題。2010-04-04