Chinaunix首页 | 论坛 | 博客
  • 博客访问: 199318
  • 博文数量: 43
  • 博客积分: 2178
  • 博客等级: 大尉
  • 技术积分: 390
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-13 15:19
文章分类

全部博文(43)

文章存档

2012年(23)

2011年(10)

2009年(10)

分类: 系统运维

2012-01-11 11:37:11

今天我们为大家介绍一个使用jQuery实现的缩略图逼近效果。主要的想法是当鼠标接近缩略图后,当前的缩略图会放大,并且周围相邻的缩略图也会相应变大一些,当你移动鼠标时,会影响移动方向上的缩略图大小变化,具体效果请大家查看演示。

你可以在这个网站 看到这个效果的原型,这里我们使用jQuery实现了一个jQuery版本的基本效果,希望大家喜欢!

在这个教程中,我们将使用James Padolsey的 jQuery Proximity plugin。

HTML标签

以下代码生成一个无序的缩略图并且添加相关图片描述:

  1. <ul id="pe-thumbs" class="pe-thumbs"> 
  2.     <li> 
  3.         <a href="#"> 
  4.             <img src="images/thumbs/1.jpg" /> 
  5.             <div class="pe-description"> 
  6.                 <h3>Timeh3> 
  7.                 <p>Since time, and his predestinated endp> 
  8.             div>a> 
  9.     li> 
  10.     <li>li> 
  11. ul> 

CSS样式

以下定义了缩略图居中,并且添加背景图片使得图片产生透明度变化效果

  1. pe-thumbs{  
  2.     width: 900px;  
  3.     height: 400px;  
  4.     margin: 20px auto;  
  5.     position: relative;  
  6.     background: transparent url(../images/3.jpg) top center;  
  7.     border: 5px solid #fff;  
  8.     box-shadow: 0 1px 2px rgba(0,0,0,0.2);  
  9. }  

同时我们也使用一个RGBA的背景颜色添加一个小点缀到背景图片。

  1. .pe-thumbs:before {  
  2.     content: "";  
  3.     display: block;  
  4.     position: absolute;  
  5.     top: 0px;  
  6.     left: 0px;  
  7.     width: 100%;  
  8.     height: 100%;  
  9.     background: rgba(255,102,0,0.2);  
  10. }  

列表中的项目将会向左float,并且我们设置锚定和图片的相对位置:

  1. .pe-thumbs li{  
  2.     float: left;  
  3.     position: relative;  
  4. }  
  5. .pe-thumbs li a,  
  6. .pe-thumbs li a img{  
  7.     display: block;  
  8.     position: relative;  
  9. }  

每一个缩略图都初始100px并且透明度为0.2:

  1. .pe-thumbs li a img{  
  2.     width: 100px;  
  3.     opacity: 0.2;  

最后我们定义描述内容的样式:

  1. .pe-description h3{  
  2.     padding: 10px 10px 0px 10px;  
  3.     line-height: 20px;  
  4.     font-family: 'BebasNeueRegular', 'Arial Narrow', Arial, sans-serif;  
  5.     font-size: 22px;  
  6.     margin: 0px;  
  7. }  
  8. .pe-description p{  
  9.     padding: 10px 0px;  
  10.     margin: 10px;  
  11.     font-size: 11px;  
  12.     font-style: italic;  
  13.     border-top: 1px solid rgba(255,255,255,0.3);  
  14. }  

JavaScript代码

主要的想法是当鼠标悬浮后计算所有的描述容器大小和位置。主要依赖于缩略图的最大尺寸及其居于主要wrapper中的位置。例如,当缩略图接近边缘,我们就使得描述区域显示在缩略图左边

然后我们将帮定逼近事件到图片。主要想法是根据鼠标位置来变化图片大小。一旦图片达到最大尺寸,我们设置z-index最高,因此位于最高层次,并且显示分开的描述。

  1. // list of thumbs  
  2. var $list        = $('#pe-thumbs'),  
  3.     // list's width and offset left.  
  4.     // this will be used to know the position of the description container  
  5.     listW        = $list.width(),  
  6.     listL        = $list.offset().left,  
  7.     // the images  
  8.     $elems        = $list.find('img'),  
  9.     // the description containers  
  10.     $descrp        = $list.find('div.pe-description'),  
  11.     // maxScale : maximum scale value the image will have  
  12.     // minOpacity / maxOpacity : minimum (set in the CSS) and maximum values for the image's opacity  
  13.     settings    = {  
  14.         maxScale    : 1.3,  
  15.         maxOpacity    : 0.9,  
  16.         minOpacity    : Number( $elems.css('opacity') )  
  17.     },  
  18.     init        = function() {  
  19.  
  20.         // minScale will be set in the CSS  
  21.         settings.minScale = _getScaleVal() || 1;  
  22.         // preload the images (thumbs)  
  23.         _loadImages( function() {  
  24.  
  25.             _calcDescrp();  
  26.             _initEvents();  
  27.  
  28.         });  
  29.  
  30.     },  
  31.     // Get Value of CSS Scale through JavaScript:  
  32.     //   
  33.     _getScaleValfunction() {  
  34.  
  35.         var st = window.getComputedStyle($elems.get(0), null),  
  36.             tr = st.getPropertyValue("-webkit-transform") ||  
  37.                  st.getPropertyValue("-moz-transform") ||  
  38.                  st.getPropertyValue("-ms-transform") ||  
  39.                  st.getPropertyValue("-o-transform") ||  
  40.                  st.getPropertyValue("transform") ||  
  41.                  "fail...";  
  42.  
  43.         if( tr !== 'none' ) {       
  44.  
  45.             var values = tr.split('(')[1].split(')')[0].split(','),  
  46.                 a = values[0],  
  47.                 b = values[1],  
  48.                 c = values[2],  
  49.                 d = values[3];  
  50.  
  51.             return Math.sqrt( a * a + b * b );  
  52.  
  53.         }  
  54.  
  55.     },  
  56.     // calculates the style values for the description containers,  
  57.     // based on the settings variable  
  58.     _calcDescrp    = function() {  
  59.  
  60.         $descrp.each( function(i) {  
  61.  
  62.             var $el        = $(this),  
  63.                 $img    = $el.prev(),  
  64.                 img_w    = $img.width(),  
  65.                 img_h    = $img.height(),  
  66.                 img_n_w    = settings.maxScale * img_w,  
  67.                 img_n_h    = settings.maxScale * img_h,  
  68.                 space_t = ( img_n_h - img_h ) / 2,  
  69.                 space_l = ( img_n_w - img_w ) / 2;  
  70.  
  71.             $el.data( 'space_l', space_l ).css({  
  72.                 height    : settings.maxScale * $el.height(),  
  73.                 top        : -space_t,  
  74.                 left    : img_n_w - space_l  
  75.             });  
  76.  
  77.         });  
  78.  
  79.     },  
  80.     _initEvents    = function() {  
  81.  
  82.         $elems.on('proximity.Photo', { max: 80, throttle: 10, fireOutOfBounds : true }, function(event, proximity, distance) {  
  83.  
  84.             var $el            = $(this),  
  85.                 $li            = $el.closest('li'),  
  86.                 $desc        = $el.next(),  
  87.                 scaleVal    = proximity * ( settings.maxScale - settings.minScale ) + settings.minScale,  
  88.                 scaleExp    = 'scale(' + scaleVal + ')';  
  89.  
  90.             // change the z-index of the element once  
  91.             // it reaches the maximum scale value  
  92.             // also, show the description container  
  93.             if( scaleVal === settings.maxScale ) {  
  94.  
  95.                 $li.css( 'z-index', 1000 );  
  96.  
  97.                 if( $desc.offset().left + $desc.width() > listL + listW ) {  
  98.  
  99.                     $desc.css( 'left', -$desc.width() - $desc.data( 'space_l' ) );  
  100.  
  101.                 }  
  102.  
  103.                 $desc.fadeIn( 800 );  
  104.  
  105.             }  
  106.             else {  
  107.  
  108.                 $li.css( 'z-index', 1 );  
  109.  
  110.                 $desc.stop(true,true).hide();  
  111.  
  112.             }      
  113.  
  114.             $el.css({  
  115.                 '-webkit-transform'    : scaleExp,  
  116.                 '-moz-transform'    : scaleExp,  
  117.                 '-o-transform'        : scaleExp,  
  118.                 '-ms-transform'        : scaleExp,  
  119.                 'transform'            : scaleExp,  
  120.                 'opacity'            : ( proximity * ( settings.maxOpacity - settings.minOpacity ) + settings.minOpacity )  
  121.             });  
  122.  
  123.         });  
  124.  
  125.     },  
  126.     _loadImages    = function( callback ) {  
  127.  
  128.         var loaded     = 0,  
  129.             total    = $elems.length;  
  130.  
  131.         $elems.each( function(i) {  
  132.  
  133.             var $el = $(this);  
  134.  
  135.             $('<img>').load( function() {  
  136.  
  137.                 ++loaded;  
  138.                 if( loaded === total )  
  139.                     callback.call();  
  140.  
  141.             }).attr( 'src', $el.attr('src') );  
  142.  
  143.         });  
  144.  
  145.     };  
  146.  
  147. return {  
  148.     init    : init  
  149. };   

原文:http://www.cnblogs.com/gbin1/archive/2012/01/09/2317605.html

阅读(1816) | 评论(0) | 转发(0) |
0

上一篇:Shell编程基础

下一篇:大话php性能

给主人留下些什么吧!~~