Exploring the z-index property in CSS along with implementing mouse interaction on background using

Currently, I am attempting to incorporate a radial gradient as the background of my blog post card. My objective is for this radial gradient to move along with the cursor when the user hovers over the blog post card. To better illustrate this effect, you can view an example of my code here: https://codepen.io/D_s/pen/OJNpNBV.

The challenge I am facing is that the functionality is not working as anticipated due to other div elements within the card (such as the title, image, tags, and button) causing interference with the hovering effect. How can I go about resolving this issue? Below is the snippet of code pertaining to the blog post card:

let backgroundgradient = document.querySelector('.card');
backgroundgradient.onmousemove = function(e) {
let rect = e.target.getBoundingClientRect();
let x = e.clientX - rect.left;
let y = e.clientY - rect.top;
backgroundgradient.style.setProperty('--x', x + 'px');
backgroundgradient.style.setProperty('--y', y + 'px');
}

Any assistance provided would be greatly appreciated! Thank you.

Answer №1

Try using .offsetLeft/.offsetRight of the .card element instead of e.target.getBoundingClientRect() and .left/.right, especially if the child elements are being hovered.

let backgroundgradient = document.querySelector('.card');
backgroundgradient.onmousemove = function(e) {
  let x = e.clientX - backgroundgradient.offsetLeft;
  let y = e.clientY - (backgroundgradient.offsetTop - window.pageYOffset);
  backgroundgradient.style.setProperty('--x', x + 'px');
  backgroundgradient.style.setProperty('--y', y + 'px');
}
...

For handling multiple card elements:

let cards = document.querySelectorAll('.card');

for (let card of cards) {
  card.onmousemove = function(e) {
    let x = e.clientX - card.offsetLeft;
    let y = e.clientY - (card.offsetTop - window.pageYOffset);
    card.style.setProperty('--x', x + 'px');
    card.style.setProperty('--y', y + 'px');
  }
}
...

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 process of executing asynchronous operations and then triggering the onResult function

Fiddle: http://jsfiddle.net/smartdev101/eLxxpjp3/ In the asyncAction function, a promise is created to sequence two asynchronous operations - getRecords and getTotal. This is followed by a final call to onResult(data) upon success or onFail(info) in case ...

Add the value of the key into the array of objects with matching keys

Consider the following array of objects: const Info = [{ "id": "1aa2", "details": [{ "name": "rusty", "age": "12", "favourite": [ObjectId("602b69 ...

Dynamically selecting themes in an Angular application based on user input

Experimenting with an angular project where users can choose themes from a dropdown menu to modify the appearance of the application using this handy tutorial Utilizing :host-context for this purpose Encountering an issue where the selected themes are no ...

Updating the title in Angular with UI Router when $stateChangeSuccess is triggered too soon?

My implementation of a custom page title follows the ngBoilerplate framework: https://github.com/ngbp/ngbp/blob/v0.3.1-release/src/app/app.js .controller( 'AppCtrl', function AppCtrl ( $scope, $location ) { $scope.$on('$stateChangeSucces ...

Unable to retrieve data within a customer component

Currently, I am engrossed in a personal project where I am crafting an odds comparison platform using Next.js for the frontend and Django REST Framework in conjunction with Django Channels for the backend. Despite immersing myself in the Next.js documentat ...

Incorporate object keys into an array using JavaScript

Query: I'm working on a JavaScript project and I have an array that looks like this: [6.7, 8, 7, 8.6]. I need to transform this array into an array of objects with named properties: [{y: 6.7} , {y: 8}, {y: 7}, {y: 8.6}]. Can someone guide me on how to ...

TypeScript compiler encountering issue with locating immutable.js Map iterator within for of loop

I am currently facing a challenge with using immutable.js alongside TypeScript. The issue lies in convincing the TypeScript compiler that a Map has an iterator, even though the code runs smoothly in ES6. I am perplexed as to why it does not function correc ...

What is the best way to fill in references that are nested within an array object using mongoose?

How do I go about populating the product fields within the cart array provided below? { "_id": "5bbd1c6e2c48f512fcd733b4", "cart": [ { "count": 1, "_id": "5bbd30ed9417363f345b15fc", "product": "5ba93a6d ...

Chart displaying a comparison between two different dates

I have been using the Morris plugin to create a line graph, but I am facing challenges when it comes to combining two different sets of data in one graph. You can find my initial graph on this fiddle. function formatDate(myDate){ var m_names = new Arr ...

The HTML modal window is experiencing loading issues

I am attempting to implement a modal box that appears on click in an HTML document. The HTML code looks like this: <a href="#openModal">Open Modal</a> <div id="openModal" class="modalDialog"> </div> <a href="#openModal">O ...

Creating a file using cloud services and transferring it to a storage bucket: A step-by-step guide

Recently, I've been working on a task to generate a file named sample.txt with the text "hello world" and then upload it to a bucket. Is there a specific way to achieve this? Below is the code snippet I have attempted: exports.uploadFile = functions.h ...

How can we avoid animations being interrupted by user interaction?

My webpage has an animation that runs smoothly on desktop, but stops as soon as I interact with the page on a touch device. I have tested this issue on Chrome and Safari on iPad. I'm curious if this behavior is intentional on the part of browser vend ...

"Implementing a function to automatically input a selected value from a dropdown menu into a text field

I'm facing an issue and need some help. I have a form with one select field and one text field. What I want is for the text field to display a value corresponding to the option selected in the select field. For example, if I choose "B" from the select ...

How can you leverage JavaScript's map() method to manipulate an element within an array using string functions?

I am attempting to achieve the following. strs = ["one", "two"]; let sorted_str = strs.map((s) => [s.sort(), s]); Basically, my goal is to create a new array of arrays where each nested array consists of the sorted string from the o ...

Turn off all pointer events on the overlaying div but keep scrolling enabled

Issue: I am facing a problem with two containers that have overflowing text content like this: https://i.sstatic.net/wsasV.png In this setup, the blue <div>s have overflow:hidden applied. Now, I am trying to synchronize the scrolling behavior of th ...

I'm looking for a solution to pass a PHP object as a parameter in JavaScript within an HTML environment,

I am currently working on a project using Laravel 5. I have a table in my view, and I want to pass all the data values to a JavaScript function when a link is clicked. I have tried several methods but have been unsuccessful so far. @foreach ($basl_offic ...

Creating vertical alignment in flexbox

How can I add a vertical line that extends between the sidebar and main content on my website? The border-right in my sidebar only goes partway down, and I need it to go all the way. Any help would be greatly appreciated! Link to code snippet ...

Error: Unable to access the 'receiver_id' property because it is undefined

This function is designed to notify the user when they receive a request in my messaging app for educational purposes. However, I am encountering an issue with the firebase console showing the error: firebase functions. TypeError: Cannot read property &ap ...

How can we activate navigation on a mobile browser?

I am currently working on a small HTML5/JavaScript website designed to be accessed by mobile browsers on devices such as Android and iPhone. I am utilizing the geolocation feature of HTML5 to obtain the user's current location, but my goal is to then ...

Can you explain the significance of [$rootScope:infdig] or [$rootScope:inprog] in Angular?

I'm encountering errors in my AngularJS application while using IE 10. The version of AngularJS I'm working with is v1.3.4. These errors occur when transitioning from one page to another. Error: [$rootScope:infdig] http://errors.angularjs.org/1. ...