轮播图 页面效果
页面代码 <!DOCTYPE html> <html > <head > <meta charset ="UTF-8" > <title > 焦点轮播图</title > <style type ="text/css" > * { margin: 0; padding: 0; text-decoration: none; } body { padding: 20px; } #container { width: 600px; height: 400px; overflow: hidden; position : relative ; margin: 0 auto; } #list { width : 4200px ; height: 400px; position : absolute ; z-index: 1; } #list img { float : left ; } #pointsDiv { position: absolute; height: 10px; width: 100px; z-index: 2; bottom: 20px; left: 250px; } #pointsDiv span { cursor: pointer; float: left; border : 1px solid #fff ; width: 10px; height: 10px; border-radius: 50%; background : #333 ; margin-right: 5px; } #pointsDiv .on { background: orangered; } .arrow { cursor: pointer; display: none; line-height: 39px; text-align: center; font-size: 36px; font-weight: bold; width: 40px; height: 40px; position: absolute; z-index: 2; top: 180px; background-color : RGBA (0, 0, 0, 0.3 ); color : #fff ; } .arrow :hover { background-color : RGBA (0, 0, 0, 0.7 ); } #container :hover .arrow { display : block ; } #prev { left: 20px; } #next { right: 20px; } </style > </head > <body > <div id ="container" > <div id ="list" style ="left: -600px;" > <img src ="img/5.jpg" alt ="5" /> <img src ="img/1.jpg" alt ="1" /> <img src ="img/2.jpg" alt ="2" /> <img src ="img/3.jpg" alt ="3" /> <img src ="img/4.jpg" alt ="4" /> <img src ="img/5.jpg" alt ="5" /> <img src ="img/1.jpg" alt ="1" /> </div > <div id ="pointsDiv" > <span index ="1" class ="on" > </span > <span index ="2" > </span > <span index ="3" > </span > <span index ="4" > </span > <span index ="5" > </span > </div > <a href ="javascript:;" id ="prev" class ="arrow" > <</a > <a href ="javascript:;" id ="next" class ="arrow" > ></a > </div > <script type ="text/javascript" src ="jquery-1.10.1.js" > </script > <script type ="text/javascript" src ="app.js" > </script > </body > </html >
逻辑代码 $(function ( ) { var $container = $('#container' ) var $list = $('#list' ) var $points = $('#pointsDiv>span' ) var $prev = $('#prev' ) var $next = $('#next' ) var PAGE_WIDTH = 600 var TIME = 1000 var ITEM_TIME = 50 var imgCount = $points.length var index = 0 var moving = false $next.click(function ( ) { nextPage(true ) }) $prev.click(function ( ) { nextPage(false ) }) var intervalId = setInterval(function ( ) { nextPage(true ) }, 1000 ) $container.hover(function ( ) { clearInterval(intervalId) }, function ( ) { intervalId = setInterval(function ( ) { nextPage(true ) }, 1000 ) }) $points.click(function ( ) { var targetIndex = $(this ).index() if (targetIndex!=index) { nextPage(targetIndex) } }) function nextPage (next ) { if (moving) { return } moving = true var offset = 0 if (typeof next==='boolean' ) { offset = next ? -PAGE_WIDTH : PAGE_WIDTH } else { offset = -(next-index)* PAGE_WIDTH } var itemOffset = offset/(TIME/ITEM_TIME) var currLeft = $list.position().left var targetLeft = currLeft + offset var intervalId = setInterval(function ( ) { currLeft += itemOffset if (currLeft===targetLeft) { clearInterval(intervalId) moving = false if (currLeft===-(imgCount+1 ) * PAGE_WIDTH) { currLeft = -PAGE_WIDTH } else if (currLeft===0 ){ currLeft = -imgCount * PAGE_WIDTH } } $list.css('left' , currLeft) }, ITEM_TIME) updatePoints(next) } function updatePoints (next ) { var targetIndex = 0 if (typeof next === 'boolean' ) { if (next) { targetIndex = index + 1 if (targetIndex===imgCount) { targetIndex = 0 } } else { targetIndex = index - 1 if (targetIndex===-1 ) { targetIndex = imgCount-1 } } } else { targetIndex = next } $points[index].className = '' $points[targetIndex].className = 'on' index = targetIndex } })
Bug
快速点击时, 翻页不正常
解决办法
创建正在翻页的标识,当连续点击时,如果正在翻页,则立即结束翻页