超简单---鼠标滑过变换图片

Posted on 2009-05-28 20:07  萧木柯  阅读(9028)  评论(5编辑  收藏  举报

      鼠标滑过,图片变换,非常实用,用JS可以很容易做到,但是JS完成后有个缺陷,在滑过的时候,第一次可能是空白的,然后才显示,因为要下载图片,网速慢的时候更加明显。其实用CSS实现更容易,而且不会出现上面的短暂显示空白的小问题。 

CSS实现例子:

<!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=gb2312" />
<title>CSS实现鼠标滑过变换图片</title>
<style>
.image10 a{
 background: url(../indeximages/computer.jpg) no-repeat top left;
    border: none;
    width: 99px;
    height: 45px;
    display: block;
 float:left;
 margin: 15px 3px;
}
.image10 a:hover{
 background: url(../indeximages/computer.jpg) no-repeat bottom left;
 border: none;
 width: 99px;
 height: 45px;
 display: block;
 float:left;
 margin: 15px 3px;
}

</style>
</head>

<body>
<div class = image10><a href="/carcount/count.htm"></a></div>
<div class = image10><a href="/compare.asp"></a></div>
<div class = image10><a href="/compare.asp"></a></div>
</body>
</html>

其实就是利用了A的hover属性,来改变背景图片的平铺位置。top left 和bottom left,就这么简单就完成了。如果想每个图片都不一样,变换的页不一样,定义成ID就可以了。