您好,登錄后才能下訂單哦!
本文實(shí)例為大家分享了js實(shí)現(xiàn)輪播圖效果展示的具體代碼,供大家參考,具體內(nèi)容如下
html結(jié)構(gòu)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style> *{ margin:0; padding:0; font-size:14px; -webkit-user-select:none; } ul,li{ list-style:none; } img{ display:block; border:none; } a{ display:block; color:#000; text-decoration: none; } a:hover,a:active,a:visited,a:target{ display:block; color:#000; text-decoration: none; } .banner{ position:relative; margin:0 auto; width:1000px; height:300px; overflow:hidden; } .banner .inner{ position:absolute; top:0; left:0; height:300px; width:1000px;/*在JS數(shù)據(jù)綁定結(jié)束后根據(jù)請(qǐng)求數(shù)據(jù)的多少來(lái)動(dòng)態(tài)調(diào)整寬度*/ } .banner .inner div{ float:left; width:1000px; height:300px; background:url('img/default.gif') no-repeat center center #e1e1e1; } .banner .inner img{ display:none; width:100%; height:100%; opacity:0; filter:alpha(opacity=0); } .banner .bannerTip{ height:18px; position:absolute; right:20px; bottom:20px; } .banner .bannerTip li{ float:left; margin-left:10px; width:18px; height:18px; border-radius:50%; background:lightblue; cursor:pointer; } .banner .bannerTip li.bg{ background:red; } .banner a{ display:none; position:absolute; top:50%; margin-top:-22.5px; width:30px; height:45px; background-image:url("img/pre.png"); background-repeat:no-repeat; opacity:0.5; filter:alpha(opacity=50); } .banner a:hover{ opacity:1; filter:alpha(opacity=100); } .banner a.bannerLeft{ left:20px; background-position:0 0; } .banner a.bannerRight{ right:20px; background-position:-45px 0; } </style> </head> <body> <div class='banner' id='banner'> <div class='inner'> <div><img src="img/banner1.jpg" alt=""></div> <div><img src="img/banner2.jpg" alt=""></div> <div><img src="img/banner3.jpg" alt=""></div> <div><img src="img/banner4.jpg" alt=""></div> </div> <ul class='bannerTip'> <li class='bg'></li> <li></li> <li></li> <li></li> </ul> <a href="javascript:;" class='bannerLeft'></a> <a href="javascript:;" class='bannerRight'></a> </div> </body> </html>
js
(function(){ var banner = document.getElementById('banner'); var bannerInner = utils.firstChild(banner); var bannerTip = utils.children(banner,'ul')[0]; var imgList = bannerInner.getElementsByTagName('img'); var oLis = bannerTip.getElementsByTagName('li'); var bannerLeft = utils.children(banner,'a')[0]; var bannerRight = utils.children(banner,'a')[1]; //實(shí)現(xiàn)數(shù)據(jù)綁定:Ajax請(qǐng)求數(shù)據(jù)、按照字符串拼接的方式綁定數(shù)據(jù) var jsonData = null,count = null ~function(){ var xhr = new XMLHttlRequest; xhr.open('get',"json/banner.txt?_="+Math.random(),false); xhr.onreadystatechange = function(){ if(xhr.readyState === 4 && /^2\d{2}$/.test(xhr.status)){ jsonData = utils.formatJSON(xhr.responseText); } } xhr.send(null); }() ~function(){ //綁定輪播圖區(qū)域的數(shù)據(jù) var str = ""; if(jsonData){ for(var i = 0,len = jsonData.length;i<len;i++){ str+='<div><img src="" trueImg="'+jsonData[i]['img']+'"></div>' } //為了實(shí)現(xiàn)無(wú)縫滾動(dòng),需要把第一張克隆一份放在末尾 str+= '<div><img src="" trueImg="'+jsonData[0]['img']+'"></div>' } bannerInner.innerHTML = str; count = jsonData.length+1; utils.css(bannerInner,"width",count*1000); //綁定焦點(diǎn)區(qū)域的數(shù)據(jù) str = ''; if(jsonData){ for(var i = 0,len = jsonData.length;i<len;i++){ i===0?str+='<li class="bg"></li>':str+='<li></li>'; } } bannerTip.innerHTML = str; }() //圖片延遲加載 function lazyImg(){ for(var i = 0,len = imgList.length;i<len;i++){ ~function(i){//這里使用閉包,來(lái)避免onload事件異步導(dǎo)致的只有最后一張圖片延遲加載的問(wèn)題 var curImg = imgList[i]; var oImg = new Image; oImg.src = curImg.getAttribute('trueImg'); oImg.onload = function(){ curImg.src = this.src; curImg.style.display = "block"; oImg = null; myAnimate(curImg,{opacity:1},300) } }(i) } } window.setTimeout(lazyImg,500); var step = 0;//記錄的是步長(zhǎng),(當(dāng)前是哪一張圖片,0是第一張圖片) //實(shí)現(xiàn)自動(dòng)輪播 var autoTimer = window.setInterval(autoMove,2000); function autoMove(){ if(step===count-1){ step =0; bannerInner.style.left = 0 } step++; myAnimate(bannerInner,{left:-step*1000},500) changeTip(); } //實(shí)現(xiàn)焦點(diǎn)對(duì)齊 function changeTip(){ var tempStep = step > oLis.length-1 ? 0 : step; for(var i = 0,len = oLis.length;i<len;i++){ var curLi = oLis[i]; i === tempStep ? utils.addClass(curLi,"bg") : utils.removeClass(curLi,"bg") } } //鼠標(biāo)滑過(guò)停止和開(kāi)啟輪播 banner.onmouseover = function(){ window.clearInterval(autoTimer); bannerLeft.style.display = bannerRight.style.display = 'block'; } banner.onmouseout = function(){ autoTimer = window.setInterval(autoTimer,2000); bannerLeft.style.display = bannerRight.style.display = 'none'; } //點(diǎn)擊焦點(diǎn)實(shí)現(xiàn)輪播圖的切換 ~function(){ for(var i = 0,len = oLis.length;i<len;i++){ var curLi = oLis[i]; curLi.index = i; curLi.onclick = function(){ step = this.index; changeTip(); myAnimate(bannerInner,{left:-step*1000},500) } } }() //實(shí)現(xiàn)左右切換 bannerRight.onclick = autoMove(); bannerLeft.onclick = function(){ if(step<=0){ step = count-1; utils.css(bannerInner,"left",-step*1000); } step--; autoMove(); } })()
綁定的數(shù)據(jù)
[ {"img":"img/banner1.jpg","desc":"第一張輪播圖"}, {"img":"img/banner2.jpg","desc":"第二張輪播圖"}, {"img":"img/banner3.jpg","desc":"第三張輪播圖"}, {"img":"img/banner4.jpg","desc":"第四張輪播圖"} ]
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。