純javaScript、jQuery實(shí)現(xiàn)個(gè)性化圖片輪播【推薦】
純javaScript實(shí)現(xiàn)個(gè)性化圖片輪播
輪播原理說明<如上圖所示>:
1. 畫布部分(可視區(qū)域)屬性說明:overflow:hidden使得超出畫布部分隱藏或說不可見。position:relative 會(huì)導(dǎo)致自身位置的相對(duì)變化,而不會(huì)影響其他元素的位置、大小的變化。使得使用了position:absolute 元素相對(duì)于畫布位置進(jìn)行定位;
absolute元素脫離了文檔結(jié)構(gòu),產(chǎn)生破壞性,導(dǎo)致父元素坍塌,float元素也會(huì)脫離文檔結(jié)構(gòu),absolute元素會(huì)懸浮在頁面上方,遮擋其他部分顯示,這點(diǎn)和PhotoShop圖層相似,所以要使用z-index控制出現(xiàn)順序
2.輪播注意點(diǎn):左右無限滾動(dòng)
prev-button 第一張圖片的前一張是最后一張圖片,
next-button 最后一張圖片的下一張圖片是第一張,
prev-button、next-button位置的偏移是通過設(shè)置prev-img-container、next-img-container的left<相對(duì)于畫布>屬性值
click-select-show-button區(qū)域,點(diǎn)擊該區(qū)域小圓圈是通過上一次圖片的所在index,當(dāng)前點(diǎn)擊myIndex, 計(jì)算公式:(myIndex-index)*(-圖片的寬度width)
3.動(dòng)畫過渡注意點(diǎn):點(diǎn)擊prev-button、next-button、click-select-show-button小圓圈,判定當(dāng)前是否處于動(dòng)畫狀態(tài)中
4.定時(shí)器setTimeout()、clearTimeout
<實(shí)現(xiàn)效果圖>
Css樣式
/**CSS-style**/ /**畫布大小*/ #container { margin:0 auto; width: 600px; height: 400px; overflow: hidden;/*超出畫布部分隱藏*/ position: relative;/*相對(duì)定位*/ cursor: pointer; } /**圖片容器*/ #list { width: 4200px; height: 400px; position: absolute; z-index:1; } #list img { float: left; } /**輪播選中按鈕樣式*/ #button { position: absolute; bottom: 25px; left: 175px; width: 250px; z-index: 2; } #button ul li { list-style: none; width: 15px; border-radius: 50%; padding: 7.5px; height: 15px; margin-right: 10px; background: green; float: left; font:15px/15px "microsoft yahei"; text-align: center; font-weight: bold; color: white; cursor: pointer; } #button ul li.chos { background: orange; } #container:hover .arrow{ display: block; } #pre { left: 20px; } #next { right: 20px; } /**pre next定位*/ .arrow { position: absolute; width: 40px; height: 40px; background: black; z-index: 3; top: 180px; text-decoration: none; text-align: center; line-height: 40px; font-size: 40px; color: white; opacity: 0.3; filter: alpha(opacity=0.3); display: none; } /**pre next按鈕透明度*/ #container a:hover { opacity: 0.7; filter: alpha(opacity=0.7); }
html代碼
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>純javaScript實(shí)現(xiàn)個(gè)性化圖片輪播</title> <link rel="stylesheet" type="text/css" href="styles/main.css"> <script type="text/javascript" src="scripts/scroImg.js"></script> </head> <body> <div id="container"> <div id="list" style="left:-600px"> <img src="images/5.jpg"> <img src="images/1.jpg"> <img src="images/2.jpg"> <img src="images/3.jpg"> <img src="images/4.jpg"> <img src="images/5.jpg"> <img src="images/1.jpg"> </div> <div id="button"> <ul> <li index='1'>1</li> <li index='2'>2</li> <li index='3'>3</li> <li index='4'>4</li> <li index='5'>5</li> </ul> </div> <a href="#" class="arrow" id="prev"><</a> <a href="#" class="arrow" id="next">></a> </div> </body> </html>
一、javaScript實(shí)現(xiàn)圖片輪播
window.onload=function(){ var container=document.getElementById('container'); var list=document.getElementById('list'); var buttons=document.getElementById('button').getElementsByTagName('li'); var prev=document.getElementById('prev'); var next=document.getElementById('next'); var index=1; var interval=1000; var timer=null; var animated=false; //next next.onclick=function(){ if (!animated) { animate(-600); }; index+=1; if (index>5) { index=1; }; showButton(); console.info('next'+index); } //prev prev.onclick=function(){ if(!animated){ animate(600); } index-=1; if(index<1){ index=5; } showButton(); console.info('prev'+index); } //animate function animate(offset){ animated=true; var left=parseInt(list.style.left)+offset; var animateTime=600;//位移總時(shí)間 var interval=10;//時(shí)間間隔 var speed=offset/(animateTime/interval);//每次位移量 var go=function(){//animate內(nèi)部函數(shù) if ((speed<0 && parseInt(list.style.left)>left) || (speed>0 && parseInt(list.style.left)<left)) {//是否位移 list.style.left=parseInt(list.style.left)+speed+'px'; setTimeout(go,interval) }else{ list.style.left=left+'px'; if (left<-3000) { //最后一張后面 list.style.left=-600+'px'; //顯示前一張 }; if(left>-600){//第一張最前面 list.style.left=-3000+'px';//顯示最后一張 } animated=false; }; } go(); } //chos function showButton(){ for (var i = 0; i < buttons.length; i++) { buttons[i].className=''; }; buttons[index-1].className='chos'; } //buttons-click for (var i = 0; i < buttons.length; i++) { buttons[i].onclick=function(){ if(this.className=='chos'){ return; } var myIndex=parseInt(this.getAttribute('index')); var offset=(myIndex-index)*-600; //偏移量 animate(offset); index=myIndex;//set Index showButton(); } }; function play(){ timer=setTimeout(function(){ next.click(); play(); },interval) } function stop(){ clearInterval(timer); } play(); container.onmouseover=function(){ stop(); } container.onmouseout=function(){ play(); } }
二、jQuery實(shí)現(xiàn)圖片輪播
$(function () { var container = $('#container'); var list = $('#list'); var buttons = $('#container').find('li'); var prev = $('#pre'); var next = $('#next'); var index = 1; var len = 5; var interval = 3000; var timer; function animate (offset) { var left = parseInt(list.css('left')) + offset; if (offset>0) { offset = '+=' + offset; } else { offset = '-=' + Math.abs(offset); } list.animate({'left': offset}, 300, function () { if(left > -200){ list.css('left', -600 * len); } if(left < (-600 * len)) { list.css('left', -600); } }); } function showButton() { buttons.eq(index-1).addClass('chos').siblings().removeClass('chos'); } function play() { timer = setTimeout(function () { next.trigger('click'); play(); }, interval); } function stop() { clearTimeout(timer); } next.bind('click', function () { if (list.is(':animated')) { return; } if (index == 5) { index = 1; } else { index += 1; } animate(-600); showButton(); }); prev.bind('click', function () { if (list.is(':animated')) { return; } if (index == 1) { index = 5; } else { index -= 1; } animate(600); showButton(); }); buttons.each(function () { $(this).bind('click', function () { if (list.is(':animated') || $(this).attr('class')=='chos') { return; } var myIndex = parseInt($(this).attr('index')); var offset = -600 * (myIndex - index); animate(offset); index = myIndex; showButton(); }) }); container.hover(stop, play); play(); });
源碼下載 http://pan.baidu.com/s/1kVfnGF1
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時(shí)也希望多多支持腳本之家!
- 原生js和jquery實(shí)現(xiàn)圖片輪播淡入淡出效果
- 基于jQuery實(shí)現(xiàn)淡入淡出效果輪播圖
- 原生js和jQuery實(shí)現(xiàn)淡入淡出輪播效果
- jQuery實(shí)現(xiàn)圖片簡(jiǎn)單輪播功能示例
- jQuery實(shí)現(xiàn)的簡(jiǎn)單圖片輪播效果完整示例
- 使用JQuery實(shí)現(xiàn)圖片輪播效果的實(shí)例(推薦)
- jQuery簡(jiǎn)單自定義圖片輪播插件及用法示例
- jQuery實(shí)現(xiàn)的圖片輪播效果完整示例
- jQuery的圖片輪播插件PgwSlideshow使用詳解
- jQuery實(shí)現(xiàn)圖片輪播效果代碼(基于jquery.pack.js插件)
- jQuery實(shí)現(xiàn)的淡入淡出圖片輪播效果示例
相關(guān)文章
javascript設(shè)計(jì)模式 – 觀察者模式原理與用法實(shí)例分析
這篇文章主要介紹了javascript設(shè)計(jì)模式 – 觀察者模式,結(jié)合實(shí)例形式分析了javascript觀察者模式相關(guān)概念、原理、用法及操作注意事項(xiàng),需要的朋友可以參考下2020-04-04javascript獲取以及設(shè)置光標(biāo)位置
本文介紹了javascript獲取以及設(shè)置光標(biāo)位置的方法,具有很好的參考價(jià)值,下面跟著小編一起來看下吧2017-02-02js獲取數(shù)組對(duì)象中的全部key和value值
本文主要介紹了js獲取數(shù)組對(duì)象中的全部key和value值,主要使用JavaScript的?map()?函數(shù)和?values()?迭代器來實(shí)現(xiàn)取出數(shù)組對(duì)象的所有key值和value值,感興趣的可以了解下2024-01-01js實(shí)現(xiàn)的仿Photoshop鼠標(biāo)滾輪控制輸入框取值(修正兼容Chrome)
一直很想做這個(gè)效果,原理是監(jiān)聽鼠標(biāo)滾輪事件;可將此效果繼續(xù)發(fā)散到其他應(yīng)用上,如圖片縮放,頁面縮放等。2010-02-02JavaScript實(shí)現(xiàn)打字效果的方法
這篇文章主要介紹了JavaScript實(shí)現(xiàn)打字效果的方法,可實(shí)現(xiàn)文字陸續(xù)出現(xiàn)的打字效果,涉及javascript時(shí)間函數(shù)及頁面元素獲取的相關(guān)技巧,需要的朋友可以參考下2015-07-07JS實(shí)現(xiàn)的簡(jiǎn)單鼠標(biāo)跟隨DiV層效果完整實(shí)例
這篇文章主要介紹了JS實(shí)現(xiàn)的簡(jiǎn)單鼠標(biāo)跟隨DiV層效果,涉及JavaScript基于時(shí)間函數(shù)動(dòng)態(tài)操作頁面元素屬性的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10淺談webpack構(gòu)建工具配置和常用插件總結(jié)
這篇文章主要介紹了淺談webpack構(gòu)建工具配置和常用插件總結(jié),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2020-05-05HTML中用JS實(shí)現(xiàn)旋轉(zhuǎn)的圣誕樹
這篇文章介紹了HTML中用JS實(shí)現(xiàn)旋轉(zhuǎn)的圣誕樹,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-12-12