JavaScript Custom Cursor with Icon and Text

JavaScript Custom Cursor with Icon and Text
Code Snippet:Circle Text Hover Effect
Author: Adam Curzon
Published: March 4, 2024
Last Updated: March 4, 2024
Downloads: 503
License: MIT
Edit Code online: View on CodePen
Read More

This JavaScript code helps you to create a custom cursor with icon and text that changes based on hovered links. The cursor follows the mouse movement and displays letters from the link text along with corresponding Font Awesome icons. It helps improve visual interaction and navigation by enhancing cursor feedback on links.

You can use this code on websites to upgrade cursor interactions. The major benefit is improved visual feedback, making navigation more engaging and intuitive for visitors.

How to Create a Custom Cursor with Icon and Text

1. First of all, include the Font Awesome stylesheet in your HTML file’s header for the icons:

<link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/all.min.css'>

2. Create the HTML structure with a cursor container, icon, link, and navigation links:

<div class="cursor">
  <div class="icon"></div>
  <div class="link"></div>
</div>
<nav>
  <a href="" id="home">Home</a>
  <a href="">Services</a>
  <a href="">About</a>
  <a href="">Contact</a>
</nav>

<div class="youtube">
  <style>
    .youtube {
      position: absolute;
      bottom: 0;
      left: 0;
      background: rgba(0, 0, 0, 0.4);
      color: white;
      left: auto;
      right: auto;
      margin: 0 auto;
      text-align: center;
      padding: 20px 50px;
      border-top-left-radius: 20px;
      border-top-right-radius: 20px;
      font-weight: 700;
            color: #0077ee !important;
    }

    .youtube a {
    }
  </style>youtube.com/@AdamCurzonDev
</div>

3. Style the cursor, navigation links, and additional elements using the following CSS code.

:root {
  --circle-size: 25px;
  --letter-width: 14px;
  --letter-space: 60px;
  --font-size: 10px;
  --letter-spacing-x: 20;
  --second-circle-border: 4px;
  --second-circle-size: 35px;
}

body {
  font-family: 'Ubuntu', sans-serif;
  margin: 0 auto;
  background-color: #191a1e !important;
  color: white;
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
}

.cursor {
  position: absolute;
  width: 25px;
  height: 25px;
  background: white;
  border-radius: 100%;
  cursor: none;
  z-index: 100;
  transition: background .1s ease;
  mix-blend-mode: difference;
}

.cursor .link {
  position: absolute;
  top: 0;
  color: white;
  animation: spin 5s infinite linear;
  transform-origin: center center;
  width: 25px;
  height: 25px;
}

.cursor .icon {
  width: var(--circle-size);
  height: var(--circle-size);
  line-height: var(--circle-size);
  position: absolute;
  top: 0;
  text-align: center;
}

.cursor:after {
  display: block;
  content: '';
  width: 35px;
  height: 35px;
  border-radius: 100%;
  z-index: -1;
  border: var(--second-circle-border) solid white;
  left: -9px;
  top: -9px;
  position: relative;
  transition: opacity .2s ease, transform .3s ease;
}

.cursor.active {
  background: rgba(0,0,0,0);
}

.cursor.active:after {
  opacity: 0;
  transform: scale(2);
}

nav {
  display: flex;
}

nav a {
  text-decoration: none !important;
  color: white;
  font-weight: 700;
  font-size: 30px;
  padding: 20px 40px;
}


.circle-letter {
  position: absolute;
  width: var(--letter-width);
  text-align: center;
  height: calc(var(--circle-size) + var(--letter-space));
  left: calc(var(--circle-size) / 2 - var(--letter-width) / 2);
  top: calc(var(--letter-space) / 2 * -1);
  text-transform: uppercase;
  font-weight: 700;
  font-size: var(--font-size);
}

.circle-letter-bottom {
  width: var(--letter-width);
  height: var(--letter-width);
  text-align: center;
  position: absolute;
  bottom: 0;
  transform: rotate(180deg);
}

@keyframes spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

4. Finally, add the following JavaScript code at the end of your HTML body or within a script tag. This code dynamically updates the cursor based on mouse movements and hovered links.

const letterSpacing = getComputedStyle(
  document.querySelector(":root")
).getPropertyValue("--letter-spacing-x");

var cursor = document.querySelector(".cursor");
var cursorLink = document.querySelector(".link");
var cursorIcon = document.querySelector(".icon");

var iconMap = {
  Home: "fa-house",
  Services: "fa-bell",
  About: "fa-user",
  Contact: "fa-envelope"
};

document.addEventListener("mousemove", (event) => {
  cursor.style.left = event.clientX - cursor.offsetWidth / 2 + "px";
  cursor.style.top = event.clientY - cursor.offsetHeight / 2 + "px";

  cursor.classList.remove("active");
  cursorLink.innerHTML = "";
  cursorIcon.innerHTML = "";

  let elements = document.elementsFromPoint(event.clientX, event.clientY);
  elements.forEach((elem) => {
    if (elem.tagName == "A") {
      cursor.classList.add("active");

      elem.innerHTML.split("").forEach((letter, i) => {
        var circleLetter = document.createElement("div");
        circleLetter.classList.add("circle-letter");
        circleLetter.innerHTML = letter;
        circleLetter.style.transform = "rotate(" + i * letterSpacing + "deg)";

        var circleLetterBottom = document.createElement("div");
        circleLetterBottom.classList.add("circle-letter-bottom");
        circleLetterBottom.innerHTML = letter;
        circleLetter.appendChild(circleLetterBottom);

        cursorLink.appendChild(circleLetter);
      });

      if (iconMap[elem.innerHTML]) {
        var circleIcon = document.createElement("i");
        circleIcon.classList.add("fa");
        circleIcon.classList.add(iconMap[elem.innerHTML]);
        cursorIcon.appendChild(circleIcon);
      }
    }
  });
});

You can customize the iconMap object to map specific link texts to Font Awesome icons.

That’s all! hopefully, you have successfully created Javascript Custom Cursor With javascript custom cursor with icon and text. If you have any questions or suggestions, feel free to comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X