Utilizing a mouse-over script for image popup alignment

I recently came across the mouse over script below on a website, and I must say it works really well and is very easy to use. The only issue I have with it is the placement of the image. Currently, the image follows the cursor as it moves.

Question: Is there a way to make the image appear centered on the screen and fixed in that position while hovering over the text?

Here is the script:

    var offsetX = 180;
    var offsetY = -25;
    $('a').hover(function(e) {
    var href = $(this).attr('href');    
$('<img id="largeImage" src="' + href + '" alt="big image" />')
.css('top', e.pageY + offsetY)
.css('left', e.pageX + offsetX)
.appendTo('body');
}, function() {
$('#largeImage').remove();
});

$('a').mousemove(function(e) {
$("#largeImage").css('top', e.pageY + offsetY).css('left', e.pageX + offsetX);
}); 

This is the CSS style:

    #largeImage {
position:absolute;
padding: .5em;
background: #e3e3e3;
border: 1px solid #BFBFBF;
color:#aa1416;
}

HTML:

    <div class="largeImage">
    <li><h2><a id="item1"href="images/image6a.png">Breakfast Plates .....</a></h2>
    </div>

Answer №1

$('a').hover(function(e) {
  var link = $(this).attr('href');
  $('<img id="largeImage" src="' + link + '" alt="big picture" />')
    .appendTo('body');
}, function() {
  $('#largeImage').remove();
});

$('a').mousemove(function(e) {});
#largeImage {
  position: fixed;
  padding: .5em;
  background: #e3e3e3;
  border: 1px solid #BFBFBF;
  color: #aa1416;
  left: 50%;
  top: 50%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="largeImage">
  <li>
    <h2><a id="item1"href="images/image6a.png">Breakfast Plates .....</a></h2>
</div>

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

Tips for concealing the check mark within a checkbox upon selection

I have checkboxes arranged in a table with 23 columns and 7 rows. My goal is to style the checkboxes in a way that hides the check mark when selected. I also want to change the background image of the checkbox to fill it with color when marked. Can someone ...

Capturing click events on a stacked area chart with nvd3

Could someone assist me in capturing the click event on a nvd3 stacked area chart? I have successfully managed to capture tooltip show and hide events but am struggling with the click event. How can I obtain information about the clicked point? Please help ...

A fading transparency box on the border

Looking for a box with varying opacity from left to right (See image for reference - the red marked box has decreasing opacity from right to left). Here is my attempt: .waterMark { background-color: #FFFFFF; float: right; opacity: 0.6; position: ...

Trouble with flex wrap in my design template using Material UI

How can I create a responsive template using flexbox wrapping method? <Box sx={{ padding: 0, margin: 0, listStyle: "none", display: "flex", flexFlow: ...

The JSON output is not displaying correctly

I've been attempting to utilize JSON.stringify in order to format my json object for better readability. However, it doesn't seem to be working as expected. Can someone please offer assistance in identifying what I might have done incorrectly? ...

Converting a jQuery DOM element into a string representation

Currently, I am working with a textarea that contains the contents of an HTML file. This textarea includes all elements of my HTML page such as doctype, head, html, etc. My goal is to save the content of the textarea into a DOM variable using $.parseHTML: ...

How to retrieve the URL of a link using Jquery upon clicking

On one of my web pages, there is a link labeled N0016 jQuery(".ms-listlink").click(function() { alert($("a", this).attr("href")); }); However, I am encountering difficulties in triggering the alert. [<div class=​"ms-vb ms-vb-menuPadding itx" ctx ...

How can I include a close/ok button at the bottom of a bootstrap multiselect component?

Currently, I am utilizing the Bootstrap multiselect tool to filter query outcomes on a specific page. After selecting one or multiple options, my goal is to have a "close" or "OK" button visible at the bottom of the selection list. Upon clicking this butto ...

Extracting information from a database (HTML, JavaScript)

I am currently working on a table that receives data from the server using ajax: $.each(data, function(i, item) { $('#MyTable tbody').append("<tr>" +"<td>" +data[i].A+ "</td><td>" +d ...

Node.js offers a simple and effective way to redirect users to another page after they have

I am experiencing an issue when trying to redirect the client to the confirm page after a successful login process. I keep encountering some errors. view screenshot router.post('/sign_in', urlend, function(req, res) { var email = req.body.user ...

JavaScript double-click functionality is not operational on IE9 and IE11 browsers

My JavaScript code has an issue where single-click opens a link in a new tab and double-click opens a lightbox. This works perfectly in all browsers except for IE9 and IE11. In my initial code, both single-click and double-click function correctly. However ...

Efficiently refresh several DIV elements with a single JSON request

After extensive searching, I've come to the conclusion that due to my limited proficiency in javascript, I need some help. The issue at hand is with a json format output produced by an ASP page on a separate webserver, which looks something like this: ...

What is the best way to organize a form's checkboxes into an array using Node.js?

My goal is to design a form with multiple checkboxes where each checkbox has the same name, and only the selected values are submitted with the form. While I know how to achieve this using PHP as demonstrated in this SO question, I am facing difficulty imp ...

What could be causing the overlap of my input box with its parent container?

Here is the code snippet I am working with: <section className="createTodo-box"> // parent <div className="createTodo-inside"> // child <Input value={this.state.text} style={{ width: '200px' }} ...

obtaining attribute values from chosen items in a multiselect dropdown menu

I am using jQuery to create a multi-select element. For instance, consider the following example: <select multiple id="multiple"> <option value="1" type="Alice">Option 1</option> <option value="2" type="Bob">Option 2</o ...

Error with setting innerHTML property of a null nested div

This issue arises when the element is referenced before the DOM has completely loaded. To combat this, I have ensured that the necessary script runs only after the window has finished loading. Interestingly, if the getElementById() call is for a standalon ...

Troubleshooting problems with local references in an Angular date picker

I am currently working with an Angular date picker component and trying to access its values using a local reference. Unfortunately, when I attempt to console log the local reference, it returns undefined. The datepicker, function, and trigger are provid ...

Implementing bbcode feature in TinyMCE

Take a look at the custom html code provided below: <button type="button" class="btn btn-default btn-sm" onclick="appendBBCode('youtube','ContentArea')">[youtube]</button> <br> <div class="form-group field-boats-co ...

How to send information to a modal component in ReactJS?

I'm feeling a bit lost here, maybe I'm missing something. What I am trying to achieve is a loop that populates an array with progress bar elements and then displays them with the relevant information. When a user clicks on a progress bar, the d ...

execute a function for every regex match found within a string

When working with WordPress or PHP in general, I came across this interesting recommendation for email obfuscation, and I would like to: Convert email addresses to mailto links Encode the mailto links using str_13() Decode them client-side using JavaScri ...