Close Menu

    Subscribe to Updates

    Get the latest creative news from FooBar about art, design and business.

    What's Hot

    How to make Magic Social Share Menu using HTML CSS and JS

    5 February 2026

    How to Make Memory Unmasked Game in HTML CSS & JavaScript

    4 February 2026

    How to Make Heart Animation in HTML CSS & JavaScript

    2 February 2026
    Facebook X (Twitter) Instagram YouTube Telegram Threads
    Coding StellaCoding Stella
    • Home
    • Blog
    • HTML & CSS
      • Login Form
    • JavaScript
    • Hire us!
    Coding StellaCoding Stella
    Home - JavaScript - How to make Magic Mouse Effect using HTML CSS & JavaScript
    JavaScript

    How to make Magic Mouse Effect using HTML CSS & JavaScript

    Coding StellaBy Coding Stella24 April 2024No Comments5 Mins Read
    Share Facebook Twitter Pinterest LinkedIn Tumblr Reddit Email WhatsApp Copy Link

    Let’s create a Magic Mouse Effect using HTML, CSS, and JavaScript. This effect will add a touch of interactivity to the mouse cursor, creating a captivating visual experience as it moves across the screen.

    We’ll keep it mesmerizing yet straightforward, using HTML to set up the structure, CSS for styling and animations, and JavaScript to handle the mouse movement interactions.

    Let’s dive into building the Magic Mouse Effect. Whether you’re a beginner or an experienced developer, this project offers an enchanting way to explore the possibilities of web design and interaction.

    Let’s make the mouse cursor come alive with magic!

    HTML :

    This HTML code sets up a webpage titled “Magic Mouse Effect” with a link to an external stylesheet (style.css) and JavaScript file (script.js). The stylesheet defines the visual styling for the webpage, while the JavaScript file contains the logic for the magic mouse effect. Additionally, it imports the Font Awesome kit to use its icons within the webpage.

    <!DOCTYPE html>
    <html lang="en" >
    <head>
      <meta charset="UTF-8">
      <title> Magic Mouse Effect </title>
      <link rel="stylesheet" href="./style.css">
    
    </head>
    <body>
    
      <script src='https://kit.fontawesome.com/1ee8f271b9.js'></script><script  src="./script.js"></script>
    
    </body>
    </html>

    CSS :

    This CSS code defines the visual styling for a star animation effect on a webpage. It includes variables for glow color, sets a gradient background, and styles stars with animations for movement, rotation, scale, and opacity. The glow-point class creates a glowing effect, while the star class styles the stars themselves.

    :root {
      --glow-rgb: 239 42 201;
    }
    
    body {
      background: linear-gradient(145deg, rgb(119, 46, 195), rgb(58, 18, 153));
      height: 100vh;
      overflow: hidden;
    }
    
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }
    
    .glow-point {
      position: absolute;
      box-shadow: 0rem 0rem 1.2rem 0.6rem rgb(var(--glow-rgb));
      pointer-events: none;
    }
    
    .star {
      position: absolute;
      z-index: 2;
      color: white;
      font-size: 1rem;
      animation-duration: 1500ms;
      animation-fill-mode: forwards;
      pointer-events: none;
    }
    
    @keyframes fall-1 {
      0% {
        transform: translate(0px, 0px) rotateX(45deg) rotateY(30deg) rotateZ(0deg) scale(0.25);
        opacity: 0;
      }
      
      5% {
        transform: translate(10px, -10px) rotateX(45deg) rotateY(30deg) rotateZ(0deg) scale(1);
        opacity: 1;
      }
      
      100% {
        transform: translate(25px, 200px) rotateX(180deg) rotateY(270deg) rotateZ(90deg) scale(1);
        opacity: 0;
      }
    }
    
    @keyframes fall-2 {
      0% {
        transform: translate(0px, 0px) rotateX(-20deg) rotateY(10deg) scale(0.25);
        opacity: 0;
      }
      
      10% {
        transform: translate(-10px, -5px) rotateX(-20deg) rotateY(10deg) scale(1);
        opacity: 1;
      }
      
      100% {
        transform: translate(-10px, 160px) rotateX(-90deg) rotateY(45deg) scale(0.25);
        opacity: 0;
      }
    }
    
    @keyframes fall-3 {
      0% {
        transform: translate(0px, 0px) rotateX(0deg) rotateY(45deg) scale(0.5);
        opacity: 0;
      }
      
      15% {
        transform: translate(7px, 5px) rotateX(0deg) rotateY(45deg) scale(1);
        opacity: 1;
      }
      
      100% {
        transform: translate(20px, 120px) rotateX(-180deg) rotateY(-90deg) scale(0.5);
        opacity: 0;
      }
    }

    JavaScript:

    This code creates a visual effect on a webpage where stars appear and follow the movement of the mouse cursor or touch input. It initializes parameters and helper functions, then handles mouse or touch movement events. Stars are created at the cursor’s position, with trailing glow effects. Stars appear at intervals based on movement and time, with parameters like animation duration and colors configurable.

    let start = new Date().getTime();
    
    const originPosition = { x: 0, y: 0 };
    
    const last = {
      starTimestamp: start,
      starPosition: originPosition,
      mousePosition: originPosition
    }
    
    const config = {
      starAnimationDuration: 1500,
      minimumTimeBetweenStars: 250,
      minimumDistanceBetweenStars: 75,
      glowDuration: 75,
      maximumGlowPointSpacing: 10,
      colors: ["249 146 253", "252 254 255"],
      sizes: ["1.4rem", "1rem", "0.6rem"],
      animations: ["fall-1", "fall-2", "fall-3"]
    }
    
    let count = 0;
      
    const rand = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min,
          selectRandom = items => items[rand(0, items.length - 1)];
    
    const withUnit = (value, unit) => `${value}${unit}`,
          px = value => withUnit(value, "px"),
          ms = value => withUnit(value, "ms");
    
    const calcDistance = (a, b) => {
      const diffX = b.x - a.x,
            diffY = b.y - a.y;
      
      return Math.sqrt(Math.pow(diffX, 2) + Math.pow(diffY, 2));
    }
    
    const calcElapsedTime = (start, end) => end - start;
    
    const appendElement = element => document.body.appendChild(element),
          removeElement = (element, delay) => setTimeout(() => document.body.removeChild(element), delay);
    
    const createStar = position => {
      const star = document.createElement("span"),
            color = selectRandom(config.colors);
      
      star.className = "star fa-solid fa-sparkle";
      
      star.style.left = px(position.x);
      star.style.top = px(position.y);
      star.style.fontSize = selectRandom(config.sizes);
      star.style.color = `rgb(${color})`;
      star.style.textShadow = `0px 0px 1.5rem rgb(${color} / 0.5)`;
      star.style.animationName = config.animations[count++ % 3];
      star.style.starAnimationDuration = ms(config.starAnimationDuration);
      
      appendElement(star);
    
      removeElement(star, config.starAnimationDuration);
    }
    
    const createGlowPoint = position => {
      const glow = document.createElement("div");
      
      glow.className = "glow-point";
      
      glow.style.left = px(position.x);
      glow.style.top = px(position.y);
      
      appendElement(glow)
      
      removeElement(glow, config.glowDuration);
    }
    
    const determinePointQuantity = distance => Math.max(
      Math.floor(distance / config.maximumGlowPointSpacing),
      1
    );
    
    const createGlow = (last, current) => {
      const distance = calcDistance(last, current),
            quantity = determinePointQuantity(distance);
      
      const dx = (current.x - last.x) / quantity,
            dy = (current.y - last.y) / quantity;
      
      Array.from(Array(quantity)).forEach((_, index) => { 
        const x = last.x + dx * index, 
              y = last.y + dy * index;
        
        createGlowPoint({ x, y });
      });
    }
    
    const updateLastStar = position => {
      last.starTimestamp = new Date().getTime();
    
      last.starPosition = position;
    }
    
    const updateLastMousePosition = position => last.mousePosition = position;
    
    const adjustLastMousePosition = position => {
      if(last.mousePosition.x === 0 && last.mousePosition.y === 0) {
        last.mousePosition = position;
      }
    };
    
    const handleOnMove = e => {
      const mousePosition = { x: e.clientX, y: e.clientY }
      
      adjustLastMousePosition(mousePosition);
      
      const now = new Date().getTime(),
            hasMovedFarEnough = calcDistance(last.starPosition, mousePosition) >= config.minimumDistanceBetweenStars,
            hasBeenLongEnough = calcElapsedTime(last.starTimestamp, now) > config.minimumTimeBetweenStars;
      
      if(hasMovedFarEnough || hasBeenLongEnough) {
        createStar(mousePosition);
        
        updateLastStar(mousePosition);
      }
      
      createGlow(last.mousePosition, mousePosition);
      
      updateLastMousePosition(mousePosition);
    }
    
    window.onmousemove = e => handleOnMove(e);
    
    window.ontouchmove = e => handleOnMove(e.touches[0]);
    
    document.body.onmouseleave = () => updateLastMousePosition(originPosition);

    In short, creating a Magic Mouse Effect with HTML, CSS, and JavaScript has been an enchanting journey into interactive web design. By combining these technologies, we’ve added a captivating visual experience to the mouse cursor movement on the screen. Whether you’re new to coding or experienced, this project offers a mesmerizing way to enhance user engagement.

    If your project has problems, don’t worry. Just click to download the source code and face your coding challenges with excitement. Have fun coding!

    Share. Copy Link Twitter Facebook LinkedIn Email WhatsApp
    Previous ArticleHow to make Monster Alien Star Rating using HTML CSS & JavaScript
    Next Article How to make Scroll Drive All The Things using HTML CSS & JavaScript
    Coding Stella
    • Website

    Related Posts

    JavaScript

    How to Make Memory Unmasked Game in HTML CSS & JavaScript

    4 February 2026
    JavaScript

    How to Make Heart Animation in HTML CSS & JavaScript

    2 February 2026
    JavaScript

    How to Make Rock Paper Scissors Game in HTML CSS & JavaScript

    29 January 2026
    Add A Comment
    Leave A Reply Cancel Reply

    Trending Post

    Master Frontend in 100 Days Ebook

    2 March 202432K Views

    How to make Modern Login Form using HTML & CSS | Glassmorphism

    11 January 202431K Views

    How to make I love you Animation in HTML CSS & JavaScript

    14 February 202424K Views

    How to make Valentine’s Day Card using HTML & CSS

    13 February 202415K Views
    Follow Us
    • Instagram
    • Facebook
    • YouTube
    • Twitter
    ads
    Featured Post

    How to Become a Full-Stack Web Developer?

    24 January 2024

    How to create Glowing Tubes Cursor using HTML CSS and JS

    28 January 2026

    How to make Egg toggle Switch Animated using HTML & CSS

    10 September 2025

    How to create Parallax Scroll Animation using HTML CSS and JS

    17 August 2025
    Latest Post

    How to make Magic Social Share Menu using HTML CSS and JS

    5 February 2026

    How to Make Memory Unmasked Game in HTML CSS & JavaScript

    4 February 2026

    How to Make Heart Animation in HTML CSS & JavaScript

    2 February 2026

    How to Make Social Media Icons Popups in HTML and CSS

    31 January 2026
    Facebook X (Twitter) Instagram YouTube
    • About Us
    • Privacy Policy
    • Return and Refund Policy
    • Terms and Conditions
    • Contact Us
    • Buy me a coffee
    © 2026 Coding Stella. Made with 💙 by @coding.stella

    Type above and press Enter to search. Press Esc to cancel.

    Ad Blocker Enabled!
    Ad Blocker Enabled!
    Looks like you're using an ad blocker. We rely on advertising to help fund our site.
    Okay! I understood