分享使用jQuery和CSS实现的一个超酷缩略图悬浮逼近效果
2012-01-09 23:00
1281 查看
在线演示 本地下载 今天我们为大家介绍一个使用jQuery实现的缩略图逼近效果。主要的想法是当鼠标接近缩略图后,当前的缩略图会放大,并且周围相邻的缩略图也会相应变大一些,当你移动鼠标时,会影响移动方向上的缩略图大小变化,具体效果请大家查看演示。你可以在这个网站http://porscheeveryday.com/ 看到这个效果的原型,这里我们使用jQuery实现了一个jQuery版本的基本效果,希望大家喜欢!在这个教程中,我们将使用James Padolsey的 jQuery Proximity plugin。
HTML标签
以下代码生成一个无序的缩略图并且添加相关图片描述:<ul id="pe-thumbs" class="pe-thumbs"> <li> <a href="#"> <img src="images/thumbs/1.jpg" /> <div class="pe-description"> <h3>Time</h3> <p>Since time, and his predestinated end</p> </div></a> </li> <li><!-- ... --></li> </ul>
CSS样式
以下定义了缩略图居中,并且添加背景图片使得图片产生透明度变化效果.pe-thumbs{ width: 900px; height: 400px; margin: 20px auto; position: relative; background: transparent url(../images/3.jpg) top center; border: 5px solid #fff; box-shadow: 0 1px 2px rgba(0,0,0,0.2); }
同时我们也使用一个RGBA的背景颜色添加一个小点缀到背景图片。
.pe-thumbs:before { content: ""; display: block; position: absolute; top: 0px; left: 0px; width: 100%; height: 100%; background: rgba(255,102,0,0.2); }
列表中的项目将会向左float,并且我们设置锚定和图片的相对位置:
.pe-thumbs li{ float: left; position: relative; } .pe-thumbs li a, .pe-thumbs li a img{ display: block; position: relative; }
每一个缩略图都初始100px并且透明度为0.2:
.pe-thumbs li a img{ width: 100px; opacity: 0.2; }
最后我们定义描述内容的样式:
.pe-description h3{ padding: 10px 10px 0px 10px; line-height: 20px; font-family: 'BebasNeueRegular', 'Arial Narrow', Arial, sans-serif; font-size: 22px; margin: 0px; } .pe-description p{ padding: 10px 0px; margin: 10px; font-size: 11px; font-style: italic; border-top: 1px solid rgba(255,255,255,0.3); }
Javascript代码
主要的想法是当鼠标悬浮后计算所有的描述容器大小和位置。主要依赖于缩略图的最大尺寸及其居于主要wrapper中的位置。例如,当缩略图接近边缘,我们就使得描述区域显示在缩略图左边然后我们将帮定逼近事件到图片。主要想法是根据鼠标位置来变化图片大小。一旦图片达到最大尺寸,我们设置z-index最高,因此位于最高层次,并且显示分开的描述。// list of thumbs var $list = $('#pe-thumbs'), // list's width and offset left. // this will be used to know the position of the description container listW = $list.width(), listL = $list.offset().left, // the images $elems = $list.find('img'), // the description containers $descrp = $list.find('div.pe-description'), // maxScale : maximum scale value the image will have // minOpacity / maxOpacity : minimum (set in the CSS) and maximum values for the image's opacity settings = { maxScale : 1.3, maxOpacity : 0.9, minOpacity : Number( $elems.css('opacity') ) }, init = function() { // minScale will be set in the CSS settings.minScale = _getScaleVal() || 1; // preload the images (thumbs) _loadImages( function() { _calcDescrp(); _initEvents(); }); }, // Get Value of CSS Scale through JavaScript: // http://css-tricks.com/get-value-of-css-rotation-through-javascript/ _getScaleVal= function() { var st = window.getComputedStyle($elems.get(0), null), tr = st.getPropertyValue("-webkit-transform") || st.getPropertyValue("-moz-transform") || st.getPropertyValue("-ms-transform") || st.getPropertyValue("-o-transform") || st.getPropertyValue("transform") || "fail..."; if( tr !== 'none' ) { var values = tr.split('(')[1].split(')')[0].split(','), a = values[0], b = values[1], c = values[2], d = values[3]; return Math.sqrt( a * a + b * b ); } }, // calculates the style values for the description containers, // based on the settings variable _calcDescrp = function() { $descrp.each( function(i) { var $el = $(this), $img = $el.prev(), img_w = $img.width(), img_h = $img.height(), img_n_w = settings.maxScale * img_w, img_n_h = settings.maxScale * img_h, space_t = ( img_n_h - img_h ) / 2, space_l = ( img_n_w - img_w ) / 2; $el.data( 'space_l', space_l ).css({ height : settings.maxScale * $el.height(), top : -space_t, left : img_n_w - space_l }); }); }, _initEvents = function() { $elems.on('proximity.Photo', { max: 80, throttle: 10, fireOutOfBounds : true }, function(event, proximity, distance) { var $el = $(this), $li = $el.closest('li'), $desc = $el.next(), scaleVal = proximity * ( settings.maxScale - settings.minScale ) + settings.minScale, scaleExp = 'scale(' + scaleVal + ')'; // change the z-index of the element once // it reaches the maximum scale value // also, show the description container if( scaleVal === settings.maxScale ) { $li.css( 'z-index', 1000 ); if( $desc.offset().left + $desc.width() > listL + listW ) { $desc.css( 'left', -$desc.width() - $desc.data( 'space_l' ) ); } $desc.fadeIn( 800 ); } else { $li.css( 'z-index', 1 ); $desc.stop(true,true).hide(); } $el.css({ '-webkit-transform' : scaleExp, '-moz-transform' : scaleExp, '-o-transform' : scaleExp, '-ms-transform' : scaleExp, 'transform' : scaleExp, 'opacity' : ( proximity * ( settings.maxOpacity - settings.minOpacity ) + settings.minOpacity ) }); }); }, _loadImages = function( callback ) { var loaded = 0, total = $elems.length; $elems.each( function(i) { var $el = $(this); $('<img>').load( function() { ++loaded; if( loaded === total ) callback.call(); }).attr( 'src', $el.attr('src') ); }); }; return { init : init };
相关文章推荐
- 分享使用jQuery和CSS实现的一个超酷缩略图悬浮逼近效果
- 分享使用jQuery和CSS实现的一个超酷缩略图悬浮逼近效果
- 使用HTML5和jQuery插件Reel实现一个超酷的星际争霸2兵种动画360度预览效果
- 使用HTML5和jQuery插件Reel实现一个超酷的星际争霸2兵种动画360度预览效果
- 使用HTML5和jQuery插件Quicksand实现一个超酷的星际争霸2兵种分类展示效果
- 使用HTML5和jQuery插件Quicksand实现一个超酷的星际争霸2兵种分类展示效果
- 使用HTML5和jQuery插件Quicksand实现一个超酷的星际争霸2兵种分类展示效果
- 分享一个CSS3和jQuery实现的模糊显示效果 - 帮助你的访问用户更好的阅读内容
- 分享Css3设计---纯Css实现超酷的iphone玻璃气泡效果
- 分享一个HTML5画布实现的超酷文字弹跳球效果
- 分享Css3设计---纯Css实现超酷的iphone玻璃气泡效果
- 使用jQuery插件realshadow实现超酷真实阴影效果
- 使用jQuery插件realshadow实现超酷真实阴影效果
- CSS+jQuery实现的一个放大缩小动画效果
- 分享一个CSS3和jQuery实现的模糊显示效果 - 帮助你的访问用户更好的阅读内容
- 使用jQuery和CSS3实现的“加载中”效果按钮 极客标签 - 做最棒的极客知识分享平台
- 分享一个jquery插件:实现类似淘宝上鼠标经过图片右侧显示大图的效果
- 一个CSS+jQuery实现的放大缩小动画效果
- 使用jQuery开发一个超酷的倒计时效果