From 1f8845dddeb5468e893ac1f628f5b59c3a84d708 Mon Sep 17 00:00:00 2001 From: Abhi-hertz <93651229+AE-Hertz@users.noreply.github.com> Date: Sun, 10 Nov 2024 14:17:48 +0530 Subject: [PATCH] bug: custom cursor fixed --- src/App.css | 15 ++++++++++++--- src/App.jsx | 25 ++++++++++++++++++++----- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/src/App.css b/src/App.css index 6a870459..83387082 100644 --- a/src/App.css +++ b/src/App.css @@ -74,15 +74,20 @@ html { /* Custom Cursor Styling */ .custom-cursor { - position: fixed; + position: absolute; width: 20px; height: 20px; border-radius: 50%; background-color: green; /* Main cursor color */ pointer-events: none; transform-origin: center; - transition: transform 0.1s ease; + transition: transform 0.1s ease, opacity 0.2s ease; z-index: 9999; /* Ensure cursor is on top */ + opacity: 1; +} + +.custom-cursor.hidden { + opacity: 0; } /* Cursor Tail Styling */ @@ -94,5 +99,9 @@ html { background-color: green; /* Tail color */ pointer-events: none; z-index: 9998; - transition: transform 0.1s ease; + transition: transform 0.1s ease, opacity 0.2s ease; } +.cursor-tail.hidden { + opacity: 0; +} + diff --git a/src/App.jsx b/src/App.jsx index e5ad46f8..81c3055b 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -3,7 +3,6 @@ import React, { useEffect, useState } from 'react'; import "./App.css"; import { Route, Routes } from "react-router-dom"; -import './App.css'; // Pages and Components import { Home, Login, Registration, Dashboard, ComingSoon } from "./pages"; @@ -27,6 +26,7 @@ const App = () => { const [isPreloaderVisible, setIsPreloaderVisible] = useState(true); const [cursorPos, setCursorPos] = useState({ x: 0, y: 0 }); const [trail, setTrail] = useState(Array(10).fill({ x: 0, y: 0 })); + const [isCursorVisible, setIsCursorVisible] = useState(true); // Controls visibility // Hide Preloader after 5 seconds useEffect(() => { @@ -43,16 +43,31 @@ const App = () => { setCursorPos({ x: e.clientX, y: e.clientY }); }; + const handleMouseLeave = () => { + setIsCursorVisible(false); + }; + + const handleMouseEnter = () => { + setIsCursorVisible(true); + }; + useEffect(() => { - window.addEventListener("mousemove", updateCursor); - return () => window.removeEventListener("mousemove", updateCursor); + document.addEventListener("mousemove", updateCursor); + document.addEventListener("mouseleave", handleMouseLeave); + document.addEventListener("mouseenter", handleMouseEnter); + + return () => { + document.removeEventListener("mousemove", updateCursor); + document.removeEventListener("mouseleave", handleMouseLeave); + document.removeEventListener("mouseenter", handleMouseEnter); + }; }, [trail]); return ( <> {/* Custom Main Cursor */}