1. 程式人生 > >【jQuery Demo】圖片切換效果整理

【jQuery Demo】圖片切換效果整理

ges 比較 nta containe 水平滾動 :hover 實現 small vertica

圖片的切換效果有很多,比較常見的有水平滾動、垂直滾動、淡入淡出等。我們接下來一一實現這些效果。

1.水平滾動

1) 我們先來實現HTML頁面,代碼很簡單:

<div id="container">
    <ul class="slider">
        <li><img src="../imgs/Girls/04.png"/></li>
        <li><img src="../imgs/Girls/05.jpg"/></li>
        <li><img src
="../imgs/Girls/14.jpg"/></li> <li><img src="../imgs/Girls/17.jpg"/></li> <li><img src="../imgs/Girls/20.jpg"/></li> </ul> <ul class="thumb"> <li>1</li> <li>2</li> <li>3</li>
<li>4</li> <li>5</li> </ul> </div>

2)然後我們設置下CSS:

/**   CSS Reset  **/
body, h1, h2, h3, h4, h5, h6, hr, p, blockquote, dl, dt, dd, ul, ol, li, pre, form, fieldset, legend, button, input, textarea, th, td { margin: 0; padding: 0; }
body, button, input, select, textarea 
{ font: 12px/1.5 tahoma, arial, \5b8b\4f53; } h1, h2, h3, h4, h5, h6 { font-size: 100%; } address, cite, dfn, em, var { font-style: normal; } code, kbd, pre, samp { font-family: couriernew, courier, monospace; } small { font-size: 12px; } ul, ol { list-style: none; } a { text-decoration: none; } a:hover { text-decoration: underline; } sup { vertical-align: text-top; } sub { vertical-align: text-bottom; } legend { color: #000; } fieldset, img { border: 0; } button, input, select, textarea { font-size: 100%; } table { border-collapse: collapse; border-spacing: 0; } /* container */ #container { width: 320px; height: 456px; overflow: hidden; position: relative; margin: 20px auto; } .slider { position: absolute; } .slider img { width: 320px; display: block; } .thumb { position: absolute; bottom: 5px; right: 5px; } .thumb li { float: left; color: #FF7300; text-align: center; line-height: 16px; width: 16px; height: 16px; font-size: 12px; font-family: Arial; margin: 3px 1px; border: 1px solid #FF7300; background: #fff; cursor: pointer; } .thumb li:hover,.thumb li.selected { color: #fff; line-height: 16px; width: 16px; height: 16px; font-size: 16px; background: #EF7300; font-weight: bold; }

目前的顯示效果如下:

技術分享圖片

3)接下來,我們需要點擊實現圖片的切換,下面是實現水平滾動效果的jQuery插件:

;(function ($) {
   $.fn.extend({
       scrollHorizontal:function () {
           var imgCount = $(".slider li").length;
           var imgWidth = $(".slider li").eq(0).width();
           $(".thumb li").eq(0).addClass("selected");
           for (var i=0;i<imgCount;i++){
               $(".slider li").eq(i).css({
                   "left":i*imgWidth+"px",
                   "position":"absolute"
               });
           }
           // 初始化
          $(".thumb li").click(function () {
              var theIndex = $(this).index();
              var nowIndex = $(".selected").index();
              var leftWidth = imgWidth*(nowIndex-theIndex);
              $(".thumb li").removeClass("selected");
              $(".thumb li").eq(theIndex).addClass("selected");
              $(".slider li").animate({left:"+="+leftWidth });
          });
       }
   });
})(jQuery);

4)最後,我們在HTML頁面調用這個插件:

<script>
    $(document).ready(function () {
        $("#container").scrollHorizontal();
    })
</script>

5)這樣效果就出來了:

技術分享圖片

2.垂直滾動

上面實現了水平滾動,那垂直滾動就簡單了。通過獲取圖片的高度,然後控制 top 的值就可以了。新建的jQuery插件如下:

;(function ($) {
   $.fn.extend({
       scrollVertical:function () {
           var imgCount = $(".slider li").length;
           var imgHeight = $(".slider li").eq(0).height();
           $(".thumb li").eq(0).addClass("selected");
           for (var i=0;i<imgCount;i++){
               $(".slider li").eq(i).css({
                   "top":i*imgHeight+"px",
                   "position":"absolute"
               });
           }
           // 初始化
           $(".thumb li").click(function () {
               var theIndex = $(this).index();
               var nowIndex = $(".selected").index();
               var topHeight = imgHeight*(nowIndex-theIndex);
               $(".thumb li").removeClass("selected");
               $(".thumb li").eq(theIndex).addClass("selected");
               $(".slider li").animate({top:"+="+topHeight });
           });
       }
   });
})(jQuery);

然後調用這個插件就可以了:

<script>
    $(document).ready(function () {
        $("#container").scrollVertical();
    })
</script>

效果如下:

技術分享圖片

3.淡入淡出

同樣淡入淡出也就很好實現了:

;(function ($) {
   $.fn.extend({
       fadeInOut:function () {
           var imgCount = $(".slider li").length;
           var imgHeight = $(".slider li").eq(0).height();
           $(".thumb li").eq(0).addClass("selected");
           for (var i=1;i<imgCount;i++){
               $(".slider li").eq(i).css({
                   "display":"none"
               });
           }
           $(".thumb li").click(function () {
               var theIndex = $(this).index();
               var nowIndex = $(".selected").index();
               $(".thumb li").removeClass("selected");
               $(".thumb li").eq(theIndex).addClass("selected");
               $(".slider li").eq(nowIndex).fadeOut();
               $(".slider li").eq(theIndex).fadeIn();
           });
       }
   });
})(jQuery);

效果如下:

技術分享圖片

【jQuery Demo】圖片切換效果整理