Swap the image when hovering over it

Is it possible to modify this specific code so that a hover effect is triggered on mouseover?

I attempted to look into similar questions and answers, but I found them difficult to understand.

Here is the HTML snippet:

<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>

The goal now is to swap out the large image when hovering over the smaller images with the mouse.

Answer №1

Give this a try, it's super simple and the most efficient method, just update the Image's URL:

<a href="REPLACE WITH TARGET URL">
    <img src="INSERT URL OF FIRST IMAGE"
         onmouseover="this.src='CHANGE TO URL OF SECOND IMAGE';"
         onmouseout="this.src='BACK TO URL OF FIRST IMAGE';">
</a>

Answer №2

Check out this code snippet below, it should function properly

<!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('big').src=img;
                }
            </script>

            <img src="../Images/small.png" alt="" width="200" height="100" id="big" />
            <p>&nbsp; </p>
            <div>
                <p>
                    <img src="../Images/big.png" height=150 width=120 onmouseover="changeImage('../Images/big.png')"/>
                </p>
                <p><img src="../Images/small.png" alt="" width="80" height="60" onmouseover="changeImage('../Images/small.png')"/></p>

                <p><img src="../Images/big.png" alt="" width="120" height="110" onmouseover="changeImage('../Images/big.png')"/></p>

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

I made some tweaks to the original code for a slight delay effect, however, the animation feature is still not functioning as intended.

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

Answer №3

Here's another option:

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

In my opinion, this method is the simplest.

Answer №4

No need for JavaScript when utilizing this method

<div class="style">
<img class="pic" src="photo.jpg" />
<img class="pic hover" src="photo-hover.jpg" />
</div>

CSS is essential to manage the effect

.style img.pic{
/*insert your desired CSS rules here*/
}
.style:hover img.pic{
display:none;
}
.style img.hover{
display:none;
}
.style:hover img.hover{
display:block;
}

Don't forget to assign a class to the div element and refer to it in your CSS naming conventions to avoid any issues :)

Answer №5

A simple method to create Roll Over images or Mouse Over effects for website menus

<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>

Answer №6

<script type="text/javascript">
    function switchImg(img){
       img.src=URL_TO_CHANGED_IMAGE;
    }
</script>

<a href="RR.html"><img id="bigImage"
                       onmouseover="switchImg(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>

Answer №7

If you're not interested in using Javascript, you can achieve the same effect by utilizing CSS along with the :hover selector.

Here is a simple example:

index.html:

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

styles.css

.image-container { background-image:url('img.png'); }/*Normal Image*/
.image-container:hover { background-image:url('img_hover.png'); }/*Hover Effect*/

Answer №8

Simply use the following code snippet:

For example:

<img src="http://example.com/image1.jpg" 
onmouseover="this.src='http://example.com/image2.jpg';"
onmouseout="this.src='http://example.com/image1.jpg';">
</img>

For best results, ensure that both images are of the same dimensions.

Just copy and paste this:

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

Answer №9

Check out this straightforward code snippet:

.modify_img {
    background-image: url(picture1.jpg);
}
.modify_img:hover {
    background-image: url(picture2.jpg);
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

jQuery wrapAll issue

I have a repeating group of three divs in my code that I need to wrap together. Here's an example from my HTML: <div class="one" /> <div class="two" /> <div class="three" /> <div class="one" /> <div class="two" /> <d ...

Shifting an HTML anchor to account for a stationary header - Safari compatibility with alternative CSS styling

I made adjustments to my HTML anchors by adding an offset in the CSS to accommodate for a fixed header in my theme: margin-top: -175px; padding-top: 175px; } /* Adjustments for screen sizes of 760px and smaller */ @media (max-width:760px){ #r ...

What is the best way to display two items side by side from a list of items in HTML?

I have a list of items that are displayed in `webView` with 4 elements per row. However, for `mobileView`, I would like to show only 2 elements in each row. Below is an example from my HTML file: <div class="row my-3 cust-heading" style="margin-left: 3 ...

Update the division by clicking the button with a randomly generated JavaScript string element

Trying to solve a unique problem here as none of the proposed solutions on similar questions have worked for me. Apologies if I'm missing something, I'm new at this. The issue is with loading an empty div on my page using javascript and strings ...

Utilizing Sass variables and customizing CSS styles within the main stylesheet

Hey there! I'm new to sass and I have a specific question about it. Do I need to link the scss file to a separate css file where I'm working, or can I write my styles directly in the same file? I tried using sass variables from Bootstrap 5.3.2, b ...

Is it possible to add file content to the beginning rather than the end?

Looking for help with my form that allows staff to submit messages to our updates page. I currently have this code: $filename = "files/armaupdates"; $text = $_POST['update']; $updatesep = "<hr><br><hr><br>"; $fp = fopen ...

Concealing just the portion of an element that moves underneath a fixed see-through navigation bar

After extensive searching for a solution, I have encountered multiple similar issues but none have provided a resolution that works for my specific case. It seems I may be overlooking something obvious. The problem at hand involves a fixed transparent nav ...

HTML scroll bar functioning intermittently

Here is my code snippet. table#projectTable > tbody , table#productTable > tbody{ height: 300px; overflow: auto; } The scrollbar is functional when clicking the top part, but not the bottom. It's quite odd. Upon usin ...

What is the best way to ensure my hover caption spans the entire size of the image?

I'm currently working on creating a hover caption that adjusts itself to fit the full width and height of an image even when it is resized. I attempted to implement a jQuery solution that I came across, but unfortunately, I am struggling to get it to ...

Problem with jQueryUI Sortable cancel property preventing input editing

Currently, I am utilizing jquery-3.2.1.min.js and jquery-ui.min.js version 1.12.1 The task at hand is to create a sortable list where certain elements are not sortable (specifically, other sortable lists). It is crucial that the input elements remain edit ...

Can file input buttons be custom styled?

Since I am using Material-UI, the traditional methods are not working for me. I have a form with two buttons positioned next to each other. One button is an "Upload File" input for users to upload a file, and the other is a submit button. I want both butto ...

Is it possible to simultaneously employ two asynchronous functions while handling two separate .json files?

Is it possible to work with 2 .json files simultaneously? My attempt did not succeed, so I am looking for an alternative method. If you have a suggestion or know the correct syntax to make this work, please share. And most importantly, does the second .j ...

Having trouble eliminating the underline on Vue router-link?

I've experimented with various approaches in an attempt to remove the underline from router-link. This is the code I have: <router-link :to="{name: 'Plan'}"> <div>Plan Your Trip</div> <div class=&apos ...

The orientation of the displayed images has turned vertical, breaking away from the traditional horizontal

My problem lies in trying to horizontally display the images fetched from the database. I attempted using "inline-block" but it proved ineffective. Despite looking for alternative solutions, none seemed to be a fit for my situation. $db = mysqli_connect(" ...

Using a Second List Element in CSS Image Rollover Map with jQuery

I have been utilizing this interactive map from Wolf's website, but I am interested in incorporating a secondary list of countries alongside the existing one as the base for the script. However, due to presentation reasons, the second menu cannot resi ...

What is the reason behind opting for ng-style over style in AngularJS for applying CSS formatting?

Recently, I've delved into AngularJS and have been exploring its depths for a few days now. I'm curious about the use of ng-style in styling components within both static and dynamic webpages. Given that we already have the traditional style tag ...

How to display a PDF in a web browser using PHP

I have the following code block: header('Content-type: application/pdf'); header('Content-Disposition: inline; filename="the.pdf"'); header('Content-Length: ' . filesize($file)); @readfile($file); It is working perfectly fin ...

CSS: Create a form with two input fields where the left field is adjustable in width and the right field fills up

------------- -------------------------------------------------- | some unique text | | some more original content, varying length | ------------- -------------------------------------------------- |<---------------------- 100% width ------------------ ...

Using jQuery to locate a JavaScript variable is a common practice in web development

I recently created a JSFiddle with buttons. As part of my journey to learn jQuery, I have been converting old JavaScript fiddles into jQuery implementations. However, I seem to be facing a challenge when it comes to converting the way JavaScript fetches an ...

Make sure to save the HTML content after we make an AJAX call to retrieve it

Is there a way to call an HTML file using Ajax and then save the response as an HTML file in a specific location? I have been trying to make an Ajax call using jQuery, like shown below: $.ajax({ type: "POST", url: "../../../project/html/T ...