JavaScript Custom Cursor

02/23/2021

Contents

Demo

Full Screen

Code

HTML

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>JavaScript Custom Cursor</title>
    <link rel="stylesheet" type="text/css" href="https://demo.plantpot.works/assets/css/normalize.css">
    <link rel="stylesheet" href="https://use.typekit.net/opg3wle.css">
    <link rel="stylesheet" type="text/css" href="style.css">
  </head>
  <body>
    <div id="container">
      <h1>JavaScript<br>Custom Cursor</h1>
      <div id="cursor" class="cursor"></div>
    </div>
  </body>
</html>

CSS

@charset "utf-8";
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}
html {
  font-size: 16px;
}
body {
  background-color: #03031b;
  font-family: futura-pt, sans-serif;
  -webkit-tap-highlight-color: rgba(0,0,0,0);
}
#container {
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  width: 100%;
  height: 100vh;
}
.cursor {
  position: absolute;
  width: 25px;
  height: 25px;
  border: 2px solid #e6277a;
  border-radius: 50%;
}
h1 {
  margin: 0;
  color: #fff;
  font-size: 4rem;
  letter-spacing: 1px;
  text-align: center;
}
@media screen and (max-width: 480px) {
  h1 {
    font-size: 2.25rem;
  }
}

JavaScript

var cursor = document.getElementById("cursor");

window.addEventListener('mousemove', e => {
  cursor.style.top = (e.clientY - 20) + 'px';
  cursor.style.left = (e.clientX - 15) + 'px';
});