用jquery封裝了一個控制圖片左右滾動的插件,有左右按鈕可以點擊實現圖片滾動效果。
代碼如下:
<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title>slide</title> <script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/1.6.2/jquery.min.js"></script><br><style> * { padding:0; margin:0; list-style: none } img{ border: none } #img-slider, #img-slider-2 { position: relative; width: 560px; height: 80px; } .slider-wrap { width: 480px;/*width的值要跟所有的li寬度總和一樣*/ overflow: hidden; position: relative; height: 150px; margin-left: 40px; } .slider-wrap ul { zoom: 1; position: absolute; left: 0; top: 0; width: 9999px; } .slider-wrap ul li { float: left; width: 120px; text-align: center; padding: 5px 0; } #prev, #next { position: absolute; top: 30px; left: 0px; width: 40px } #next { left: auto; right: 0px } </style><br><script type="text/javascript"> $(function () { $("#img-slider").imgScroll(); $("#img-slider-2").imgScroll(); }); /*插件*/ (function ($) { $.fn.imgScroll = function () { var isDone = false, scrollBox = $(this), prevBtn = scrollBox.find("#prev"), nextBtn = scrollBox.find("#next"), imgBox = scrollBox.find("ul"), next_over = imgBox.find("li").width() * imgBox.find("li").length, slide_width = $(".slider-wrap").width(); return this.each(function(){ function setOpacity() { imgBox.animate({ opacity: 1 }, 800,function(){ isDone = false; }) } function scrollNext() { if(!isDone && next_over + parseInt(imgBox.css("left"),10) > slide_width){ isDone = true; imgBox.animate({ left: "+=" + "-" + slide_width, opacity: 0.5 }, 800, setOpacity); } //isDone = false } function scrollPrev() { if(!imgBox.is(':animated') && parseInt(imgBox.css("left"),10) !=0){ imgBox.animate({ left: "+=" + slide_width,//不斷左移 opacity: 0.5 }, 800, setOpacity); } } prevBtn.bind('click', scrollPrev); //向前滾動 nextBtn.bind('click', scrollNext); //向後滾動 }) } })(jQuery); </script> </head> <body> <div id="img-slider"> <button id="prev">prev</button> <div class="slider-wrap"> <ul> <li><a href="#"><img src="http://www.baidu.com/images/logo.gif" width="100"/></a></li> <li><a href="#"><img src="http://www.baidu.com/images/logo.gif" width="100"/></a></li> <li><a href="#"><img src="http://www.baidu.com/images/logo.gif" width="100"/></a></li> <li><a href="#"><img src="http://www.baidu.com/images/logo.gif" width="100"/></a></li> <li><a href="#"><img src="http://www.baidu.com/images/logo.gif" width="100"/></a></li> <li><a href="#"><img src="http://www.baidu.com/images/logo.gif" width="100"/></a></li> </ul> </div> <button id="next">next</button> </div> <div id="img-slider-2"> <button id="prev">prev</button> <div class="slider-wrap"> <ul> <li><a href="#"><img src="http://www.baidu.com/images/logo.gif" width="100"/></a></li> <li><a href="#"><img src="http://www.baidu.com/images/logo.gif" width="100"/></a></li> <li><a href="#"><img src="http://www.baidu.com/images/logo.gif" width="100"/></a></li> <li><a href="#"><img src="http://www.baidu.com/images/logo.gif" width="100"/></a></li> <li><a href="#"><img src="http://www.baidu.com/images/logo.gif" width="100"/></a></li> <li><a href="#"><img src="http://www.baidu.com/images/logo.gif" width="100"/></a></li> </ul> </div> <button id="next">next</button> </div> </body> </html>
大家可以直接復制上面的代碼,保存成html測試。