jQuery实现图片与文本信息切换动画效果
原创文章 作者:月光光 2010年12月24日 14:26helloweba.com 标签:jQuery
jQuery可以实现简单的动画效果,在本文中,当您用鼠标滑向图片时,图片开始滑动,并切换成图片的文本信息,当鼠标离开时,图片又还原成原样。

HTML
<div class="wrap">
<img src="images/s1.jpg" alt="photo" />
<h3>图片标题</h3>
<p>图片说明文字</p>
<p><a href="#">查看详情</a></p>
</div>
...
我创建了一个DIV.wrap,里面放置了一张图片,以及文字部分。然后在页面中创建多个同样的DIV.wrap,使用CSSK控制DIV.wrap的外观。
CSS
.wrap{position:relative; float:left; width: 200px; height:200px; margin:5px;
background:#e8f5fe; overflow:hidden; cursor:pointer;}
.wrap img{position:absolute; top:0; left:0}
设置.wrap向左浮动,并给定宽度和高度,这个宽度和高度正好是要展示的图片的宽度和高度。然后注意设置overflow:hidden,这样的话.wrap中超出的文字部分会被隐藏。
注意还要设置图片的位置为absolute。
jQuery
在使用jQuery之前一定要先加载jQuery库。
$(function(){
$(".wrap").hover(function(){
$(this).children("img").stop().animate({
top:"-200px"
},500);
},function(){
$(this).children().stop("img").animate({
top:"0"
},500);
});
});
我们使用jQuery侦听用户的鼠标行为。当用户鼠标滑向DIV.wrap时,告诉DIV里的图片向上滑动,滑动的距离是200px正是图片和DIV.wrap的高度。同样当鼠标离开时,图片还原。
注意:stop()是指停止所有的动画。如果不知道图片的高度,可以使用outerHeight()获取高度。
声明:本文为原创文章,helloweba.net和作者拥有版权,如需转载,请注明来源于helloweba.net并保留原文链接:https://www.helloweba.net/javascript/95.html

共0条评论