首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > CSS >

Jquery跟CSS3实现图片鱼眼显示效果

2012-07-23 
Jquery和CSS3实现图片鱼眼显示效果?基本效果:鼠标移动到图片上时,放大图片同时将该图片周围的图片适当放大

Jquery和CSS3实现图片鱼眼显示效果

?基本效果:鼠标移动到图片上时,放大图片同时将该图片周围的图片适当放大,形成一个鱼眼效果,图片依次高亮显示。我们可以参看http://porscheeveryday.com/?这个swf的显示效果。为了用jquery实现该效果,我们将使用一个插件 jQuery Proximity plugin?作者 James Padolsey.

现在让我们一步步学习如何实现类似的鱼眼效果。首先我们构建主容器展示图片。采用ul无序列表方式来组织图片,代码如下
<ul id="pe-thumbs" />
???????????? <div class="pe-description"><h3>鱼眼效果</h3><p>美瑞网www.mxria.com</p></div></a></li>
???? <li><!-- ... --></li>
</ul>
主容器构建好后,我们接下来定义样式。CSS3代码如下,背景采用透明处理,同时给容器加上了阴影效果。

.pe-thumbs:before {???? content: "";???? display: block;???? position: absolute;???? top: 0px;???? left: 0px;???? width: 100%;???? height: 100%;???? background: rgba(255,102,0,0.2);?} 

为了增加更好的效果,我们使用:before伪元素增添了对比效应。这里出现了一个很特别的伪元素:rgba,这里对其简要说明。background在CSS3中得到了大力增强,背景透明效果如上面的代码中有用到,而rgba它的效果类似opacity,可以取得很风骚的半透明的效果,如果应用到合适的配色,效果很赞。最主要的区别是,它不会将该区块里头含有的文字也变成半透明,只是改变容器区块。

?? .pe-thumbs:before {
??? content: "";
??? display: block;
??? position: absolute;
??? top: 0px;
??? left: 0px;
??? width: 100%;
??? height: 100%;
??? background: rgba(255,102,0,0.2);
}

为每个图片项设置相应的样式,定义其位置、透明度。

?

.pe-thumbs li{ ????float: left; ????position: relative; } .pe-thumbs li a, .pe-thumbs li a img{ ????display: block; ????position: relative; }

其他部分略过,大家可以看源代码中的内容。下面来看看Javascript部分。

JS部分主要的思路就是当鼠标接近图片后,对利用hover效果来触发,对光标悬停的图片进行位置计算,并将其相连的图片进行对应的样式转换,其中核心的内容就是识别出主容器边缘的图片,并进行相应处理。代码如下:

?123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149????// 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 };

参考英文原文http://tympanus.net/codrops/2012/01/04/thumbnail-proximity-effect/?翻译时非直译,加入了自己的理解。 DEMO? 源代码下载?


作者:夜飞羽?来源:www.mxria.com ?时间:2012-02-27

热点排行