Hey there, web design enthusiasts! Have you ever come across a website with a playful cursor effect that instantly grabbed your attention? Well, today we'll show you how to create a fun and engaging circle-following cursor using CSS and JavaScript. Not only will this effect add a touch of magic to your web pages, but it's also a fantastic way to enhance user experience and interaction!
So, let's dive into the world of coding and learn how to create this mesmerizing effect.
Step 1: HTML Setup We'll start by creating a simple HTML structure that includes a div
element for our circle:
<body>
<div class="circle"></div>
<script src="script.js"></script>
</body>
Step 2: CSS Styling Next, let's add some style to our circle. We want to make sure it has the right shape, size, and appearance, so we'll add a few CSS properties to achieve this:
.circle {
position: absolute;
width: 50px;
height: 50px;
background-color: #f06;
border-radius: 50%;
transform: translate(-50%, -50%);
pointer-events: none;
transition: all 0.15s ease;
}
Step 3: JavaScript Magic Now it's time to add the magic touch with JavaScript. We'll create a script.js file that will make the circle follow the cursor as it moves across the screen:
const circle = document.querySelector('.circle');
document.addEventListener('mousemove', (e) => {
const mouseX = e.clientX;
const mouseY = e.clientY;
circle.style.left = `${mouseX}px`;
circle.style.top = `${mouseY}px`;
});
In this code, we first select the circle element using querySelector
. Then, we add a mousemove
event listener to the document. This listener will update the circle's position based on the mouse's clientX
and clientY
coordinates.
Step 4: Test and Enjoy! That's it! You've created a fun and interactive circle-following cursor effect for your web pages. Test it out in your browser to see the magic come to life!
Conclusion: By combining the power of CSS and JavaScript, you can create engaging and interactive elements that capture the user's attention and enhance their experience. So, don't be afraid to experiment with different effects and add a touch of playfulness to your web designs. Happy coding, and may your cursor always shine with magic! ๐