jQuery轮播图实例详解

1、html+css+js代码

<!DOCTYPE html> <html> <head> <title></title> <style type="text/css"> *{ margin: 0; padding: 0; text-decoration: none; } body{ padding: 20px; } #container{ min-width: 1000px; /*width: 1300px;*/ height: 400px; overflow: hidden; position: relative; margin: 0 auto; } #list{ /*width: 9100px;*/ height: 400px; position: absolute; z-index: 1; top:0; left: 0; overflow: hidden; } #list img{ float: left; /*width: 1300px;*/ height: 400px; } #buttons{ position: absolute; height: 10px; width: 100px; z-index: 2; bottom: 20px; left: 660px; text-align: center; } #buttons span{ cursor: pointer; float: left; width: 10px; height: 10px; margin-right: 9px; display: inline-block; background-image: url(img/_eb1b95e.png); background-position: -1079px -687px; } #buttons .on{ background-position: -1049px -687px; } .arrow{ cursor: pointer; display: none; width: 36px; height: 76px; position: absolute; z-index: 2; top: 180px; background-color: rgba(0,0,0,.3); color: #fff; } #container:hover .arrow{ display: block; } #prev{ background: url(img/_eb1b95e.png); background-position: -569px -491px; left: 20px; } #next{ background: url(img/_eb1b95e.png); background-position: -513px -491px; right: 20px; } </style> </head> <body> <div> <div > <img src="https://www.jb51.net/img/5.jpg" alt="1"/> <img src="https://www.jb51.net/img/1.jpg" alt="1"/> <img src="https://www.jb51.net/img/2.jpg" alt="2"/> <img src="https://www.jb51.net/img/3.jpg" alt="3"/> <img src="https://www.jb51.net/img/4.jpg" alt="4"/> <img src="https://www.jb51.net/img/5.jpg" alt="5"/> <img src="https://www.jb51.net/img/1.jpg" alt="5"/> </div> <div> <span index="1"></span> <span index="2"></span> <span index="3"></span> <span index="4"></span> <span index="5"></span> </div> <a href="javascript:;"></a> <a href="javascript:;"></a> </div> <script type="text/javascript" src="https://www.jb51.net/js/jquery.1.10.2.js"></script> <script type="text/javascript"> var container = $("#container"); var list = $("#list"); var listimg = $("#list img"); var buttons = $("#buttons span"); var prev = $("#prev"); var next = $("#next"); var index = 1; var len = 5; var num =len+2; var interval = 3000;//变换周期 var timer; var clientwidth=document.documentElement.clientWidth;//屏幕的宽度 var conwidth = parseInt(clientwidth)-100;//显示界面的宽度 $(function(){ setwidth();//设置container的宽度以及里面元素list和list中img的宽度 function animate(offset){ var left = parseInt(list.css("left"))+offset; // list.animate({left:left+'px'},'normal'); list.animate({left:left+'px'},conwidth,function(){ //第一位规定产生动画效果的样式,第二位设置速度,第三位是动画函数执行完后执行的函数 if (left > -conwidth) {//如果是第一个元素还向前移,就让最后一个元素是这个元素 list.css('left',-conwidth*len); } if (left < (-conwidth*len)) {//如果是最后一个元素还向后移,就让第一个元素是这个元素 list.css('left', -conwidth); } }); } function showbutton(){//通过操作css来将显示的图片代表的下方原点变大,其余变小 buttons.eq(index-1).addClass('on').siblings().removeClass('on'); } function play(){ timer = setTimeout(function(){ next.trigger('click');//trigger()方法触发被选元素的指定事件类型。 play(); },interval); } function stop(){ clearTimeout(timer); } next.bind('click',function(){ if (list.is(':animated')) { return; } if (index == 5) { index = 1; } else{ index++; } animate(-conwidth); showbutton(); }); prev.bind('click',function(){ if (list.is(':animated')) { return; } if (index == 1) { index = 5; } else{ index--; } animate(conwidth); showbutton(); }); buttons.each(function(){ $(this).bind('click',function(){ if (list.is(':animated') || $(this).attr('class')=='on') { return; } var myindex = parseInt($(this).attr('index')); var offset = -conwidth*(myindex - index); animate(offset); index = myindex; showbutton(); }) }); container.hover(stop,play);//鼠标悬停时执行stop()函数,移开时执行play() play(); }); function setwidth(){//设置container的宽度以及里面元素list和list中img的宽度 container[0].style.width = conwidth +'px' ; list[0].style.width = num*conwidth +'px'; list[0].style.left = '-'+conwidth +'px'; for (var i = 0; i < listimg.length; i++) { listimg[i].style.width = conwidth + 'px'; } } </script> </body> </html>

2、实现思路

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/3d685e8db0af898c5d754e5bed4d2122.html