<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta content="" /> <meta content="" /> <link type="text/css" href="https://www.jb51.net/css/global.css" /> <script type="text/javascript" src="https://www.jb51.net/js/jquery-1.7.min.js"></script> <script type="text/javascript" src="https://www.jb51.net/js/scroll_img.js"></script> <title>图片列表滚动</title> </head> <body> <div> <p><a href="javascript:void(0)"><img src="https://www.jb51.net/images/btn.jpg" /></a></p> <div> <ul> <li> <p><a href="#"><img src="https://www.jb51.net/images/1.jpg" alt="#" /></a></p> <p><a href="#">information1</a></p> </li> <li> <p><a href="#"><img src="https://www.jb51.net/images/1.jpg" alt="#" /></a></p> <p><a href="#">information2</a></p> </li> <li> <p><a href="#"><img src="https://www.jb51.net/images/1.jpg" alt="#" /></a></p> <p><a href="#">information3</a></p> </li> <li> <p><a href="#"><img src="https://www.jb51.net/images/1.jpg" alt="#" /></a></p> <p><a href="#">information4</a></p> </li> <li> <p><a href="#"><img src="https://www.jb51.net/images/2.jpg" alt="#" /></a></p> <p><a href="#">information5</a></p> </li> <li> <p><a href="#"><img src="https://www.jb51.net/images/2.jpg" alt="#" /></a></p> <p><a href="#">information6</a></p> </li> <li> <p><a href="#"><img src="https://www.jb51.net/images/2.jpg" alt="#" /></a></p> <p><a href="#">information7</a></p> </li> <li> <p><a href="#"><img src="https://www.jb51.net/images/2.jpg" alt="#" /></a></p> <p><a href="#">information8</a></p> </li> <li> <p><a href="#"><img src="https://www.jb51.net/images/1.jpg" alt="#" /></a></p> <p><a href="#">information9</a></p> </li> <li> <p><a href="#"><img src="https://www.jb51.net/images/1.jpg" alt="#" /></a></p> <p><a href="#">information10</a></p> </li> <li> <p><a href="#"><img src="https://www.jb51.net/images/1.jpg" alt="#" /></a></p> <p><a href="#">information11</a></p> </li> <li> <p><a href="#"><img src="https://www.jb51.net/images/1.jpg" alt="#" /></a></p> <p><a href="#">information12</a></p> </li> <li> <p><a href="#"><img src="https://www.jb51.net/images/1.jpg" alt="#" /></a></p> <p><a href="#">information13</a></p> </li> <li> <p><a href="#"><img src="https://www.jb51.net/images/1.jpg" alt="#" /></a></p> <p><a href="#">information14</a></p> </li> </ul> </div> <p><a href="javascript:void(0)"><img src="https://www.jb51.net/images/btn.jpg" /></a></p> </div> </body> <script type="text/javascript"> (function(){ var test=new scroll_img('scroll_img',150,590,140,10); test.initialize(); $('.left_btn').click(function(){ test.stop(); test.left(); test.autoplay(); }); $('.right_btn').click(function(){ test.stop(); test.right(); test.autoplay(); }); })(); </script> </html>
scroll_img.js如下:
function scroll_img(target,distance,show_width,li_width,mr){ this.distance=distance; //每次移动距离 this.target=$("#"+target); this.show_width=show_width; //显示区域宽度 this.li_width=li_width; //items宽度 this.mr=mr; //items间距 this.scrollbar=$("#"+target).find('ul'); this.position=0; this.direction=1; } scroll_img.prototype={ version:1.00, author:"yangfeifei", date:2011-11-21, initialize:function(){ var t=this; t.scrollbar.css('position','relative'); //初始动作 t.autoplay(); t.scrollbar.mouseover(function(){t.stop();}); //鼠标移到图片上停止自动播放 t.scrollbar.mouseout(function(){t.autoplay();}); //鼠标移出图片开始自动播放 }, right:function(){ var t=this; (-t.position)<t.total_length()?t.position-=t.distance:t.position=t.position; if((-t.position)<t.total_length()){ t.scrollbar.animate({left:t.position},500); t.direction=1; } if((-t.position)==t.total_length()){ t.scrollbar.animate({left:t.position},500); t.direction=-1; } }, left:function(){ var t=this; (-t.position)>0?t.position+=t.distance:t.position=t.position; if((-t.position)>0){ t.scrollbar.animate({left:t.position},500); t.direction=-1; } if((-t.position)==0){ t.scrollbar.animate({left:t.position},500); t.direction=1; } }, total_length:function(){ var t=this, total_num=t.scrollbar.find('li').length; return total_num*(t.li_width+t.mr)-(t.mr+t.show_width); }, autoplay:function(){ var t=this; t.setInt=setInterval(function(){ t.direction==1?t.right():t.left(); },3000); }, stop:function(){ var t=this; clearInterval(t.setInt); } }
global.css如下: