What is the best way to implement an effect based on the movement or position of the mouse?

I have 3 buttons positioned absolutely:

.micro {
position:absolute;
}

#micro_1 {
left:1165px;
top:176px;
}

#micro_2 {
left:800px;
top:300px;
}

#micro_3 {
left:300px;
top:400px;
}

I am looking to create a fading effect on these 3 elements based on the mouse movement towards one of the images. When the mouse approaches image 1, it will fade in while images 2 and 3 fade out.

To track the mouse position I am using jQuery:

$('body').mousemove(function(e){
    $('#mouse_position').html("mouse position: x=" + e.pageX + "; y=" + e.pageY);

By applying some mathematical calculations, I can achieve the desired effect.

This is what I have implemented so far:

$('body').mousemove(function(e){
        $('#mouse_position').html("mouse position: x=" + e.pageX + "; y=" + e.pageY);
        if (e.pageX > 394 && e.pageX < 533) {
            $('#lueur_video_1').fadeIn('slow');
            $('#lueur_video_2').fadeOut('slow');
            $('#lueur_video_3').fadeOut('slow');
        }

        if (e.pageX > 533 && e.pageX < 722) {
            $('#lueur_video_2').fadeIn('slow');
            $('#lueur_video_1').fadeOut('slow');
            $('#lueur_video_3').fadeOut('slow');
        }

        if (e.pageX > 722 && e.pageX < 1116) {
            $('#lueur_video_3').fadeIn('slow');
            $('#lueur_video_1').fadeOut('slow');
            $('#lueur_video_2').fadeOut('slow');
        }

Answer №1

To create a cool effect, monitor the position of the mouse (e.pageX / e.pageY) in relation to the image's center and adjust the opacity accordingly. Use $("#micro_3").offset() to determine the elements' positions on the page.

Determine the fading range by setting minimum and maximum distances for the effect. When the mouse is outside the maximum distance, opacity is 0; when inside the minimum distance, opacity is 1. For values in between, calculate opacity using formula

(Range - CurrentDistance) / Range
.

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

positioned division towards a subset

Is there a way to align a div with a span? For instance: <div style="some magic">foo bar<div> <p>this is a random text with <span class="stressed">stressed phrase</span> in it</p> The desired output would look like th ...

Double Trouble: KnockoutJS causing an ajax form submission glitch

Dealing with a form that is persistently submitting twice can be quite frustrating. I've gone through various jQuery-related queries on Stack Overflow, but haven't come across a definitive solution yet. Additionally, scouring Google groups for an ...

An alternative to PHP's exec function in Node.js

I am interested in developing a piece of software using C (such as prime number factorization) and hosting it on my web server with Node.js. Following that, I would like to create an HTML document containing a form and a button. Upon clicking the button, ...

Tips for overlaying text onto a MaterialUI icon

https://i.stack.imgur.com/cFr5Y.pngI am currently working on a project that requires me to overlay a number on top of an icon. Specifically, I am utilizing the MaterialUI ModeComment icon and attempting to display text over it. Despite trying the following ...

Dynamic active class implementation in the Bootstrap tabs

I'm having trouble changing the active class based on the selected thumbnail (see code below). I want the active class to be determined by the thumbnail that is clicked. html <h2 class="">title</h2> <div class="row text-center a ...

The problem with column width in Datatable persists even after refreshing the row data

Utilizing datatable to generate the table showcased below: https://i.sstatic.net/N795U.png Satisfied with the outcome as each column's width is automatically calculated, keeping one line per row for a clean look. The API generates the table above us ...

What is the best way to locate an element using text in xpath?

Having an issue with this HTML source code: <td class="specs_title"> Processortype <a href="#" class="info-link"> <img src="x.jpg" title="" height="16" alt="" width="16" /> <span class="info-popup"> <span class="hd">Processor ...

Wondering how to modify an image source when clicked using javascript and css?

I am looking to create a menu button labeled "menu" that rotates around using rotateX, and then changes into the text "close". Initially, I attempted to achieve this effect with text and animation, followed by text and transform. When those methods failed, ...

Is it beneficial to utilize components in order to streamline lengthy HTML code, regardless of whether the components do not repeat and do not require any props (such as in Vue or any other JavaScript library

In the Vue.js team project I am currently involved in, I have structured my code in the view as follows: component01 component02 ... There are more than 10 components in total. Each component represents a section of the landing page, consisting mostl ...

I'm puzzled as to why the color isn't showing up on my navigation bar even though I used "padding: 10px !important;"

I really need help with this, I'm quite new to all of this and it's really confusing me. When I remove this line, the color shows up again, but when I add it back in, it just disappears. I've been following a tutorial on YouTube on how to cr ...

Unable to connect Dropzone to dynamically loaded DIV using AJAX

Using Dropzone for client-side image uploads has been a breeze. Take a look at this basic example that is currently up and running: If you examine the source code, you'll notice that I am utilizing JQuery to connect Dropzone to the upload1 div ID. H ...

Show the component on all routes with the exception of a few in react-router-dom version 6

In my web app, I am working on setting up various routes using react-router. Some pages require shared components like the Navigation or Footer, while others do not. What I need is a way to determine if a path does not match specific locations and only re ...

Troubleshooting: Why the TableTools basic usage example is not functioning as

After replicating the code from http://datatables.net/release-datatables/extensions/TableTools/examples/simple.html in my Visual Studio project, I organized the files by saving two css files as style1.css and style2.css, along with three js files named sc ...

Having issues passing parameters with Ajax, Python Bottle, Jquery, and JSON collaboration

Every time I make an AJAX request POST, the newUser() function seems to be ignoring any arguments being passed even though I have filled out both userInput and passInput fields. Here's the JS/JQ/AJAX code snippet: var userInput = document ...

Eliminate redundant entries from an Ionic3 array

Looking for a solution to eliminate duplicate values in the storage array within Ionic3 this.storage.get('thestations').then((val) => { for(let i =0;i<val.length;i++){ if(this.newarray.indexOf(this.newarray) == -1) ...

Fixing the slide bar in React using styled components

In the early stages of creating a slider where cards (divs) can be moved left and right with a click, I encountered an issue. The onClick handler is functioning properly. However, upon running the project, I noticed that the cards start 230px away from the ...

Combining CSS styles

Currently, I am in the process of integrating RTL support into a vast framework. To achieve this, I have replaced all directional rules with mixins like: a { @include padding(0, 1px, 0, 0); @include margin(0, 1px, 0, 0); } This implementation ensures ...

Unexpected labels continue to pop up in the footer of the HTML file

I have noticed that the following HTML always appears at the very bottom of the body tag in all my projects, regardless of whether I am using React.js or not. Interestingly, when I switch to an incognito browser, these tags disappear. Curiously, these sa ...

"Material-Table does not have the ability to insert a new row

Just started learning JS and React. I'm attempting to integrate Material-table with an add row button, but encountering issues where the added row is not reflecting. Every time I refresh the page, the rows are reset. I suspect there's a problem w ...

Utilize hosted jQuery in HTML on IIS or locally: Which is better?

Currently, I am facing an issue with embedding hosted jQuery in my web application. When using the "hosted" version of jQuery from an external URL like this: <script src="http://code.jquery.com/jquery-latest.js"></script> The script doesn&apo ...