本文實例講述了js實現鼠標感應圖片展示的方法。分享給大家供大家參考。具體實現方法如下:
代碼如下:<!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(../images/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="/" title="觀賞該圖片詳細信息"><img src="/images/m01.jpg" alt="紅葉傳情" title="這裡是圖片標題" /></a>
<h5>紅葉傳情 By <a href="/" title="訪問代碼家園">代碼家園</a> 2010.09.23</h5>
</li>
<li class="hovereffect"><a href="/" title="觀賞該圖片詳細信息"><img src="/images/m02.jpg" alt="野花綻放" title="這裡是圖片標題" /></a>
<h5>野花綻放 By <a href="/" title="訪問珊珊影視在線">珊珊影視在線</a> 2010.09.23</h5>
</li>
<li class="hovereffect"><a href="/" title="觀賞該圖片詳細信息"><img src="/images/m03.jpg" alt="往事如茶" title="這裡是圖片標題" /></a>
<h5>往事如茶 By <a href="/" title="訪問代碼家園">代碼家園</a> 2010.09.23</h5>
</li>
<li class="hovereffect"><a href="/" title="觀賞該圖片詳細信息"><img src="/images/m04.jpg" alt="油菜花開" title="這裡是圖片標題" /></a>
<h5>油菜花開 By <a href="/" title="訪問七彩影視">七彩影視</a> 2010.09.23</h5>
</li>
<li class="hovereffect"><a href="/" title="觀賞該圖片詳細信息"><img src="/images/m05.jpg" alt="玫瑰情思" title="這裡是圖片標題" /></a>
<h5>玫瑰情思 By <a href="/" title="訪問代碼家園">代碼家園</a> 2010.09.23</h5>
</li>
<li class="hovereffect"><a href="/" title="觀賞該圖片詳細信息"><img src="/images/m09.jpg" alt="小雛菊" title="這裡是圖片標題" /></a>
<h5>小雛菊 By <a href="/" title="訪問珊珊影視在線">珊珊影視</a> 2010.09.23</h5>
</li>
</ul>
</div>
</body>
</html>
希望本文所述對大家的javascript程序設計有所幫助。