使用jQuery和CSS实现超酷缩略图悬浮逼近效果

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

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

实例下载

你可以在这个网站http://porscheeveryday.com/ 看到这个效果的原型,这里我们使用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>Time</h3> 
  7.                 <p>Since time, and his predestinated end</p> 
  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.     // http://css-tricks.com/get-value-of-css-rotation-through-javascript/  
  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

【编辑推荐】

  1. 11月Web技术最前沿:2011年度***jQuery插件揭晓
  2. 使用HTML 5、CSS3和jQuery增强网站用户体验
  3. 使用jQuery设计数据表格之设计表格基类
  4. 10月10款有趣强大的jQuery插件推荐
  5. 当jQuery遭遇CoffeeScript——妙不可言
责任编辑:陈贻新 来源: GBin1.com
相关推荐

2012-07-18 20:59:40

jQuery

2013-12-02 15:07:57

jQuery插件

2009-08-28 15:19:17

C#实现缩略图

2013-08-12 15:26:49

测试

2009-10-26 17:38:22

VB.NET实现缩略图

2019-02-15 14:00:57

Linux命令缩略图

2009-12-07 11:21:59

PHP生成缩略图

2009-08-28 10:22:13

Windows 7系统故障应对缩略图无法显示

2009-08-12 16:33:37

.NET生成缩略图

2011-07-01 11:18:50

Qt 多线程

2010-01-20 10:29:37

Chrome缩略图标签管理

2012-09-20 15:00:38

Win 8操作系统

2020-11-02 14:49:46

GitHub Java图片

2011-05-04 09:05:39

Flash

2022-02-21 16:38:19

Serverless图片视频

2011-03-02 13:15:26

HTML 5jQuery

2023-05-15 17:04:33

Edge浏览器

2021-07-01 14:52:17

Windows 11操作系统微软

2010-04-07 09:28:29

Chrome缩略图

2023-06-21 15:37:07

微软Edge浏览器
点赞
收藏

51CTO技术栈公众号