1

嗨,我正在为我目前正在开发的网站尝试简单的翻转图像。当我在Firefox中测试效果时,鼠标悬停效果会移动网页上的其他元素。对于如何解决这个问题,有任何的建议吗?

<head>
<script type="text/javascript">
if (document.images) {
    img1 = new Image();
    img1.src = "training2.png";
} </script></head>

<body>
<script type="text/javascript">
function rollover(name, filename)
{
    var fullpath = '' + filename;
    document.images[name].src = fullpath; }
    </script>
<div id="trainingbutton"><a href="#" onmouseover="rollover('button1','training2.png');" onmouseout="rollover('button1','training.png')">
<img src="training.png" name="button1" border="0" alt="training"  /></a></div></body>
4

2 回答 2

1

我不会使用 javascript 来翻转图像,只需创建一个包含正常状态和悬停状态的精灵,然后使用 css 的悬停属性可以移动背景位置。

像这样的东西:

<a class="rollover">Link</a>

然后是风格:

a.rollover
{
    background-image:url('/images/background.png');
    background-position:top left;
}

a.rollover:hover
{
    background-position:bottom left;
}

此方法也不会因在您翻转时必须下载图像而受到影响,因为它是作为初始请求的一部分下载的

于 2012-04-04T16:28:54.113 回答
1

正如前一个人回答的那样;精灵是要走的路。它负责必须预加载“悬停”图像;它是纯 CSS。

示例(图像链接翻转):

HTML:

<a href="#" class="mysprite">Join Newsletter</a>

将非悬停和悬停状态图像放在一个画布上。如果每个“图像”高 20 像素,则堆叠的两个将是 40 像素。因此,要显示底部图像,您只需背景向上移动 20px(因此在下面的悬停状态下为 -20px)

CSS:

.mysprite {
    display: block;
    width: 100px;
    height: 20px;
    background: url('mysprite.png') no-repeat;
    background-position: 0 0; /* technically not neccesary, but illustrates the example */
    text-indent: -9999px; /* eliminates the link text so only the background image shows */
    font: 0px; /* eliminates IE displaying the text despite text-indent */

}

.mysprite:hover {
   background-position: 0 -20px;
}

从本质上讲,您创建了一个“蒙版”,您可以从中移动下面的背景图像以显示您想要的内容。

于 2012-04-04T16:40:58.073 回答