JavaScript Spotlight Effect on Mousemove
02/19/2021
Contents
Demo
Code
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Spotlight Effect</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">
<div id="spotlight" class="spotlight">
<h1>Spotlight Effect<br>on Mousemove</h1>
</div>
</div>
</body>
</html>
CSS
@charset "utf-8";
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
html {
font-size: 16px;
}
body {
font-family: futura-pt, sans-serif;
-webkit-tap-highlight-color: rgba(0,0,0,0);
cursor: none;
}
#container {
position: relative;
width: 100%;
height: 100vh;
background-image: url(bg.jpg);
background-position: center;
background-size: cover;
background-repeat: no-repeat;
}
.spotlight {
display: flex;
justify-content: center;
align-items: center;
position: absolute;
width: 100%;
height: 100%;
background-image: radial-gradient(circle, transparent 40px, rgba(0, 0, 0, .8) 55px);
}
h1 {
margin: 0;
color: #fff;
font-size: 3rem;
letter-spacing: 1px;
text-align: center;
}
@media screen and (max-width: 480px) {
h1 {
font-size: 2rem;
}
}
JavaScript
var spotlight = document.getElementById("spotlight");
window.addEventListener('mousemove', e => {
spotlight.style.backgroundImage = "radial-gradient(circle at " + e.clientX + "px " + e.clientY + "px, transparent 40px, rgba(0, 0, 0, .8) 55px)";