Mouse Effect in Nextjs can greatly enhance the user experience by adding a touch of interactivity to your website. It's a simple yet effective way to make your site feel more dynamic.
By using Mouse Effect in Nextjs, you can create a hover effect that changes the background color of an element, making it stand out from the rest of the page. This can be achieved by adding a simple CSS class to your component.
The Mouse Effect in Nextjs also allows you to animate the transition between different states, making the interaction feel smoother and more engaging. This can be done by using a combination of CSS transitions and Nextjs's built-in animation features.
By incorporating Mouse Effect in Nextjs into your project, you can create a more immersive and interactive user experience that keeps visitors engaged.
Customizing Cursor Behavior
You can make the spotlight follow the mouse cursor by using CSS variables and JavaScript. To set the position of the before and after pseudo elements, we used translate-x-[var(--mouse-x)] and translate-y-[var(--mouse-y)].
This involves creating a JavaScript class called Spotlight that sets --mouse-x and --mouse-y CSS variables on each card when hovering the card container.
The Spotlight class updates --mouse-x and --mouse-y values on mouse move, but only if the mouse cursor is inside the card container.
To achieve this, we're using the MousePosition.ts component, which gets the mouse coordinates.
Here are the key steps to customize cursor behavior:
- Use CSS variables to set the position of pseudo elements
- Create a JavaScript class to update CSS variables on mouse move
- Use a component to get mouse coordinates
By following these steps, you can create a custom cursor behavior that follows the mouse cursor.
Adding Interactive Elements
To add interactive elements to your Next.js project, you need to listen for mouse movement events. This can be achieved by listing the first event for the DOMContentLoaded event and then proceeding.
You can use JavaScript to track the mouse movement and apply the glow effect. For example, you can write a JavaScript function that listens for mouse movement over the .glow-capture element and updates CSS variables to move the glow effect.
Here are the key steps to follow:
- Listed first for the DOMContentLoaded event and then proceed
- Find all elements with the .glow-capture class
- Loop through each element with the .glow-capture class and clone it.
- The overlay div added in our HTML gets a new child element which effectively doubles our original fish card.
- On the individual card, we listen for both the mousemove and mouseleave events, setting custom CSS styles based on the mouse x and y position. If you leave the card with your mouse, we remove opacity, so you can't see the "glow” effect.
Add JavaScript Tracking
To add interactive elements to your web page, you'll need to write some JavaScript code to track the mouse movement and apply the glow effect.
First, you'll need to listen for the DOMContentLoaded event, which is triggered when the HTML document has been completely loaded. This is typically listed first in the JavaScript code.
To track the mouse movement, you'll need to find all elements with the .glow-capture class and loop through each element to clone it. This effectively doubles the original element, allowing you to apply the glow effect to both.
You'll also need to listen for both the mousemove and mouseleave events on the individual card, setting custom CSS styles based on the mouse x and y position. If you leave the card with your mouse, you'll remove the opacity, making the glow effect invisible.
Here are the steps to add JavaScript tracking in detail:
- Listen for the DOMContentLoaded event.
- Find all elements with the .glow-capture class.
- Clone each element with the .glow-capture class.
- Listen for mousemove and mouseleave events on the individual card.
- Set custom CSS styles based on the mouse x and y position.
- Remove opacity on mouseleave event.
Adding Glow Variant Classes
Adding Glow Variant Classes is a game-changer for interactive elements. You can apply the new Tailwind CSS variant called glow to elements within a card to change as you navigate the card with your mouse.
The glow variant can be applied to elements like the card, title, button, and fish image. This produces a neat effect based on the —glow-color variable passed to the div with the class glow-overlay.
The glow variant can be used like any other Tailwind CSS variant. This means you can easily customize the appearance of your interactive elements with just a few lines of code.
Design Variations
You can create different effects by applying Tailwind CSS variants to specific elements within a card. This can be done by introducing new classes and variables, such as the glow variant.
The glow variant can be applied to various elements, like the card, title, button, and fish image, to produce a neat effect based on the —glow-color variable passed to the div with the class glow-overlay.
Adding the glow variant to individual elements allows for a high degree of customization and control over the design.
Sources
- https://flowbite.com/docs/components/carousel/
- https://cruip.com/how-to-create-a-spotlight-card-hover-effect-with-tailwind-css/
- https://developer.mozilla.org/en-US/docs/Web/CSS/pointer-events
- https://webcrunch.com/posts/mouse-tracking-glow-effect-tailwind-css
- https://nextjs.org/learn/react-foundations/updating-state
Featured Images: pexels.com