用CSS+JavaScript制作的网页图片展示特效,具体用在什么地方你自己定吧!
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>鼠标感应的网页图片展示特效</title> <!--[if lte IE 6]> <script language="Javascript"> var W3CDOM = (document.createElement() && document.getElementsByTagName()); window.onload = pinballEffect; function pinballEffect() { if (!W3CDOM) return; var allElements = document.getElementsByTagName('*'); var originalBackgrounds=new Array(); for (var i=0; i<allElements.length; i++) { if (allElements[i].className.indexOf('hovereffect') >= 0) { allElements[i].onmouseover = mouseGoesOver; allElements[i].onmouseout = mouseGoesOut; } } } function mouseGoesOver() { originalClassNameString = this.className; this.className += " lay-on"; } function mouseGoesOut() { this.className = originalClassNameString; } pinballEffect(); </script> <![endif]--> <style type="text/css"> body { background:#fff; font:small/1.5 "宋体", SimSun, serif; _font-size:medium; } a img { border:none; } ul, li, h5 { list-style:none inside; margin:0; padding:0; } .recomm { background:#999; border:1px solid #666; width:600px; height:170px; overflow:hidden; padding:10px; margin:0 auto; position:relative; } .recomm ul { border:1px solid #fff; border-left:none; height:168px; width:599px; overflow:hidden; *position:absolute; /* 解决IE中overflow:hidden无法正确隐藏元素的问题 */ } .recomm li { float:left; position:relative; margin-right:-179px; height:100%; overflow:hidden; white-space:nowrap; text-align:center; } .recomm li img { display:block; border-left:1px solid #fff; width:248px; height:168px; } .recomm li h5 { position:absolute; bottom:0; left:0; height:20px; width:239px; line-height:20px; background:url(http://www.webjx.com/files/090331/1_211621.png) no-repeat; display:none; text-align:right; padding-right:10px; font-size:1em; font-weight:normal; } .recomm li:hover, .recomm .lay-on { width:249px; margin-right:0; } .recomm li:hover h5, .recomm .lay-on h5 { display:block; } </style> </head> <body> <div class="recomm"> <ul> <li class="hovereffect"><a href="http://www.webjx.com/" title="观赏该图片详细信息"><img src="http://www.webjx.com/files/090331/1_211515.jpg" alt="图片1" title="这里是图片标题" /></a> <h5>By <a href="http://www.webjx.com/" title="访问中国互联网络资讯网">中国互联网络资讯网</a> 3-31-09 Beijing</h5> </li> <li class="hovereffect"><a href="http://www.webjx.com/" title="观赏该图片详细信息"><img src="http://www.webjx.com/files/090331/1_211525.jpg" alt="图片2" title="这里是图片标题" /></a> <h5>By <a href="http://www.webjx.com/" title="访问中国互联网络资讯网">中国互联网络资讯网</a> 3-31-09 Beijing</h5> </li> <li class="hovereffect"><a href="http://www.webjx.com/" title="观赏该图片详细信息"><img src="http://www.webjx.com/files/090331/1_211533.jpg" alt="图片3" title="这里是图片标题" /></a> <h5>By <a href="http://www.webjx.com/" title="访问中国互联网络资讯网">中国互联网络资讯网</a> 3-31-09 Beijing</h5> </li> <li class="hovereffect"><a href="http://www.webjx.com/" title="观赏该图片详细信息"><img src="http://www.webjx.com/files/090331/1_211543.jpg" alt="图片4" title="这里是图片标题" /></a> <h5>By <a href="http://www.webjx.com/" title="访问中国互联网络资讯网">中国互联网络资讯网</a> 3-31-09 Beijing</h5> </li> <li class="hovereffect"><a href="http://www.webjx.com/" title="观赏该图片详细信息"><img src="http://www.webjx.com/files/090331/1_211553.jpg" alt="图片5" title="这里是图片标题" /></a> <h5>By <a href="http://www.webjx.com/" title="访问中国互联网络资讯网">中国互联网络资讯网</a> 3-31-09 Beijing</h5> </li> <li class="hovereffect"><a href="http://www.webjx.com/" title="观赏该图片详细信息"><img src="http://www.webjx.com/files/090331/1_211603.jpg" alt="图片6" title="这里是图片标题" /></a> <h5>By <a href="http://www.webjx.com/" title="访问中国互联网络资讯网">中国互联网络资讯网</a> 3-31-09 Beijing</h5> </li> </ul> </div><!--/.recomm--> </body> </html>
[Ctrl+A 全部选择 提示:你可先修改部分代码,再按运行]
介绍下制作的几个重点:
1. 图片横排
1) 将li左浮动(float:left),可以使图片横排。
2) 利用li的负右边距(margin-right的负值),使后面的向左移动叠加在前面的li之上。
.recomm li {float:left;margin-right:-179px;overflow:hidden;white-space:nowrap;text-align:center;}
当鼠标指向图片时(li:hover),设定li的右边距为0,则将后面的图片挤向右边。
.recomm li:hover, .recomm .lay-on {width:249px;margin-right:0;}
2. 边框
图片有1px的白色边框,但是需要注意的是,左右相邻的2个图片之间的边框只有1px,右边框是后面图片的左边框。因此实现的思路是:上/下/右边框用ul的边框来实现,而左边框由图片的边框实现。
.recomm ul {border:1px solid #fff;border-left:none;}.recomm li img {display:block;border-left:1px solid #fff;}
3. 尺寸的计算
此处最容易糊涂的地方是边框与宽度。
图片包括边框,如果设计的宽度为250px,那么图片的实际宽度应为248px(250px-2px)。(高度同理)由于ul有1px的右边框,因此ul的CSS设定宽度为设计宽度-1px,本例中为599px(600px-1px)。
.recomm ul {……height:168px;width:599px;}.recomm li img {……width:248px;height:168px;}
4. IE内的问题
在IE内,虽然对ul设定了overflow:hidden,但是会出现如图5所示的情况。因此,需要对ul设定position:absolute,以使其能正确隐藏溢出的内容。
5. h5的显示与隐藏
h5内是图片的简要说明,默认状态不显示,当鼠标指向图片的时候会显示出来。
h5是叠加在图片上的,因此使用绝对定位来控制其位置,同时,为了使其相对于li绝对定位,因此给li设定相对定位以产生包含块。
.recomm li h5 {position:absolute;bottom:0;left:0;……}.recomm li:hover h5, .recomm .lay-on h5 {display:block;}