幻灯片/轮播

jQuery缩略图翻转幻灯片插件

阿里云

这是一款效果炫酷的 jQuery 缩略图翻转全屏响应式幻灯片插件。该幻灯片插件可以通过键盘上下键或鼠标滚轮进行导航切换。幻灯片在切换时缩略图会带有垂直翻转动画效果。这是一款效果炫酷的 jQuery 缩略图翻转全屏响应式幻灯片插件。该插件可以通过鼠标滚轮和键盘上下键进行导航。在缩略图上有一个放大缩小的按钮可以使背景图片退出全屏模式。它还是响应式的,你可以缩放浏览器看看效果。

HTML 部分:

首先添加一个 loading 元素用于加载图片时显示:

也想出现在这里?联系我们
创客主机
  1. <div id="tf_loading" class="tf_loading"></div>

使用一个全屏大小的 div 包住所有的图片:

  1. <div id="tf_bg" class="tf_bg">
  2.     <img src="images/1.jpg" alt="Image 1" longdesc="images/thumbs/1.jpg" />
  3.     <img src="images/2.jpg" alt="Image 2" longdesc="images/thumbs/2.jpg"/>
  4.     <img src="images/3.jpg" alt="Image 3" longdesc="images/thumbs/3.jpg"/>
  5.     <img src="images/4.jpg" alt="Image 4" longdesc="images/thumbs/4.jpg"/>
  6.     <img src="images/5.jpg" alt="Image 5" longdesc="images/thumbs/5.jpg"/>
  7.     <img src="images/6.jpg" alt="Image 6" longdesc="images/thumbs/6.jpg"/>
  8.     <div class="tf_pattern"></div>
  9. </div>

这里使用一个“longdesc”自定义属性来指定缩略图的位置。最后一个 div 用于叠加模式。接着用一个 div tf_content_wrapper 来制作左边的图片描述信息。

  1. <div id="tf_content_wrapper" class="tf_content_wrapper">
  2.     <div class="tf_content" id="content1" style="display:block;">
  3.         <h2>Dreamer</h2>
  4.         <p>Far far away, behind the word mountains, ... </p>
  5.     </div>
  6.     <div class="tf_content" id="content2">
  7.         ...
  8.     </div>
  9.     ...
  10. </div>

右边的缩略图的结构如下:

  1. <div id="tf_thumbs" class="tf_thumbs">
  2.     <span id="tf_zoom" class="tf_zoom"></span>
  3.     <img src="images/thumbs/1.jpg" alt="Thumb1"/>
  4. </div>

span 中的 class tf_zoom 或 tf_fullscreen 用于控制是否全屏模式。最后添加一对前后导航按钮。

  1. <div id="tf_next" class="tf_next"></div>
  2. <div id="tf_prev" class="tf_prev"></div>

JAVASCRIPT 部分:

首先要引入 jQuery 和相关的 js 文件:

  1. <script type="text/javascript" src="http://ajax.useso.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>
  2. <script type="text/javascript" src="http://ajax.useso.com/ajax/libs/jqueryui/1.8.9/jquery-ui.min.js"></script>
  3. <script type="text/javascript" src="js/jquery.flip.js"></script>
  4. <script type="text/javascript" src="js/jquery-mousewheel-3.0.4/jquery.mousewheel.min.js"></script>

添加预加载图片函数:

  1. (function($) {
  2.     $.fn.preload = function(options) {
  3.         var opts    = $.extend({}, $.fn.preload.defaults, options);
  4.         o           = $.meta ? $.extend({}, opts, this.data()) : opts;
  5.         var c       = this.length,
  6.             l       = 0;
  7.         return this.each(function() {
  8.             var $i  = $(this);
  9.             $('<img>').load(function(i){
  10.                 ++l;
  11.                 if(l == c) o.onComplete();
  12.             }).attr('src',$i.attr('src'));  
  13.         });
  14.     };
  15.     $.fn.preload.defaults = {
  16.         onComplete  : function(){return false;}
  17.     };
  18. })(jQuery);

接着设置一些变量参数:

  1. var $tf_bg              = $('#tf_bg'),
  2.     $tf_bg_images       = $tf_bg.find('img'),
  3.     $tf_bg_img          = $tf_bg_images.eq(0),
  4.     $tf_thumbs          = $('#tf_thumbs'),
  5.     total               = $tf_bg_images.length,
  6.     current             = 0,
  7.     $tf_content_wrapper = $('#tf_content_wrapper'),
  8.     $tf_next            = $('#tf_next'),
  9.     $tf_prev            = $('#tf_prev'),
  10.     $tf_loading         = $('#tf_loading');

接下来预加载图片:

  1. $tf_bg_images.preload({
  2.     onComplete  : function(){
  3.         $tf_loading.hide();
  4.         init();
  5.     }
  6. });

然后使用参数函数显示第一张图片和初始化一些事件:

  1. function init(){
  2.     //get dimentions for the image, based on the windows size
  3.     var dim = getImageDim($tf_bg_img);
  4.     //set the returned values and show the image
  5.     $tf_bg_img.css({
  6.         width   : dim.width,
  7.         height  : dim.height,
  8.         left    : dim.left,
  9.         top     : dim.top
  10.     }).fadeIn();
  11.     //resizing the window resizes the $tf_bg_img
  12.     $(window).bind('resize',function(){
  13.         var dim = getImageDim($tf_bg_img);
  14.         $tf_bg_img.css({
  15.             width   : dim.width,
  16.             height  : dim.height,
  17.             left    : dim.left,
  18.             top     : dim.top
  19.         });
  20.     });
  21.     //expand and fit the image to the screen
  22.     $('#tf_zoom').live('click',
  23.     function(){
  24.         if($tf_bg_img.is(':animated'))
  25.             return false;
  26.         var $this   = $(this);
  27.         if($this.hasClass('tf_zoom')){
  28.             resize($tf_bg_img);
  29.             $this.addClass('tf_fullscreen')
  30.             .removeClass('tf_zoom');
  31.         }
  32.         else{
  33.             var dim = getImageDim($tf_bg_img);
  34.             $tf_bg_img.animate({
  35.                 width   : dim.width,
  36.                 height  : dim.height,
  37.                 top     : dim.top,
  38.                 left    : dim.left
  39.             },350);
  40.             $this.addClass('tf_zoom')
  41.             .removeClass('tf_fullscreen');  
  42.         }
  43.     }
  44.     );
  45.     //click the arrow down, scrolls down
  46.     $tf_next.bind('click',function(){
  47.         if($tf_bg_img.is(':animated'))
  48.             return false;
  49.         scroll('tb');
  50.     });
  51.     //click the arrow up, scrolls up
  52.     $tf_prev.bind('click',function(){
  53.         if($tf_bg_img.is(':animated'))
  54.             return false;
  55.         scroll('bt');
  56.     }); 
  57.     //mousewheel events - down / up button trigger the scroll down / up
  58.     $(document).mousewheel(function(e, delta) {
  59.         if($tf_bg_img.is(':animated'))
  60.             return false;
  61.  
  62.         if(delta > 0)
  63.             scroll('bt');
  64.         else
  65.             scroll('tb');
  66.         return false;
  67.     });
  68.     //key events - down / up button trigger the scroll down / up
  69.     $(document).keydown(function(e){
  70.         if($tf_bg_img.is(':animated'))
  71.             return false;
  72.         switch(e.which){
  73.             case 38:    
  74.                 scroll('bt');
  75.                 break;  
  76.             case 40:    
  77.                 scroll('tb');
  78.                 break;
  79.         }
  80.     });
  81. }

“scroll”函数用于控制前一张和后一张图片的显示:

  1. function scroll(dir){
  2.     //if dir is "tb" (top -> bottom) increment current, 
  3.     //else if "bt" decrement it
  4.     current = (dir == 'tb')?current + 1:current - 1;
  5.     //we want a circular slideshow, 
  6.     //so we need to check the limits of current
  7.     if(current == total) current = 0;
  8.     else if(current < 0) current = total - 1;
  9.     //flip the thumb
  10.     $tf_thumbs.flip({
  11.         direction   : dir,
  12.         speed       : 400,
  13.         onBefore    : function(){
  14.             //the new thumb is set here
  15.             var content = '<span id="tf_zoom" class="tf_zoom"></span>';
  16.             content     +='<img src="' + $tf_bg_images.eq(current).attr('longdesc') + '" alt="Thumb' + (current+1) + '"/>';
  17.             $tf_thumbs.html(content);
  18.         }
  19.     });
  20.     //we get the next image
  21.     var $tf_bg_img_next = $tf_bg_images.eq(current),
  22.     //its dimentions
  23.     dim             = getImageDim($tf_bg_img_next),
  24.     //the top should be one that makes the image out of the viewport
  25.     //the image should be positioned up or down depending on the direction
  26.     top = (dir == 'tb')?$(window).height() + 'px':-parseFloat(dim.height,10) + 'px';      
  27.     //set the returned values and show the next image   
  28.     $tf_bg_img_next.css({
  29.         width   : dim.width,
  30.         height  : dim.height,
  31.         left    : dim.left,
  32.         top     : top
  33.     }).show(); 
  34.     //now slide it to the viewport
  35.     $tf_bg_img_next.stop().animate({
  36.         top     : dim.top
  37.     },1000);     
  38.     //we want the old image to slide in the same direction, out of the viewport
  39.     var slideTo = (dir == 'tb')?-$tf_bg_img.height() + 'px':$(window).height() + 'px';
  40.     $tf_bg_img.stop().animate({
  41.         top     : slideTo
  42.     },1000,function(){
  43.         //hide it
  44.         $(this).hide();
  45.         //the $tf_bg_img is now the shown image
  46.         $tf_bg_img  = $tf_bg_img_next;
  47.         //show the description for the new image
  48.         $tf_content_wrapper.children()
  49.         .eq(current)
  50.         .show();
  51.     });
  52.     //hide the current description  
  53.     $tf_content_wrapper.children(':visible')
  54.     .hide()
  55.  
  56. }

“resize”函数用于控制屏幕缩小放大时图片的响应式效果:

  1. function resize($img){
  2.     var w_w = $(window).width(),
  3.     w_h = $(window).height(),
  4.     i_w = $img.width(),
  5.     i_h = $img.height(),
  6.     r_i = i_h / i_w,
  7.     new_w,new_h;
  8.  
  9.     if(i_w > i_h){
  10.         new_w   = w_w;
  11.         new_h   = w_w * r_i;
  12.  
  13.         if(new_h > w_h){
  14.             new_h   = w_h;
  15.             new_w   = w_h / r_i;
  16.         }
  17.     }   
  18.     else{
  19.         new_h   = w_w * r_i;
  20.         new_w   = w_w;
  21.     }
  22.  
  23.     $img.animate({
  24.         width   : new_w + 'px',
  25.         height  : new_h + 'px',
  26.         top     : '0px',
  27.         left    : '0px'
  28.     },350);
  29. }

最后一个函数“getImageDim”用于获取图片的位置:

  1. function getImageDim($img){
  2.     var w_w = $(window).width(),
  3.     w_h = $(window).height(),
  4.     r_w = w_h / w_w,
  5.     i_w = $img.width(),
  6.     i_h = $img.height(),
  7.     r_i = i_h / i_w,
  8.     new_w,new_h,
  9.     new_left,new_top;
  10.  
  11.     if(r_w > r_i){
  12.         new_h   = w_h;
  13.         new_w   = w_h / r_i;
  14.     }
  15.     else{
  16.         new_h   = w_w * r_i;
  17.         new_w   = w_w;
  18.     }
  19.     return {
  20.         width   : new_w + 'px',
  21.         height  : new_h + 'px',
  22.         left    : (w_w - new_w) / 2 + 'px',
  23.         top     : (w_h - new_h) / 2 + 'px'
  24.     };
  25. }

jQuery 缩略图翻转幻灯片插件

已有 412 人购买
查看演示升级 VIP立刻购买

演示地址 下载地址
收藏
(0)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!