Display the item exactly where the cursor is located once the transform scale is implemented

How can I accurately determine the mouse position when a transform scale, such as transform: scale(1.6);, is applied? Currently, the popup element is displaying too far down and to the right in this scenario.

Check out the example here

$(".spanhover").hover(
  function (event) {
    var divid = "#popup" + $(this).attr("id");
    $(divid).css({ top: event.clientY, left: event.clientX }).show();
  },
  function () {
    var divid = "#popup" + $(this).attr("id");
    $(divid).hide();
  }
);
.spanhover {
    cursor: pointer;
}
.popup {
    position: absolute;
    display: none;
}

body {
   transform: scale(1.6);
   transform-origin: 0 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="popup" id="popuphover1" style="display:none">test1</div>
<div class="popup" id="popuphover2" style="display:none">test2</div>
<br><br>
<span class="spanhover" id="hover1">Mouse over</span>

Answer №1

Consider using offset position:

$(".spanhover").hover(
  function (event) {
    var divid = "#popup" + $(this).attr("id");
    $(divid).css({ top: event.offsetY, left: event.offsetX }).show();
  },
  function () {
    var divid = "#popup" + $(this).attr("id");
    $(divid).hide();
  }
);
.spanhover {
    cursor: pointer;
}
.popup {
    position: absolute;
    display: none;
}

body {
   transform: scale(1.6);
   transform-origin: 0 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="popup" id="popuphover1" style="display:none">test1</div>
<div class="popup" id="popuphover2" style="display:none">test2</div>
<br><br>
<span class="spanhover" id="hover1">Mouse over</span>

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

The concept of an undefined property

I encountered this error in my console: index.js:70 Uncaught TypeError: Cannot read property 'searchForm' of undefined at eval (index.js:70) at Module../src/js/index.js (bundle.js:4245) at __webpack_require__ (bundle.js:20) at e ...

Align a button to the left or right based on its position within the layout

On the left side, there is dynamic text and on the right side, a button is displayed as shown below: <div class="dynamic-text"> {{ dynamicText }} </div> <div class="some-button"> < ...

Tips for addressing color contrast accessibility problems with inactive buttons

An issue has arisen with a button that is conditionally disabled and appears greyed out. The background color is #e0e0e0 and the text color is #a6a6a6, resulting in a contrast ratio of 1.84, which falls short of the required 4.5 minimum. Is there a way to ...

Targeting child elements with CSS selectors

In my ecommerce software, I have a menu that I want to customize by making the main categories bold. I have attempted various methods, including the ones shown below, but I am struggling to target the correct elements (specifically those in capital letter ...

Changing the width of an HTML table does not produce any noticeable results

I attempted to create a striped table with 5 columns. The desired column sizes are: 10% width --- 10% width --- 40% width --- 20% width --- 20% width However, my current code is not achieving the correct widths (the 'description' column does n ...

In Firefox, event.preventDefault() suddenly ceased to function

The code I had been using was working perfectly fine until just recently. I noticed that event.preventDefault(); is not triggering in Firefox. The code still functions properly in Chrome, but in Firefox it redirects me to a blank page with generated code v ...

MongoDB is experiencing an issue where the latest document is not being stored at the

Storing comments for my webpage in MongoDB has been flawless so far. Whenever a new comment is saved, it gets added to the bottom of the collection. This results in the newest comment appearing at the top when loading comments, which is ideal. However, I e ...

What is the best way to generate a new Object using an Object that contains Arrays?

I currently have a global array saved with a catalog and a list of items that the user has saved. My task is to generate a new array of Objects (with arrays) containing only the items saved by the user. I am working with javascript in react-native, and I ...

Prevent code from freezing due to JavaScript errors within jQuery blockUI

Trying to make sense of things... We're utilizing the jQuery block UI plugin to block the UI. $(document).ajaxStart(function() { $.blockUI({ message: '<h4><i class="fa fa-circle-o-notch fa-spin fa-fw"></i> loading...< ...

What is the best way to make sure that only one tab changes color when clicked?

I have been working on a function that changes the color of a tab to gold when clicked, which is working fine. However, I also want to toggle the active property of each tab so that it displays a nice white outline. The problem I'm facing is that the ...

Picture escapes the div in a hurry

I mistakenly placed the yellow dot "gif1" outside of the black box "gif" instead of inside. How many errors can you spot in my code? Take a look at the Livewave Preview to see the current state of the design. I have attempted to fix this issue by using o ...

Catch 22: Initiating Script only when Dependent Element is Present

My aim is to selectively load JavaScript in the <head> section only if a specific element exists within the <body>. The issue I am facing revolves around the inclusion of a Web Component, which is essentially a large <script> sourced fro ...

How can I address the issue of width:100% truncating the content and causing a horizontal scrollbar in HTML/CSS?

I've searched high and low for a solution to this issue but have come up empty-handed every time. It's possible I just didn't search hard enough, but everything I've attempted so far has failed to work. This problem has been plaguing me ...

What is the best way to create a unique transition for every component?

I have three elements that I would like to display with a transition effect when they appear or disappear. There is one main element and the other two show up when you click the corresponding button. You can view my current code sample here: https://jsfid ...

Looking to minimize the amount of HTML code in Angularjs by utilizing ng-repeat

Working with some HTML <tr class="matrix_row" ng-repeat="process in matrixCtrl.processes | filter : { park: parentIndex } track by $index"> <td class="properties" ng-click="dashboardCtrl.currParam=0; dashboardCtrl.currentProcess=process"> ...

"Utilizing d3 to parse and track variables within JSON data

To calculate the number of occurrences of s1, s2, and s0 in JSON data and use this information to plot a multiline chart with date (path of date is as follows reviews_details>>variable vf of JSON) on the X-axis versus the number of reviews (s1/s0/s2 ...

Revamp the design of the Google image layout

I am trying to replicate the layout of a Google search results page. My goal is to have a full-width div appear on screen when an image is clicked, with a small triangle at the bottom of the clicked image. I attempted to place the img inside another div, ...

When attempting to display a sub view in Express, a 404 error is encountered

I am currently working with express and jade to display a web page. My intention is to render the page using this format 'http://127.0.0.1:5000/services/landfreight' but I keep encountering a 404 error. router.get('/service/landfreight' ...

Prevent DIV from shifting upwards while resizing the window

I am in the process of creating a responsive website, and I encountered an issue with a particular DIV that spans 100% width of the body. Whenever I resize the browser window, this DIV moves upwards and covers the one above it. This problem only occurs whe ...

Eliminate the need for jQuery when performing basic text rotation operations

Currently, I find myself loading an entire jQuery library for a task that could be achieved with much simpler code. The task involves cycling through a list of text spans to display different messages each time. Below is the existing code: (function($) ...