Transform a text hyperlink into an image by hovering over it

Is there a way to turn a text hyperlink into an image when hovered over?

I've seen image rollovers, but I'm not sure how to code a text-to-image rollover.

I'm unsure where to start and which programming language to use—JavaScript, PHP, or jQuery?

I began with this code snippet:

<a href="#" onmouseover="(document.img.src)='SAM_2251.jpg';">Mouseover here</a>

<img name="img" alt="" border="0" />

However, this code displays the text along with the loaded image. I want the text to disappear completely once the image loads.

Any assistance would be greatly appreciated. Thank you!

Answer №1

Here is a simple way to achieve this effect using only HTML and CSS with a background image:

HTML

<a href="#" class="hover_image">Hover over me</a>

CSS

a.hover_image:hover {
    background: url(/path/to/image) no-repeat center center;
    text-indent: -9999em;
}

You may need additional CSS to set the width and height of the a tag, but this should give you a good starting point.

Answer №2

There are numerous methods to achieve this, one way is by using a CSS example like the one below:

give it a shot

Answer №3

Transforming a link into an image upon hover, changing back to text when not hovered (using only CSS)

<style>
.changeable img
{
  display:none;
}
.changeable:hover span
{
  display:none;
}
.changeable:hover img
{
  display:inline-block;
}
</style>
<a href="http://www.example.com" class="changeable"><span>Hyper Text</span><img src="img.png" /></a>

If you prefer the link to permanently switch to an image (utilizing jQuery)

<style>
.changeable img
{
  display:none;
}
</style>
<a href="http://www.example.com" class="changeable"><span>Hyper Text</span><img src="img.png" /></a>

<script type="text/javascript">
$('.name').hover(function(){
  $(this).children('img').show();
  $(this).children('span').hide();
})
</script>

Answer №4

Below is a simple JavaScript implementation:


    <div id="link">    
        <a href="#" onmouseover="document.getElementById('link').style.display='none'; document.getElementById('hoverImage').style.display='block';">Mouseover here</a>  
    </div>
    <div id="hoverImage" style="display:none">
        <img name="img" alt="" border="0" src="img.JPG" onmouseout="document.getElementById('link').style.display='block'; document.getElementById('hoverImage').style.display='none';"/>    
    </div>

Answer №5

Suppose we have HTML code that looks like the example below:

<a href="http://www.example.com/image.png" class="textToImg">Some text</a>

This JavaScript code should be functional for converting text to an image:

function changeTextToImage(element){
    if (!element) {
        return false;
    }
    else {
        var img = document.createElement('img');
        img.src = element.href;
        element.removeChild(element.firstChild);
        element.appendChild(img);
    }
}

Check out this JS Fiddle link for a demonstration.

Answer №6

One way to achieve this effect is through the use of jQuery:

View Demo

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
</head>

<body>
<script type='text/javascript'>
$( function() {
  $("#imglink").hover(
    function () {
      $(this).attr('small',$(this).html());
      $(this).html($(this).attr('full'));
    },
    function () {
       $(this).html($(this).attr('small'));
    }
  );
});
</script>

<a herf="" id='imglink' full='<img border="0" src="someImage.png" width="163" height="37"/>'>Hover for image, rollout for text</a>

</body>
</html>

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

Initializng an array with null values in Javascript

Can you explain why this Javascript code: var myArray = new Array(3); does not produce the same result as: var otherArray = [null, null, null]; ? Keep in mind that (myArray == otherArray) returns false. Additionally, is there a way to create an ...

Creating custom ExpectedConditions with Protractor for detecting attribute changes

I've been working on creating a custom ExpectedConditions method that can wait for an element attribute to change. Here is the approach I came up with: const CustomExpectedCondition = function() { /** * Check if element's attribute matches ...

The Mongoose framework has initiated a request to access the nested schema

I am puzzled as to why my database is not updating with my requests. I have a schema with nested objects, and I am trying to send a request to update a specific object within another object. The response indicates success, but nothing actually gets added t ...

missing method in HTML/JavaScript

Just experimenting with functions and time... The outcome is showing as undefined on the live server. However, if I execute the JavaScript code separately, the correct greetings display. Why is this happening? Is there a solution to rectify it? const tod ...

Unveiling the Technique: Adjusting Field Visibility When Dropdown is Altered

I tried to find a solution on Stackoverflow for displaying/hiding a field based on dropdown selection using either jQuery or inline JavaScript. However, I am facing difficulties when implementing this within a table. Let's start with an easy approach ...

Knowing when to use a comma or a space in HTML attributes is crucial for proper formatting

<frameset cols="25%,75%"></frameset> <!-- separating columns with a comma --> <input type="text" form="first second three" /> <!-- forming the input form attribute with spaces --> <input type="file" accept="image/gif, imag ...

Retrieve the Title from Tiles Option

Our automation team has requested that we hide the page names in a field on our page to assist them in verifying proper navigation. We rely on Apache tiles to build our pages using a layout of header, body, and footer, with most content residing in the bo ...

Located at the lower section of the parent container

In my flex container, I have two boxes arranged side by side (collapsing into one column on smaller screens). I've ensured that these boxes have the same height when displayed together, as shown in the image below: https://i.sstatic.net/ZjsfW.png No ...

Steps for including a subdocument within a mongoose schema

I am currently working on setting up a subdocument within a mongoose schema using node.js/Express. There are two schemas in play: Member and Address Member.js // app/models/member.js // Loading mongoose to define the model var mongoose = require(' ...

Maintain component state in a React app when the page is re

After navigating to a specific page by passing props, I need the ability to reload the page without losing its state. Currently, when I try to refresh the page, an error is thrown indicating that the prop is missing. For instance, if I use history.push({ ...

Using AngularJS to convert a JSON object into an array

Hey there, I've got a JSON return object that looks like this: color_selected = [ { id: 4}, { id: 3} ]; Any tips on how I can convert it to the following format? color_selected = [4,3] Appreciate any help or s ...

Leveraging the power of react routes for efficient navigation within a react-based application

I am currently facing an issue with setting up routes for a basic react application using react-router. The given routes don't seem to match and the switch defaults to displaying the 404 page. Below is the code for the routes: import { BrowserRout ...

Consistent max-width applied to various items using full HTML and CSS styling

Can a CSS class be created where the width is dynamically set to match the width of the longest item? I know how to do this in jQuery by iterating through, but I need a solution using only CSS/CSS3. ...

Decoding a serialized string into an object

I have a data string that is serialized in the format a=4&b=2&c=7. I am looking to convert this string into an object where each key-value pair is represented like this: { a:4, b:2, c:7 }. When I use serializeArray(), it only gives me an array with ...

The ng-click function ceases to trigger after the $location dependency is included in the controller

I need some help with running a ng-click function from my controller. The strange thing is, when I don't use the $location dependency, the ng-click function works fine. But as soon as I add $location to the controller, the ng-click function stops work ...

Error in HTML code when trying to incorporate a JavaScript file

I'm struggling with something seemingly simple - including a JavaScript file in my flat HTML page. Here is the content of my JavaScript file: <script> $(document).ready(function() { $('div').css('background', 'red& ...

JQuery allows for multiple drag and drop functionalities, including events that occur after an object

I'm currently working on a .php page where I need to implement a drag-and-drop functionality. There will be 3 draggable elements that must be placed in their corresponding droppable elements - each draggable can only be dropped in a specific droppable ...

fuzzy lettering on specific edges of three-dimensional cube

I've been attempting to design a cube with text on all its sides, but I've noticed that some of the sides display blurry text. Upon conducting a quick search online, I discovered that adding -webkit-font-smoothing: subpixel-antialiased to the con ...

Tips for setting up my bot to pull random messages from a channel and send one when an autoresponse is activated

As per the headline, I attempted to utilize fetchMessages in order to monitor the messages being sent in a specific channel. However, when I experimented with this method, it simply generated never-ending blocks of data in the console. Upon inspection, I ...

Rearrange elements with a custom breakpoint trigger

Having four items with varying sizes lined up in a row can present some challenges. While larger screens may display them perfectly, issues arise on large screens (1199.98px/bootstrap 'lg') that require re-ordering of the div items. Specifically ...