利用jQuery制作具有滑动动画效果的层
基本原理
这些具有动态效果的滑动盒都基于同样的基本原理。在你经过想要"窥见"对象中的其他两个项目,这个带有".boxgrid"的DIV标签充当着一个窗口。还不明白? 让这个图片来给你线索吧:
理解了这个基本原理之后,我们就可以利用滑动元素的动画效果来揭开或遮盖住要展示的区域,以此来创造滑动效果。
你可能需要的东西: 预览 | 下载个实例的源文件
第一步 – CSS 基础工作
在上面给出基本结构的启示图中,我们需要使用一点CSS来让它显示出预期的效果。下面这个CSS定义了查看窗口(.boxgrid) 并的在LEFT和TOP设定图片的默认POSITION,这对于滑动时的重叠交代很重要。并且不要忘记overflow:hidden将使这一切成为可能。
.boxgrid{ width: 325px; height: 260px; margin:10px; float:left; background:#161613; border: solid 2px #8399AF; overflow: hidden; position: relative; } .boxgrid img{ position: absolute; top: 0; left: 0; border: 0; } .boxcaption{ float: left; position: absolute; background: #000; height: 100px; width: 100%; opacity: .8; /* For IE 5-7 */ filter: progid:DXImageTransform.Microsoft.Alpha(Opacity=80); /* For IE 8 */ -MS-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=80)"; } .captionfull .boxcaption { top: 260; left: 0; } .caption .boxcaption { top: 220; left: 0; } $(document).ready(function(){ //要更改上、下方向和左、右方向,只需要在top/left的值中添加"-"号(表示反方面)。 //垂直滑动 $('.boxgrid.slidedown').hover(function(){ $(".cover", this).stop().animate({top:'-260px'},{queue:false,duration:300}); }, function() { $(".cover", this).stop().animate({top:'0px'},{queue:false,duration:300}); }); //水平没去 $('.boxgrid.slideright').hover(function(){ $(".cover", this).stop().animate({left:'325px'},{queue:false,duration:300}); }, function() { $(".cover", this).stop().animate({left:'0px'},{queue:false,duration:300}); }); //比例缩放滑动 $('.boxgrid.thecombo').hover(function(){ $(".cover", this).stop().animate({top:'260px', left:'325px'},{queue:false,duration:300}); }, function() { $(".cover", this).stop().animate({top:'0px', left:'0px'},{queue:false,duration:300}); }); //部分滑动 (只显示一部分背景) $('.boxgrid.peek').hover(function(){ $(".cover", this).stop().animate({top:'90px'},{queue:false,duration:160}); }, function() { $(".cover", this).stop().animate({top:'0px'},{queue:false,duration:160}); }); //完全滑动的说明 (从完全隐藏到完全显示) $('.boxgrid.captionfull').hover(function(){ $(".cover", this).stop().animate({top:'160px'},{queue:false,duration:160}); }, function() { $(".cover", this).stop().animate({top:'260px'},{queue:false,duration:160}); }); //部分滑动的说明 (部分显示-部分隐藏) $('.boxgrid.caption').hover(function(){ $(".cover", this).stop().animate({top:'160px'},{queue:false,duration:160}); }, function() { $(".cover", this).stop().animate({top:'220px'},{queue:false,duration:160}); }); }); <div target="_BLANK">More Work</a></p> </div> </div>