2012-02-22 132 views
22

如何更改此确切代码以对mouseover执行悬停效果?更改悬停图像

我尝试了一些其他问题和答案,但我无法真正关注它们。

因此,HTML是:

现在我要做的是改变大尺寸图片时,将鼠标悬停在小图像
<a href="RR.html"><img src="R3.jpg" width=700 height=300 /></a> 

<div> 
    <a href="SSX.html"><img src="SSX.jpg" height=100 width=120 /></a> 
    <a href="MPreview.html"><img src="MaxPayne3Cover.jpg" height=100 width=120 /></a> 
    <a href="NC.html"><img src="NC.jpg" height=100 width=120 /></a> 
    </br> 
</div> 

+0

对不起,我说的是第二图像 – andrrs 2012-02-22 18:45:28

回答

19

请尝试下面的代码。它的工作

<!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>Untitled Document</title><br /> 
    </head> 

    <body> 
     <p> 
      <script type="text/javascript" language="javascript"> 
       function changeImage(img){ 
        document.getElementById('bigImage').src=img; 
       } 
      </script> 

      <img src="../Pictures/lightcircle.png" alt="" width="284" height="156" id="bigImage" /> 
      <p>&nbsp; </p> 
      <div> 
       <p> 
        <img src="../Pictures/lightcircle2.png" height=79 width=78 onmouseover="changeImage('../Pictures/lightcircle2.png')"/> 
       </p> 
       <p><img src="../Pictures/lightcircle.png" alt="" width="120" height="100" onmouseover="changeImage('../Pictures/lightcircle.png')"/></p> 

       <p><img src="../Pictures/lightcircle2.png" alt="" width="78" height="79" onmouseover="changeImage('../Pictures/lightcircle2.png')"/></p> 

       <p>&nbsp;</p> 
       </br> 
      </div> 
    </body> 
</html> 

我修改了代码,就像它会与它的一些延迟工作..但尽管如此,它不是动画..

function changeImage(img){ 
    // document.getElementById('bigImage').src=img; 
    setTimeout(function() {document.getElementById('bigImage').src=img;},1250); 
} 
+0

用户友好代码: – 2016-08-31 09:17:06

3
<script type="text/javascript"> 
    function changeImage(img){ 
     img.src=URL_TO_NEW_IMAGE; 
    } 
</script> 

<a href="RR.html"><img id="bigImage" 
         onmouseover="changeImage(document.getElementById('bigImage'));" 
         src="R3.jpg" 
         width=700 
         height=300/></a> 
<div> 
    <a href="SSX.html" ><img src="SSX.jpg" height=100 width=120/></a> 
    <a href="MPreview.html"><img src="MaxPayne3Cover.jpg" height=100 width=120/></a> 
    <a href="NC.html" ><img src="NC.jpg" height=100 width=120/></a> 
    </br> 
</div> 
+0

得到它。谢谢! – 2012-02-22 18:59:13

9

还是这样做:

<a href="SSX.html"> 
    <img src="SSX.jpg" 
     onmouseover="this.src='SSX2.jpg';" 
     onmouseout="this.src='SSX.jpg';" 
     height=100 
     width=120 /> 
</a> 

我觉得这是最简单的方法。

+0

是的,它的行为如此 – Asif 2012-02-23 02:47:53

48

试试这个它了这么容易和最短的一个,只是改变图像的URL:

<a href="TARGET URL GOES HERE"> 
    <img src="URL OF FIRST IMAGE GOES HERE" 
     onmouseover="this.src='URL OF SECOND IMAGE GOES HERE';" 
     onmouseout="this.src='URL OF FIRST IMAGE GOES HERE';"> 
    </img> 
</a> 
+0

真棒!谢谢:) – 2015-05-28 18:20:56

4

的翻滚图像或鼠标在图像的网页的最简单方式功能表的

<a href="#" onmouseover="document.myimage1.src='images/ipt_home2.png';" 
    onmouseout="document.myimage1.src='images/ipt_home1.png';"> 
    <img src="images/ipt_home1.png" name="myimage1" /> 
</a> 
3

如果你不想做Javascript,你可以使用CSS和:hover selector来获得相同的效果。

方法如下:

的index.html:

<!DOCTYPE HTML> 
<html> 
<head> 
<link rel="stylesheet" type="text/css" href="stylesheet.css" /> 
<title>Image hover example</title> 
</head> 
<body> 
<div class="change_img"></div> 
</body> 
</html> 

stylesheet.css中

.change_img { background-image:url('img.png'); }/*Normal Image*/ 
.change_img:hover { background-image:url('img_hover.png'); }/*On MouseOver*/ 
6

没有所需的JavaScript,如果你使用这种技术

<div class="effect"> 
<img class="image" src="image.jpg" /> 
<img class="image hover" src="image-hover.jpg" /> 
</div> 

的,你需要的CSS来控制它

.effect img.image{ 
/*type your css here which you want to use for both*/ 
} 
.effect:hover img.image{ 
display:none; 
} 
.effect img.hover{ 
display:none; 
} 
.effect:hover img.hover{ 
display:block; 
} 

记得给一些类股利和提到它在你的CSS名称,以避免麻烦:)

+0

现在你也可以使用Sprite CSS技术了,更多详细信息 – 2013-09-05 13:52:10

0

下面是一个简单的代码示例:

.change_img { 
    background-image: url(image1.jpg); 
} 
.change_img:hover { 
    background-image: url(image2.jpg); 
} 
2

只是用这样的:

例子:

<img src="http://nineplanets.org/planets.jpg" 
onmouseover="this.src='http://nineplanets.org/planetorder.JPG';" 
onmouseout="this.src='http://nineplanets.org/planets.jpg';"> 
</img> 

最适合与图片尺寸相同的图片。

复制此

<img src="IMG-1" 
onmouseover="this.src='IMG-2';" 
onmouseout="this.src='IMG-1';"> 
</img>