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

实例下载
你可以在这个网站http://porscheeveryday.com/ 看到这个效果的原型,这里我们使用jQuery实现了一个jQuery版本的基本效果,希望大家喜欢!
在这个教程中,我们将使用James Padolsey的 jQuery Proximity plugin。
HTML标签
以下代码生成一个无序的缩略图并且添加相关图片描述:
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);
 - $('
 ').load( function() {
- ++loaded;
 - if( loaded === total )
 - callback.call();
 - }).attr( 'src', $el.attr('src') );
 - });
 - };
 - return {
 - init : init
 - };
 
原文:http://www.cnblogs.com/gbin1/archive/2012/01/09/2317605.html
                本文名称:使用jQuery和CSS实现超酷缩略图悬浮逼近效果
                
                链接地址:http://www.csdahua.cn/qtweb/news19/475619.html
            
网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网